java - Merging two lists without duplicates -
i trying merge 2 lists while removing duplicate values inside of them. when doing this, adds every station , assumes mapvalue list not contain of stations though (im ending lot of duplicates). doing wrong?
map<string, list<stations>> overview = new treemap<string, list<stations>>(); for(trains train: trainoverview){ list<stations> mapvalue = overview.get(train.gettrainnumber()); //merge lists if key exists, , replace old value merged list if(overview.containskey(train.gettrainnumber())){ for(stations station: train.getstations()){ if(!mapvalue.contains(station)) mapvalue.add(station); } overview.put(train.gettrainnumber(), mapvalue); } //if no key exists, create new entry else{ overview.put(train.gettrainnumber(), train.getstations()); } }
uniqueness of elements within collection can achieved using set implementation. should use it. first iterate on element of first list , add them set. repeat same procedure second list. it's done.
Comments
Post a Comment