-
Bug
-
Resolution: Not an Issue
-
P4
-
None
-
8u221
-
generic
-
generic
ADDITIONAL SYSTEM INFORMATION :
OSX/Java 8
A DESCRIPTION OF THE PROBLEM :
containsAll method is not working as expected when ArrayList containing duplicate value is used.
REGRESSION : Last worked in version 8u221
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
step 1 :- create two Array Lists.
ArrayList<Integer> aList1 = new ArrayList<>(); ArrayList<Integer> aList2 = new ArrayList<>();
Step 2 :- populate one of the ArrayList with distinct values.
aList1.add(1);aList1.add(2);aList1.add(3); // aList1 contains [1,2,3]
Step 3:- populate another ArrayList with duplicate values.
aList1.add(2);aList1.add(2);aList1.add(2); // aList1 contains [2,2,2]
Step 4:- use containsAll method to check if we have all the elements of aList2 available in aList1.
aList1.containsAll(aList2); // returns true
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Ideally, in the above mentioned scenario at step 4, (aList1.containsAll(aList2);) it should return false. As only one of the elements of aList2 is available in aList1. Lists can contain duplicate values. So, containsAll should check for all the elements even if elements are duplicate.
ACTUAL -
In the above mentioned scenario at step 4, (aList1.containsAll(aList2);) it should return false but it returns true.
---------- BEGIN SOURCE ----------
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class CodeCheck {
public static void main(String args[]) {
HashSet hs = new HashSet();
HashMap hm = new HashMap();
StringBuilder sb = new StringBuilder();
ArrayList<Integer> al1 = new ArrayList<>();
ArrayList<Integer> al2 = new ArrayList<>();
al1.add(1);
al1.add(2);
al1.add(3);
al2.add(2);
al2.add(2);
al2.add(2);
//al2.add(4);
System.out.println(al1);
System.out.println(al2);
if(al1.containsAll(al2)) {
System.out.println("contains");
}else {
System.out.println("doesnt contains");
}
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
static HashMap<Integer,Integer> checkDuplicate = new HashMap<>();
private static int containsAll(ArrayList<Integer> list1, ArrayList<Integer> list2) {
int value;
checkDuplicate.clear();
for(Integer element : list1) {
if(checkDuplicate.containsKey(element)) {
value = checkDuplicate.get(element);
value += 1;
checkDuplicate.put(element, value);
}else {
checkDuplicate.put(element, 1);
}
}
for(Integer element : list2) {
if(checkDuplicate.containsKey(element)) {
value = checkDuplicate.get(element);
value -= 1;
checkDuplicate.put(element, value);
}else {
return 0;
}
}
for(Integer element : list2) {
if(checkDuplicate.containsKey(element)) {
value = checkDuplicate.get(element);
if(value != 0)
return 0;
}else {
return 0;
}
}
return 1;
}
FREQUENCY : always
OSX/Java 8
A DESCRIPTION OF THE PROBLEM :
containsAll method is not working as expected when ArrayList containing duplicate value is used.
REGRESSION : Last worked in version 8u221
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
step 1 :- create two Array Lists.
ArrayList<Integer> aList1 = new ArrayList<>(); ArrayList<Integer> aList2 = new ArrayList<>();
Step 2 :- populate one of the ArrayList with distinct values.
aList1.add(1);aList1.add(2);aList1.add(3); // aList1 contains [1,2,3]
Step 3:- populate another ArrayList with duplicate values.
aList1.add(2);aList1.add(2);aList1.add(2); // aList1 contains [2,2,2]
Step 4:- use containsAll method to check if we have all the elements of aList2 available in aList1.
aList1.containsAll(aList2); // returns true
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Ideally, in the above mentioned scenario at step 4, (aList1.containsAll(aList2);) it should return false. As only one of the elements of aList2 is available in aList1. Lists can contain duplicate values. So, containsAll should check for all the elements even if elements are duplicate.
ACTUAL -
In the above mentioned scenario at step 4, (aList1.containsAll(aList2);) it should return false but it returns true.
---------- BEGIN SOURCE ----------
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class CodeCheck {
public static void main(String args[]) {
HashSet hs = new HashSet();
HashMap hm = new HashMap();
StringBuilder sb = new StringBuilder();
ArrayList<Integer> al1 = new ArrayList<>();
ArrayList<Integer> al2 = new ArrayList<>();
al1.add(1);
al1.add(2);
al1.add(3);
al2.add(2);
al2.add(2);
al2.add(2);
//al2.add(4);
System.out.println(al1);
System.out.println(al2);
if(al1.containsAll(al2)) {
System.out.println("contains");
}else {
System.out.println("doesnt contains");
}
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
static HashMap<Integer,Integer> checkDuplicate = new HashMap<>();
private static int containsAll(ArrayList<Integer> list1, ArrayList<Integer> list2) {
int value;
checkDuplicate.clear();
for(Integer element : list1) {
if(checkDuplicate.containsKey(element)) {
value = checkDuplicate.get(element);
value += 1;
checkDuplicate.put(element, value);
}else {
checkDuplicate.put(element, 1);
}
}
for(Integer element : list2) {
if(checkDuplicate.containsKey(element)) {
value = checkDuplicate.get(element);
value -= 1;
checkDuplicate.put(element, value);
}else {
return 0;
}
}
for(Integer element : list2) {
if(checkDuplicate.containsKey(element)) {
value = checkDuplicate.get(element);
if(value != 0)
return 0;
}else {
return 0;
}
}
return 1;
}
FREQUENCY : always