服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - Java Map 通过 key 或者 value 过滤的实例代码

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

2021-05-12 14:17志哥 Java教程

这篇文章主要介绍了Java Map 通过 key 或者 value 过滤的实例代码,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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 过滤的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:https://segmentfault.com/a/1190000015421902

延伸 · 阅读

精彩推荐