java - New ArrayList with same values -
i have arraylist:
arraylist<integer> example = new arraylist<integer>(); example.add(1); example.add(1); example.add(2); example.add(3); example.add(3);
so want make others 3 arraylists containing in 1 same values (where there 1 value arraylist have one).
is possible?
here's approach filtering out elements, implemented: using generic map (in generic class) encapsulate values.
- the key object want, , value determined follows:
- if key never existed, have list @ 1 element, same key;
- if key has existed prior, have list @ least 1 element, same key.
here's how it's laid out. instantiate type of object want split.
public class uniquesplitter<t> { public map<t, list<t>> filteroutelements(final list<?> thecandidatelist) { final map<t, list<t>> candidatemap = new hashmap<>(); for(object element : thecandidatelist) { if(candidatemap.containskey(element)) { candidatemap.get(element).add((t) element); } else { final list<t> elementlist = new arraylist<>(); elementlist.add((t) element); candidatemap.put((t)element, elementlist); } } return candidatemap; } }
Comments
Post a Comment