• Guava Maps 用法示例


    Guava Maps 用法

    不定期更新

    1. 初始化

       @Test
       public void test_init(){
       	Map<String,String> map = Maps.newHashMap();
       }
      
    2. 将List转为Map,这个特性主要针对的场景是有一组对象,它们在某个属性上分别有独一无二的值,而我们希望能够按照这个属性值查找对象

       List<Person> personList = Lists.newArrayList();
       @Before
       public void before(){
           for(int i=0;i<10;i++){
               String temp = String.valueOf(i);
               Person p = new Person(Long.valueOf(i),temp,temp);
               personList.add(p);
           }
       }
       @Test
       public void test_list2map(){
           ImmutableMap<Long,Person> map = Maps.uniqueIndex(personList, new Function<Person, Long>() {
               @Override
               public Long apply(Person input) {
                   return input.getId();
               }
           });
           System.out.println(map);
       }
      

      output:

       {0=Person{id=0, name='0', value='0'}, 1=Person{id=1, name='1', value='1'}, 2=Person{id=2, name='2', value='2'}, 3=Person{id=3, name='3', value='3'}, 4=Person{id=4, name='4', value='4'}, 5=Person{id=5, name='5', value='5'}, 6=Person{id=6, name='6', value='6'}, 7=Person{id=7, name='7', value='7'}, 8=Person{id=8, name='8', value='8'}, 9=Person{id=9, name='9', value='9'}}
      
    3. 将Properties转化为Map

       @Test
       public void test_properties2map(){
           Properties prop = new Properties();
           prop.put("a","a1");
           prop.put("b","b1");
           ImmutableMap<String,String> map = Maps.fromProperties(prop);
           System.out.println(map);
       }
      
    4. 根据key来过滤map

       @Test
       public void test_filter_by_key(){
           Map<String,String> map = Maps.newHashMap();
           map.put("a1","b1");
           map.put("c1","d1");
           map.put("c2","d2");
           map.put("a2","b2");
           Map<String,String> result = Maps.filterKeys(map, new Predicate<String>() {
               @Override
               public boolean apply(String input) {
                   return input.contains("1");
               }
           });
           System.out.println(result);
       }
      

    如果和java8的lambda一起使用,就会更加简洁

    	@Test
        public void test_filter_by_key(){
            Map<String,String> map = Maps.newHashMap();
            map.put("a1","b1");
            map.put("c1","d1");
            map.put("c2","d2");
            map.put("a2","b2");
    
            Map<String,String> result = Maps.filterKeys(map,a->a.contains("1"));
            System.out.println(result);
        }
    
    1. 根据value来过滤map

       @Test
       public void test_filter_by_value(){
           Map<String,String> map = Maps.newHashMap();
           map.put("a1","b1");
           map.put("c1","d1");
           map.put("c2","d2");
           map.put("a2","b2");
           System.out.println(Maps.filterValues(map,a->a.contains("1")));
       }
      
    2. 根据key+value来过滤,其实就是Entry的方式

       @Test
       public void test_filter_by_entry(){
           Map<String,String> map = Maps.newHashMap();
           map.put("a1","b1");
           map.put("c1","d1");
           map.put("c2","d2");
           map.put("a2","b2");
           System.out.println(Maps.filterEntries(map,a->a.getKey().contains("1") && a.getValue().contains("b")));
       }
  • 相关阅读:
    c11---位运算相关
    c10---多文件开发
    C9---include,编译
    c8---递归
    c7---函数
    c6----函数的声明和实现
    c5
    Arm 环境上面libgdiplus的简单安装配置
    批量查找可执行文件目录的简单方法
    Android常用工具类
  • 原文地址:https://www.cnblogs.com/kakaxisir/p/7248421.html
Copyright © 2020-2023  润新知