ArrayList - Count if the same Object i listed exact 2 times and if: append to "tempFinalFilterSearchList"

how to count the number of occurrences of an element in a arraylist in java
how to count duplicate elements in arraylist in java
java 8 count occurrences in list
how to count number of elements in a list in java
how to count duplicate elements in array in java
java map count occurrences
arraylist number of same elements
count duplicate values in hashmap

Basicly im trying to look through an Arraylist to check if the same object is listed 2 times - and if: append to "tempFinalFilterSearchList"

Im not allowed to use Hashmap (School assignment).

UPDATE - This code actual works now... Now I just need to remove dublicates from "tempFinalFilterSearchList"

public List<Venue> filterToFinalSearchList()
        {

            for (int i=0; i < tempFilterSearchList.size(); i++)
            {

                int occurences=0;

                for (int j = 0; j < tempFilterSearchList.size(); j++)
                {

                    if (tempFilterSearchList.get(i).getVenueId() == tempFilterSearchList.get(j).getVenueId())
                    {
                        occurences++;
                    }

                }

                if (occurences == 2)
                {
                    tempFinalFilterSearchList.add(tempFilterSearchList.get(i));
                }
            }
            return tempFinalFilterSearchList;
        }
  • if the same venueId is listed exact 2 times in "tempfilterSearchList", then the Object have to be added to "tempFinalFilterSearchList"...

  • have tried several different things now, without luck - And also search here / google - there alot of solutions, but all with Hashmap which im not allowed to use.

Thank you in advance for any advise.

First of all keep in mind that using the remove function inside a loop for the same list is not safe(Unless using an iterator). I assume you have a class let's call it A that has the attribute venueId. It looks something like this + other attributes that you want:

public class A {
    private int venueId;

    public A(int venueId) {
        this.venueId = venueId;
    }

    public int getVenueId() {
        return venueId;
    }

    public void setVenueId(int venueId) {
        this.venueId = venueId;
    }
}

1.Create a function that parses the list and counts the number of times an object with the same venueId repeats itself

public boolean doesVenueIdRepeatInList(int venueId, List<A> list) {
    int timesRepeated = 0;

    //Parse the list and count the number of items that have the same venueId
    for (int i = 0; i < list.size(); i++) {
        if (list.get(i).getVenueId() == venueId) {
            timesRepeated++;
        }
    }

    //If the venueId repeats more than 3 times
    if (timesRepeated >= 3) {
        return true;
    }
    return false;
}

3.Now to the code that actually does what you asked. We will parse the list and identify the the objects that repeat more than 3 times. If they repeat more than 3 times we won't add them to the new list

List<A> tempFilterSearchList = Arrays.asList(
        new A(1),
        new A(2),
        new A(1),
        new A(2),
        new A(3),
        new A(1),
        new A(2)
);

//We will be using a new list to put the result in
//It's not safe to use the delete function inside a loop
List<A> filteredList = new ArrayList<>();

//Count the number an object repeats and if it repeats more than 3 times store it inside repeatedVenueIds
for (int i=0; i < tempFilterSearchList.size(); i++)
{
    int venueId = tempFilterSearchList.get(i).getVenueId();
    boolean itRepeat3Times = doesVenueIdRepeatInList(venueId, tempFilterSearchList);

    //If it doesn't repeat more than 3 times add it to the new list
    if(!itRepeat3Times) {
        filteredList.add(tempFilterSearchList.get(i));
    }
}

You have your result inside filteredList

Why does my ArrayList contain N copies of the last item added to the , Why does my ArrayList contain N copies of the last item added to the list? Note: The ArrayList only stores objects so although the above lines appear to add int values to ArrayList the are automatically changed to Integer objects as they are appended to the ArrayList. A standard array can be used to populate an ArrayList by converted it to a List collection using the Arrays.asList method and adding it to the ArrayList

It is a better advanced option to use 'iterator' since it allows you to remove elements while iterating an arraylist

ArrayList<Integer> tempFilterSearchList = new ArrayList<Integer>(Arrays.asList(1,2,5,3,7,3,7,3) );
    Iterator itr = tempFilterSearchList.iterator();
    while (itr.hasNext())
    {
        int count = 0;
        int number = (Integer)itr.next();
        for (int i=0; i < tempFilterSearchList.size(); i++)
        {
            int x = tempFilterSearchList.get(i);
            if (x == number)
            {
                count++;
            }
        }
        if( count != 3 )
        {
            itr.remove();
        }
    }

"using the remove function inside a loop for the same list is not safe. " this will not be an issue if you use iterator in java which is a advance option in java language

Count the number of items in my array list, What is the proper way to get the number of items in an ArrayList called list? The problem with my code despite being able to add the first person and second person successfully is when i add the third person what happens is, it goes through each item in the arrayList and subsequently adds the new person 2 more times. so for instance: check if person name in the arrayList is the same if not add person to the arrayList

Your code must look like the folowing. First of all you should loop over all the elements in tempFilterSearchList. Then for each element, you should count the number of times it appears in the list. I choosed rather to place all the positions where the current elements' venueId occures in separate List to further delete them easly.

ArrayList<Integer> occurrences = new ArrayList<Integer>()
for (int i=0; i < tempFilterSearchList.size(); i++)
    for (int j=0; j < tempFilterSearchList.size(); j++){
        if(tempFilterSearchList.get(i).getVenueId() ==tempFilterSearchList.get(j).getVenueId()){
            occurrences.add(j)
        }
    }

    if(occurrences.size() != 3){
        for(int j:occurrences){
            tempFilterSearchList.remove(occurrences[j])
        }     
    }
}

Count occurrences of elements of list in Java, How do you count the number of occurrences of an element in a ArrayList in Java? [code]List<String> strings = new ArrayList<String>(); strings.put("stack", "overflow", "stack", "yahoo", "google&quot;, &quot;msn&quot;, &quot;MSN&quot;, &quot;stack

Count frequency of elements in a List in Java, How do you count the number of occurrences of an element in a list Java 8? Last element: We use the Length property on arrays, and also the Count property on ArrayList and List, to access the final element. Tip: The final element is located at the count minus one, except when the length of the array is 0.

ArrayList, ArrayList - Count if the same Object i listed exact 2 times and if: append to "​tempFinalFilterSearchList". how to count the number of occurrences of an element in  The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed. Standard Java arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means that you must know in advance how many elements an

C# 2012 for Programmers, At any time, an ArrayList contains a certain number of elements less than or use method TrimToSize ofclass ArrayList to trim an Array- List to its exact Method or property Description Add Capacity Clear Contains Count IndexOf Insert Fig. Returns true if the specified object is in the ArrayList ; otherwise, returns false. Which adds more overhead in various forms. And the memory used by an ArrayList of objects, or an array of those same objects, is generally pretty close to the memory used by the objects themselves. I tried serializing 1000 Integer objects in various forms to see how much space they took: ArrayList of Integers: 10125 bytes Integer[] array: 10112

Comments
  • Let me give you a hint...you need to "count" somewhere the object within the arraylist that has those venueId equals to 3. If that count is actually >=3 then you proceed to erase the ojbects in the arraylist with that venueId. I mean, you could strive the problem using two loops. One for counting, and one for deleting (if is that the case)
  • You're testing if the VenueId of an element is different than 3. That doesn't test if the same object is present 3 times in the list. Start by doing that: write a method that takes a list and an object as argument, and which returns the number of times the object is in the list. Then write another method which calls the first one for every element of the list. If the first method returns 3, remove the object from the list. And of course to do that, you'll need to write a third method which takes a list and an object as argument, and removes every occurrence of the object from the list.
  • What range your ids go?
  • @VidorVistrom Im not sure i understand your question ?.
  • Integer values that your id can hold... Like 1,2,...100
  • This way - the method needs "venueId" - the "venueId" is in the list... ( tempFilterSearchList )
  • I'm not sure what you mean by that and why is that a problem?
  • "using the remove function inside a loop for the same list is not safe. " this will not be an issue if you use iterator in java which is a advance option in java language