• (java/javascript) list 交集 并集 差集 去重复并集


    java list 交集 并集 差集 去重复并集
    package com;
    
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    
    public class Test {
    
     public static void main(String[] args) {
      List list1 =new ArrayList();
      list1.add("1111");
      list1.add("2222");
      list1.add("3333");
      
      List list2 =new ArrayList();
      list2.add("3333");
      list2.add("4444");
      list2.add("5555");
      
      //并集
      //list1.addAll(list2);
      //交集
      //list1.retainAll(list2);
      //差集
      //list1.removeAll(list2);
      //无重复并集
        list2.removeAll(list1);
        list1.addAll(list2);
      
      Iterator<String> it=list1.iterator();
      while (it.hasNext()) {
       System.out.println(it.next());
       
      }
      
      //System.out.println("-----------------------------------
    ");
      //printStr(list1);
      
     }
     
     public static void printStr(List list1){
      for (int i = 0; i < list1.size(); i++) {
       System.out.println(list1.get(i));
      }
     }
    }

    list去重:

     1 去除List中重复元素
     2 转载自:http://blog.csdn.NET/mwq384807683/article/details/8088706
     3 
     4 用Set ,倘若list里边的元素不是基本数据类型而是对象,那么请覆写Object的boolean equals(Object obj) 和int hashCode()方法.  
     5   
     6 return new ArrayList(new HashSet(list));  
     7   
     8 方法一:循环元素删除   
     9 // 删除ArrayList中重复元素   
    10 public static void removeDuplicate(List list) {  
    11    for ( int i = 0 ; i < list.size() - 1 ; i ++ ) {  
    12     for ( int j = list.size() - 1 ; j > i; j -- ) {  
    13       if (list.get(j).equals(list.get(i))) {  
    14         list.remove(j);  
    15       }   
    16      }   
    17    }   
    18    System.out.println(list);  
    19 }   
    20 方法二:通过HashSet剔除  
    21 // 删除ArrayList中重复元素   
    22 public static void removeDuplicate(List list) {  
    23      HashSet h = new HashSet(list);  
    24      list.clear();  
    25      list.addAll(h);  
    26      System.out.println(list);  
    27 }   
    28 方法三: 删除ArrayList中重复元素,保持顺序  
    29 // 删除ArrayList中重复元素,保持顺序   
    30 public static void removeDuplicateWithOrder(List list) {  
    31     Set set = new HashSet();  
    32      List newList = new ArrayList();  
    33    for (Iterator iter = list.iterator(); iter.hasNext();) {  
    34          Object element = iter.next();  
    35          if (set.add(element))  
    36             newList.add(element);  
    37       }   
    38      list.clear();  
    39      list.addAll(newList);  
    40     System.out.println( " remove duplicate " + list);  
    41 }  
    42   
    43   
    44 如果用HashSet的话,如果是对象,则要将对象实现equals和hashCode方法

    javascript:

     1 1.  取交集 (A和B都有)
     2 
     3 List A : { 1 , 2 , 3 , 5 , 9 }
     4 List B : { 4 , 3 , 9 }
     5 var intersectedList = list1.Intersect(list2);
     6 结果 : { 3 , 9 }
     7 判断A和B是否有交集 bool isIntersected = list1.Intersect(list2).Count() > 0
     8 
     9 2. 取差集 (A有,B沒有)
    10 List A : { 1 , 2 , 3 , 5 , 9 }
    11 List B : { 4 , 3 , 9 }
    12  var expectedList = list1.Except(list2);
    13 结果 : { 1 , 2 , 5 }
    14 判断A和B是否有差集 bool isExpected = list1.Expect(list2).Count() > 0
    15 
    16  3.  取联集 (包含A和B)
    17 
    18 var unionList =list1.Union(list2);
  • 相关阅读:
    MVC+EasyUI 菜单导航的实现
    MVC4 +EasyUI 使用TreeGrid 方法
    MYSQL 内存报错 Use 'mysqld --thread_stack=#' to specify a bigger stack.
    System.Data.EntityState”在未被引用的程序集中定义
    android 学习第一天 了解事件机制,页面跳转等常用操作
    EasyUI TreeGrid DataTable转换数据实现案例
    王慧文清华大学的演讲的观点摘要
    《自由选择》
    优秀的工程师
    【mark】Windows Exploitation, post exploitation sites for reference
  • 原文地址:https://www.cnblogs.com/liuxiaoming123/p/7479323.html
Copyright © 2020-2023  润新知