欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-13 23:39:22
今天写根过滤的时候一下子有点愣眼,先是想到用 java 原生的 map 循环查出来,但是觉得太 low, 后面思考了一下可以用 java8 的 lambda,写完了,又发现...

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

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

<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 过滤的实例代码,希望对大家有所帮助