I have two lists List listOne, List listTwo, and i want to compare both and if both are same i want to add the list item into some other list ol1 else add to ol2.
Here the ElementRangeIndex is a bean class which contains some string values.
While comparing the two lists it needs to compare each string value from the bean.
I have used the below code to but contains adding duplicate values since both lists have different objects.
public static Map<Integer, List<ElementRangeIndex>> compareLists(List<ElementRangeIndex> listOne, List<ElementRangeIndex> listTwo) {
boolean indicator = false;
List<ElementRangeIndex> listOnes = new ArrayList<ElementRangeIndex>();
List<ElementRangeIndex> listTwos = new ArrayList<ElementRangeIndex>();
List<ElementRangeIndex> listThree = new ArrayList<ElementRangeIndex>();
Map<Integer, List<ElementRangeIndex>> map = new HashMap<Integer, List<ElementRangeIndex>>();
if (listOne!= null && listTwo!=null && listOne.size() == listTwo.size()) {
for (ElementRangeIndex listTwoData : listTwo) {
for (ElementRangeIndex listOneData : listOne) {
/* if (listOneData.getNamespaceUri().equals(listTwoData.getNamespaceUri())
&& listOneData.getCollation().equals(listTwoData.getCollation())
&& listOneData.getScalarType().equals(listTwoData.getScalarType())
&& listOneData.getLocalname().equals(listTwoData.getLocalname())) {*/
if ((listOneData.getNamespaceUri().hashCode()== listTwoData.getNamespaceUri().hashCode())
&& (listOneData.getCollation().hashCode() == listTwoData.getCollation().hashCode())
&& (listOneData.getScalarType().hashCode() == listTwoData.getScalarType().hashCode())
&& (listOneData.getLocalname().hashCode() == listTwoData.getLocalname().hashCode())) {
listOnes.add(listOneData);
if(listTwos.contains(listOneData))
listTwos.remove(listOneData);
if(listTwos.contains(listTwoData))
listTwos.remove(listTwoData);
if(listThree.contains(listOneData))
listThree.remove(listOneData);
if(listThree.contains(listTwoData))
listThree.remove(listTwoData);
}else{
if(!listOnes.contains(listOneData))
if(!listTwos.contains(listOneData))
listTwos.add(listOneData);
if(!listOnes.contains(listTwoData))
if(!listThree.contains(listTwoData))
listThree.add(listTwoData);
}
}
}
map.put(1,listOnes);
map.put(2, listTwos);
map.put(3, listThree);
}
return map;
}
My aim is to add similar list items into one list(listOnes), left only to other list(listTwos) and right to other list(listThree).
Thanks, Arjun