How to Compare two lists in java -
i have 2 lists list listone, list listtwo, , want compare both , if both same want add list item other list ol1 else add ol2.
here elementrangeindex bean class contains string values.
while comparing 2 lists needs compare each string value bean.
i have used below code 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()) { (elementrangeindex listtwodata : listtwo) { (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 add similar list items 1 list(listones), left other list(listtwos) , right other list(listthree).
thanks, arjun
if need split yourself, i'd this:
create copy of list , name
leftonly. contain elements present in list one.create copy of list 2 , name if
rightonly. contain elements present in list two.create empty list
intersectlistcontain elements present in both lists.up
leftonlymight contain many elements we'll need filter those. use iterator iterate on each element , check whether contained inrightonlywell. if remove elementleftonly,rightonly, addintersectlist.
to speed process (contains , remove on lists linear operations) might make leftonly , rightony of type linkedhashset allows faster operations not allow duplicates (using duplicates in result break whole logic anyways).
Comments
Post a Comment