zoukankan      html  css  js  c++  java
  • Lists、Sets、Maps和Collections2的使用

    1、Lists

    Java代码  收藏代码
    1. //Lists  
    2. System.out.println("### Lists ###");  
    3. ArrayList<String> arrayList = Lists.newArrayList();  
    4. arrayList.add("A");  
    5. arrayList.add("B");  
    6. arrayList.add("C");  
    7. System.out.println(arrayList);  
    8.   
    9. LinkedList<String> linkedList = Lists.newLinkedList();  
    10. linkedList.add("A");  
    11. linkedList.add("B");  
    12. linkedList.add("C");  
    13. System.out.println(linkedList);  
    14.   
    15. System.out.println(Lists.reverse(arrayList));   //翻转集合元素的顺序  
    16. System.out.println(Lists.reverse(linkedList));  
    17.   
    18. System.out.println(Lists.partition(arrayList, 2)); //一个集合内再分成n个子集合  

    2、Sets

    Java代码  收藏代码
    1. //Sets  
    2. System.out.println("### Sets ###");  
    3. HashSet<String> set1 = Sets.newHashSet();  
    4. set1.add("A");  
    5. set1.add("B");  
    6. set1.add("C");  
    7.   
    8. LinkedHashSet<String> set2 = Sets.newLinkedHashSet();  
    9. set2.add("B");  
    10. set2.add("C");  
    11. set2.add("D");  
    12. set2.add("E");  
    13.   
    14. //Sets.newTreeSet();  
    15. //Sets.newConcurrentHashSet();  
    16.   
    17. System.out.println(Sets.union(set1, set2)); //返回两个集合的并集  >> [A, B, C, D, E]  
    18. System.out.println(Sets.difference(set1, set2)); //返回set1中不存在于set2的元素集合  >> [A]  
    19. System.out.println(Sets.intersection(set1, set2)); //返回两个集合的交集  >> [B, C]  
    20.   
    21. Set<String> resultSet = Sets.filter(set2, new Predicate<String>() {  
    22.     @Override  
    23.     public boolean apply(String value) {  
    24.         return !"D".equals(value); //过滤掉值为D的元素  >> [B, C, E]  
    25.     }  
    26. });  
    27. System.out.println(resultSet);  

    3、Maps

    Java代码  收藏代码
    1. //Maps  
    2. System.out.println("### Maps ###");  
    3. Maps.newHashMap();  
    4. Maps.newLinkedHashMap();  
    5. Maps.newTreeMap();  
    6. Maps.newConcurrentMap();  

    4、Collections2

    Java代码  收藏代码
    1. //Collections2  
    2. System.out.println("### Collections2 ###");  
    3. Collection<String> linkedList2 = Collections2.filter(linkedList, new Predicate<String>() {  
    4.     @Override  
    5.     public boolean apply(String value) {  
    6.         return !"B".equals(value); //过滤器:过滤掉值为B的元素  >> [A, C]  
    7.     }  
    8. });  
    9. System.out.println(linkedList2);  
    10.   
    11. Collection<String> linkedList3 = Collections2.transform(linkedList, new Function<String, String>() {  
    12.     @Override  
    13.     public String apply(String value) {  
    14.         return value + "_"; //转换器:元素值做特殊处理后返回  >> [A_, B_, C_]  
    15.     }  
    16. });  
    17. System.out.println(linkedList3);  
  • 相关阅读:
    There is an overlap in the region chain修复
    There is an overlap in the region chain
    region xx not deployed on any region server
    python 中的re模块,正则表达式
    TCP粘包问题解析与解决
    yield from
    Git push提交时报错Permission denied(publickey)...Please make sure you have the correct access rights and the repository exists.
    mysql 中Varchar 与char的区别
    Mysql 字符集及排序规则
    请实现一个装饰器,限制该函数被调用的频率,如10秒一次
  • 原文地址:https://www.cnblogs.com/xiaohu1218/p/9238181.html
Copyright © 2011-2022 走看看