import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TesList {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("123");
list.add("456");
list.add("789");
List<String> list2 = new ArrayList<String>();
list2.add("123");
list2.add("456");
list2.add("789");
list2.add("xxxx");
list2.add("yyyy");
List<String>list3= getDiffrent(list, list2);
for (String string : list3) {
System.out.println(string);
}
}
private static List<String> getDiffrent(List<String> list1,
List<String> list2) {
long st = System.nanoTime();
Map<String, Integer> map = new HashMap<String, Integer>(list1.size()
+ list2.size());
List<String> diff = new ArrayList<String>();
for (String string : list1) {
map.put(string, 1);
}
for (String string : list2) {
Integer cc = map.get(string);
if (cc != null)
{
map.put(string, ++cc);
continue;
}
map.put(string, 1);
}
for (Map.Entry<String, Integer> entry : map.entrySet())
{
if (entry.getValue() == 1)
{
diff.add(entry.getKey());
}
}
System.out.println("getDiffrent total times "
+ (System.nanoTime() - st));
return diff;
}
}
转载于:https://my.oschina.net/bluesroot/blog/222471