java找出2个集合相同和不同的元素

it2023-05-31  68

找到2个集合中相同和不同的元素

public void getCommonElement(){ //集合一 List<String> _first=new ArrayList<String>(); _first.add("jim"); _first.add("tom"); _first.add("jack"); //集合二 List<String> _second=new ArrayList<String>(); _second.add("jack"); _second.add("happy"); _second.add("sun"); _second.add("good"); Collection exists = new ArrayList<String>(_second); Collection notexists = new ArrayList<String>(_second); exists.removeAll(_first); System.out.println("_second中不存在于_set中的:"+exists); notexists.removeAll(exists); System.out.println("_second中存在于_set中的:"+notexists); }

结果: _second中不存在于_set中的元素:[happy, sun, good] _second中存在于_set中的元素:[jack]

最新回复(0)