重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

JavaMap通过key或者value过滤的实例代码

今天写根过滤的时候一下子有点愣眼,先是想到用 Java 原生的 map 循环查出来,但是觉得太 low, 后面思考了一下可以用 Java8 的 Lambda,写完了,又发现 Google Guava 有现成的方法,这里一一列出来,供参考使用。

创新新互联,凭借十年的网站制作、网站设计经验,本着真心·诚心服务的企业理念服务于成都中小企业设计网站有1000+案例。做网站建设,选创新互联公司

首先提示,如果照搬我的代码的话别忘了引这些依赖


    
      junit
      junit
      4.12
      test
      
        
          org.hamcrest
          hamcrest-core
        
      
    
    
      org.hamcrest
      hamcrest-library
      1.3
      test
    
    
      com.google.guava
      guava
      25.1-jre
    

filter by key

public class FilterMapByKeyTest {
  private Map WEEK = new HashMap<>();
  @Before
  public void setUp () {
    WEEK.put(1, "Monday");
    WEEK.put(2, "Tuesday");
    WEEK.put(3, "Wednesday");
    WEEK.put(4, "Thursday");
    WEEK.put(5, "Friday");
    WEEK.put(6, "Saturday");
    WEEK.put(7, "Sunday");
  }
  /**
   * Java 8之前的版本
   */
  @Test
  public void filterMapByKey () {
    Map map = new HashMap<>();
    for (Map.Entry entry : WEEK.entrySet()) {
      if (entry.getKey() <= 3) {
        map.put(entry.getKey(), entry.getValue());
      }
    }
    assertThat(map.keySet(), contains(1, 2, 3));
  }
  /**
   * Java 8 Lambda
   */
  @Test
  public void filterMapByKeyJava8Lambda () {
    Map map = WEEK.entrySet().stream().filter(r -> r.getKey() <= 3)
            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    assertThat(map.keySet(), contains(1, 2, 3));
  }
  /**
   * Google Guava
   */
  @Test
  public void filterMapByKeyGuava () {
    Map map = Maps.filterKeys(WEEK, r -> r <= 3);
    assertThat(map.keySet(), contains(1, 2, 3));
  }
}

filter by value

public class FilterMapByValueTest {
  private Map WEEK = new HashMap<>();
  @Before
  public void setUp () {
    WEEK.put(1, "Monday");
    WEEK.put(2, "Tuesday");
    WEEK.put(3, "Wednesday");
    WEEK.put(4, "Thursday");
    WEEK.put(5, "Friday");
    WEEK.put(6, "Saturday");
    WEEK.put(7, "Sunday");
  }
  /**
   * Java 8之前的版本
   */
  @Test
  public void filterMapByValue () {
    Map map = new HashMap<>();
    for (Map.Entry entry : WEEK.entrySet()) {
      if (entry.getValue().startsWith("S")) {
        map.put(entry.getKey(), entry.getValue());
      }
    }
    assertThat(map.values(), contains("Saturday","Sunday"));
  }
  /**
   * Java 8 Lambda
   */
  @Test
  public void filterMapByValueJava8Lambda () {
    Map map = WEEK.entrySet().stream().filter(r -> r.getValue().startsWith("S"))
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    assertThat(map.values(), contains("Saturday","Sunday"));
  }
  /**
   * Google Guava
   */
  @Test
  public void filterMapByValueGuava () {
    Map map = Maps.filterValues(WEEK, r -> r.startsWith("S"));
    assertThat(map.values(), contains("Saturday","Sunday"));
  }
}

总结

以上所述是小编给大家介绍的Java Map 通过 key 或者 value 过滤的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对创新互联网站的支持!


当前文章:JavaMap通过key或者value过滤的实例代码
分享URL:http://cqcxhl.com/article/poeoco.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP