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

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

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

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

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <groupId>org.hamcrest</groupId>
          <artifactId>hamcrest-core</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.hamcrest</groupId>
      <artifactId>hamcrest-library</artifactId>
      <version>1.3</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
      <version>25.1-jre</version>
    </dependency>
</dependencies>

filter by key

public class FilterMapByKeyTest {
  private Map<Integer, String> 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<Integer, String> map = new HashMap<>();
    for (Map.Entry<Integer, String> 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<Integer, String> 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<Integer, String> map = Maps.filterKeys(WEEK, r -> r <= 3);
    assertThat(map.keySet(), contains(1, 2, 3));
  }
}

filter by value

public class FilterMapByValueTest {
  private Map<Integer, String> 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<Integer, String> map = new HashMap<>();
    for (Map.Entry<Integer, String> 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<Integer, String> 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<Integer, String> map = Maps.filterValues(WEEK, r -> r.startsWith("S"));
    assertThat(map.values(), contains("Saturday","Sunday"));
  }
}

总结

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

文章标题:JavaMap通过key或者value过滤的实例代码
路径分享:https://www.cdcxhl.com/article20/poeoco.html

成都网站建设公司_创新互联,为您提供网站营销企业建站App开发商城网站定制开发

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

商城网站建设