-
查找两个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;
-
- }
-
相关阅读:
Codeforces441B_Valera and Fruits(暴力)
《Python核心编程》第五章:数字
app-framework学习--Scroller
饿了么移动APP的架构演进
移动支付--银联,支付宝,微信(android)
android推送,极光推送
Flume 1.7 源代码分析(四)从Source写数据到Channel
<html>
软件系统演示脚本实践(草稿)
mmu介绍
-
原文地址:https://www.cnblogs.com/JeffXia/p/7015883.html
Copyright © 2020-2023
润新知