继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Java Map 通过 key 或者 value 过滤

代码写到手抽筋
关注TA
已关注
手记 294
粉丝 26
获赞 113

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

@Beforepublic 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之前的版本 */@Testpublic 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 */@Testpublic 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 */@Testpublic 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<>();

@Beforepublic 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之前的版本 */@Testpublic 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 */@Testpublic 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 */@Testpublic void filterMapByValueGuava () {    Map<Integer, String> map = Maps.filterValues(WEEK, r -> r.startsWith("S"));    assertThat(map.values(), contains("Saturday","Sunday"));}

}
如果觉得内容还不错,可以关注一下我哦

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP