服务器之家

服务器之家 > 正文

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

时间:2021-05-12 14:17     来源/作者:志哥

今天写根过滤的时候一下子有点愣眼,先是想到用 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

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部