zoukankan      html  css  js  c++  java
  • 【java】集合操作中的retainAll的坑

    JDK1.8

    首先,set1中有值, set2中无值

    import com.alibaba.fastjson.JSON;
    import com.google.common.collect.Sets;
    import java.util.*;
    
    
    
    public class ListTest {
    
        public static void main(String[] args) throws Exception {
    
            Set<Long> set1 = new HashSet<>();
            set1.add(1L);
            set1.add(2L);
    
            Set<Long> set2 = new HashSet<>();
    
    
            Sets.SetView<Long> intersection = Sets.intersection(set1, set2);
            System.out.println(JSON.toJSONString(intersection));
    
            System.out.println(JSON.toJSONString(set1));
            System.out.println(JSON.toJSONString(set2));
    
    
            set1.retainAll(set2);
            System.out.println(JSON.toJSONString(set1));
            System.out.println(JSON.toJSONString(set2));
    
        }
    }

    结果:

    再来一次,set2中放值

    public static void main(String[] args) throws Exception {
    
            Set<Long> set1 = new HashSet<>();
            set1.add(1L);
            set1.add(2L);
    
            Set<Long> set2 = new HashSet<>();
            set2.add(1L);
    
            Sets.SetView<Long> intersection = Sets.intersection(set1, set2);
            System.out.println(JSON.toJSONString(intersection));
    
            System.out.println(JSON.toJSONString(set1));
            System.out.println(JSON.toJSONString(set2));
    
    
            set1.retainAll(set2);
            System.out.println(JSON.toJSONString(set1));
            System.out.println(JSON.toJSONString(set2));
    
        }

    结果如下

  • 相关阅读:
    使用pull解析XML文件
    使用Pull解析器生成XML文件
    Android下文件访问的权限
    Android之SharedPreference存储数据
    Android之外部存储(SD卡)
    Android的内部存储
    Android数据存储的方式
    点击事件的四种写法
    Context
    EclipseADT编写单元测试代码的步骤
  • 原文地址:https://www.cnblogs.com/sxdcgaq8080/p/14861965.html
Copyright © 2011-2022 走看看