-
查找两个List集合中的不同元素
- package com.czp.test;
-
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- public class TestList {
-
- public static void main(String[] args) {
- List<String> list1 = new ArrayList<String>();
- List<String> list2 = new ArrayList<String>();
- for (int i = 0; i < 10000; i++) {
- list1.add("test"+i);
- list2.add("test"+i*2);
- }
-
- getDiffrent5(list1,list2);
-
-
-
- }
-
- private static List<String> getDiffrent5(List<String> list1, List<String> list2) {
- long st = System.nanoTime();
- List<String> diff = new ArrayList<String>();
- List<String> maxList = list1;
- List<String> minList = list2;
- if(list2.size()>list1.size())
- {
- maxList = list2;
- minList = list1;
- }
- Map<String,Integer> map = new HashMap<String,Integer>(maxList.size());
- for (String string : maxList) {
- map.put(string, 1);
- }
- for (String string : minList) {
- if(map.get(string)!=null)
- {
- map.put(string, 2);
- continue;
- }
- diff.add(string);
- }
- for(Map.Entry<String, Integer> entry:map.entrySet())
- {
- if(entry.getValue()==1)
- {
- diff.add(entry.getKey());
- }
- }
- System.out.println("getDiffrent5 total times "+(System.nanoTime()-st));
- return diff;
-
- }
-
相关阅读:
4.9 省选模拟赛 圆圈游戏 树形dp set优化建图
C#异步编程の-------异步编程模型(APM)
C#异步编程の----Threadpool( 线程池)
C#の----Func,Action,predicate在WPF中的应用
C#常见委托のdelegate定义,Func,Action,Predicate总结
c++のmap的遍历
C#深度学习の----深拷贝与浅拷贝
NSIS学习记录の----查找注册表某个键是否存在
WPFの操作文件浏览框几种方式
C#Url处理类
-
原文地址:https://www.cnblogs.com/JeffXia/p/7015883.html
Copyright © 2020-2023
润新知