java的LinkedHashMap如何使用Stream过滤

539 阅读2分钟

1. 背景

很多时候我们需要用到有序的Map,需要使用LinkedHashMap。如果我们需要对LinkedHashMap做一个过滤呢?是不是可以使用Stream的Filter?看起来很简单的一个问题,伴随展开却能反映出很多。我先给出结论,然后使用一个LinkedHashMap过滤值中包含1的小需求来展开不同的思考。

先给出结论:

  • 不要书写团队人员都觉得晦涩的代码
  • 不同业务代码中共用的业务无关的技术重复代码应该抽取
  • Collectors.toMap的默认收集结果并不是LinkedHashMap

2. 错误写法-直接使用Stream的Filter

正常的Stream加Filter写法如下:使用普通的MapStream来过滤,然后使用Collectors.toMap,结果是一个无序的Map。

        Map<String, String> map = new LinkedHashMap<>();
        map.put("a", "a1");
        map.put("aa", "a2");
        map.put("b", "b1");
        map.put("bb", "b2");
        System.out.println(map);

        //筛选出来包含1的,并保持有序
        map = map.entrySet().stream()
                .filter(e -> e.getValue().contains("1"))
                .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
        System.out.println(map);

我们想要的是LinkedHashMap,是否可以强制类型转换为LinkedHashMap呢?

LinkedHashMap<String, String> map = new LinkedHashMap<>();
map.put("a", "a1");
map.put("aa", "a2");
map.put("b", "b1");
map.put("bb", "b2");
System.out.println(map);
map = (LinkedHashMap) map.entrySet().stream()
        .filter(e -> e.getValue().contains("1"))
        .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
System.out.println(map);

执行结果会报错:

{a=a1, aa=a2, b=b1, bb=b2}
Exception in thread "main" java.lang.ClassCastException: class java.util.HashMap cannot be cast to class java.util.LinkedHashMap (java.util.HashMap and java.util.LinkedHashMap are in module java.base of loader 'bootstrap')
	at fly.sample.collection.LinkedHashMapFilterTest.test2(LinkedHashMapFilterTest.java:40)
	at fly.sample.collection.LinkedHashMapFilterTest.main(LinkedHashMapFilterTest.java:10)

原因是因为toMap的结果不是原来的类型LinkedHashMap,而是新建立的HashMap,看下toMap的源码

    public static <T, K, U>
    Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper,
                                    Function<? super T, ? extends U> valueMapper) {
        return new CollectorImpl<>(HashMap::new,
                                   uniqKeysMapAccumulator(keyMapper, valueMapper),
                                   uniqKeysMapMerger(),
                                   CH_ID);
    }

3. Sream的正确写法

LinkedHashMap<String, String> map = new LinkedHashMap<>();
map.put("a", "a1");
map.put("aa", "a2");
map.put("b", "b1");
map.put("bb", "b2");
System.out.println(map)
map = map.entrySet()
        .stream().filter(e -> e.getValue().contains("1")).collect(Collectors.toMap(e -> e.getKey(), v -> v.getValue(),
                (oldValue, newValue) -> oldValue, LinkedHashMap::new));
System.out.println("4" + map);

可以正常执行,输出依然有序

{a=a1, aa=a2, b=b1, bb=b2}
4{a=a1, b=b1}

正确的写法易读性很差,很多人都会抱怨干嘛不用原始写法。所以个人建议自己抽取个可读性更好的方法,这样不仅仅可读性好,不同业务之间过滤LinkedHashMap可以共用代码。

4. 通用方法抽取

针对上面晦涩难以读懂的写法,我们抽取一个公共方法,不仅仅取得了易读的好处,而且业务逻辑中不用再耦合这些业务无关的技术代码:

public static <K, V> LinkedHashMap<K, V> filterLinkedHashMap(LinkedHashMap<K, V> linkedHashMap, Predicate<Map.Entry<K, V>> predicate) {
    LinkedHashMap resultMap = new LinkedHashMap<>();
    for (Map.Entry entry : linkedHashMap.entrySet()) {
        if (predicate.test(entry)) {
            resultMap.put(entry.getKey(), entry.getValue());
        }
    }
    return resultMap;
}

程序中只要如下使用filterLinkedHashMap方法就可以,易读性就可以得到极大提高。

LinkedHashMap<String, String> map = new LinkedHashMap<>();
map.put("a", "a1");
map.put("aa", "a2");
map.put("b", "b1");
map.put("bb", "b2");
System.out.println(map)
map = filterLinkedHashMap(map, e -> e.getValue().contains("1"));
System.out.println(map);

当然,你也可以使用3部分中的晦涩一点的写法实现filterLinkedHashMap方法,因为对于方法使用者是不可见的。