Java Map 通过 key 或者者 value 过滤

作者 : 开心源码 本文共2588个字,预计阅读时间需要7分钟 发布时间: 2022-05-11 共50人阅读

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

假如觉得内容还不错,能关注一下我哦
期待与你相遇,一同成长前行!

说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » Java Map 通过 key 或者者 value 过滤

发表回复