개발/일반
java set 으로 union, intersection, difference, reverse
by 로그인시러
2016. 12. 27.
//Union
List<Integer> c = new ArrayList<Integer>(a.size() + b.size());
addNoDups(c,a);
addNoDups(c,b);
private void addNoDups(List<Integer> toAddTo,List<Integer> iterateOver) {
for(Integer num:iterateOver){
if(toAddTo.indexOf(num) == -1) {
toAddTo.add(num);
}
}
}
//intersection
List<Integer> c = new ArrayList<Integer> (a.size() > b.size() ?a.size():b.size());
c.addAll(a);
c.retainAll(b);
//difference a-b
List<Integer> c = new ArrayList<Integer> (a.size());
c.addAll(a);
c.removeAll(b);
출처 : http://stackoverflow.com/questions/3590677/how-to-do-union-intersect-difference-and-reverse-data-in-java
댓글