简体   繁体   中英

Checking if an ArrayList contains more than 1 of the same integer and removing them from the list?

I ran into a seemingly simple problem that I haven't been able to figure out. Basically, my list holds a number of ints which represent different items, which works fine, but I need to be able to check if the list contains the same integer more than once and then remove them from the list.

if (myList.contains(100)) { // I want to check if the int '100' exists in myList twice
    myList.remove(new Integer(100)); // I want to remove the 2 aforementioned duplicate integers
}

Apologies if my explanation wasn't the clearest. Thanks in advance!

EDIT: To clarify, I want the list to contain duplicates but I want to be able to check if the duplicate exists X times and then remove those instances from the list.

ie

I might want to add the int 100 7 times, and then later check if it exists twice and then remove 2 instances of it from the list only.

1) myList.remove(new Integer(100)); will remove only the first occurrence that is equals to 100 .
You should loop on remove() while the list contains still an object with the same value.

2) To know if the list contains more than once the object, you could use indexOf() and lastIndexOf() .
If these are distinct, it means that you have more than one element.
So according to your requirement, you can remove all of them with the method described in the point 1.

  Integer valueToCheck = 100;
  if ( myList.indexOf(valueToCheck) != myList.lastIndexOf(valueToCheck) ) {   
       while (myList.contains(valueToCheck)){
          myList.remove(valueToCheck);  
       }
   }

You can use a Set which does not allowed duplicate keys eg

Set<Ineger> foo = new HashSet<>(myList);

And you can create a new List from that or use it as it is.

you can create a method to accomplish the task at hand, something along the lines of this:

private static boolean removeSpecfiedNumber(int number,int numberOfTimes, List<Integer> integerList){
        if(integerList.stream().filter(x -> x == number).count() >= numberOfTimes){
             for (int i = 0; i < numberOfTimes; i++) {
                 integerList.remove((Integer)number);
             }
             return true;
        }
        return false;
}
  • The parameter number is the number you want to check its occurrences ie 100 in your case.
  • The parameter numberOfTimes is the number of times you want to remove that element from the list.
  • The parameter integerList is, of course, the list you want to remove the elements from.

Let's take the code below as an example:

List<Integer> myList = new ArrayList<>();
myList.add(100);
myList.add(200);
myList.add(100);
myList.add(100);
myList.add(100);
myList.add(100);
myList.add(100);
myList.add(100);
removeSpecfiedNumber(100,2,myList);
System.out.println(myList);

this will remove 100 twice from myList and should, therefore, yield the elements below:

[200, 100, 100, 100, 100, 100]

The method return type can be void if you want, However, the return type of boolean can come in handy at times hence I've used that approach.

Also, you need not use the static modifier if you're dealing with objects, therefore you can remove it.

Starting java 8 you can use java stream api in such way:

List<Integer> i  = new ArrayList<Integer>();
    i.add(1);
    i.add(2);
    i.add(3);
    i.add(1);

List<Integer> collect = i.stream().distinct().collect(Collectors.toList());

NOTE: new list will be created.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM