栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

有什么方法可以像“(k,v)”一样流式传输地图,而不是使用(entry)?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

有什么方法可以像“(k,v)”一样流式传输地图,而不是使用(entry)?

既然这是一个重复的问题,我将提出一个完整的解决方案。这种

PairStream
类型默认情况下是普通对象周围的简单包装器
Stream
(尽管可以是
interface
,也可以选择)。

它着重于提供方便的中间体操作和不能由调用的方法之一可以容易地进行这些终端的操作

keys()
values()
entries()
返回到常规的单元素
Stream
和连锁的终端操作。因此,例如,这
PairStream.from(map).filterValue(predicate).keys().findAny()
是获取映射值与谓词匹配的键的直接方法。
filterValue
是一种方便的中间操作,
keys
转回到
Stream
允许对键进行任意终端操作的普通操作。

一些例子

    Map<String,Integer> m=new HashMap<>();    m.put("foo", 5);    m.put("bar", 7);    m.put("baz", 42);    // {b=49, f=5}    Map<Character,Integer> m2=PairStream.from(m)      .mapKey(s->s.charAt(0))      .toMap(Integer::sum);    // foo bar    String str=PairStream.from(m)      .filterValue(i->i<30)      .keys().sorted(Comparator.reverseOrder())      .collect(Collectors.joining(" "));    Map<String,Integer> map=new HashMap<>();    map.put("muhv~", 26);    map.put("kfool", 3);    String str = PairStream.from(map)      .sortedByValue(Comparator.naturalOrder())      .flatMapToInt((s,i)->s.prePoints().map(c->c^i))      .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)      .toString();

这是完整的类(我尚未测试所有操作,但是大多数操作都是简单明了的):

import java.util.*;import java.util.function.*;import java.util.stream.*;public interface PairStream<K,V> {    static <K,V> PairStream<K,V> from(Map<K,V> map) {        return from(map.entrySet().stream());    }    static <K,V> PairStream<K,V> from(Stream<Map.Entry<K,V>> s) {        return ()->s;    }    static <K,V> PairStream<K,V> from(Stream<K> s, Function<? super K, ? extends V> f) {        return ()->s.map(k->new AbstractMap.SimpleImmutableEntry<>(k, f.apply(k)));    }    default PairStream<K,V> distinct() {        return from(entries().distinct());    }    default PairStream<K,V> peek(BiConsumer<? super K, ? super V> action) {        return from(entries().peek(e->action.accept(e.getKey(), e.getValue())));    }    default PairStream<K,V> skip(long n) {        return from(entries().skip(n));    }    default PairStream<K,V> limit(long maxSize) {        return from(entries().limit(maxSize));    }    default PairStream<K,V> filterKey(Predicate<? super K> mapper) {        return from(entries().filter(e->mapper.test(e.getKey())));    }    default PairStream<K,V> filterValue(Predicate<? super V> mapper) {        return from(entries().filter(e->mapper.test(e.getValue())));    }    default PairStream<K,V> filter(BiPredicate<? super K, ? super V> mapper) {        return from(entries().filter(e->mapper.test(e.getKey(), e.getValue())));    }    default <R> PairStream<R,V> mapKey(Function<? super K,? extends R> mapper) {        return from(entries().map(e->new AbstractMap.SimpleImmutableEntry<>( mapper.apply(e.getKey()), e.getValue()        )));    }    default <R> PairStream<K,R> mapValue(Function<? super V,? extends R> mapper) {        return from(entries().map(e->new AbstractMap.SimpleImmutableEntry<>( e.getKey(), mapper.apply(e.getValue())        )));    }    default <R> Stream<R> map(BiFunction<? super K, ? super V,? extends R> mapper) {        return entries().map(e->mapper.apply(e.getKey(), e.getValue()));    }    default DoubleStream mapToDouble(ToDoubleBiFunction<? super K, ? super V> mapper) {        return entries().mapToDouble(e->mapper.applyAsDouble(e.getKey(), e.getValue()));    }    default IntStream mapToInt(ToIntBiFunction<? super K, ? super V> mapper) {        return entries().mapToInt(e->mapper.applyAsInt(e.getKey(), e.getValue()));    }    default LongStream mapToLong(ToLongBiFunction<? super K, ? super V> mapper) {        return entries().mapToLong(e->mapper.applyAsLong(e.getKey(), e.getValue()));    }    default <RK,RV> PairStream<RK,RV> flatMap( BiFunction<? super K, ? super V,? extends PairStream<RK,RV>> mapper) {        return from(entries().flatMap( e->mapper.apply(e.getKey(), e.getValue()).entries()));    }    default <R> Stream<R> flatMapToObj( BiFunction<? super K, ? super V,? extends Stream<R>> mapper) {        return entries().flatMap(e->mapper.apply(e.getKey(), e.getValue()));    }    default DoubleStream flatMapToDouble( BiFunction<? super K, ? super V,? extends DoubleStream> mapper) {        return entries().flatMapToDouble(e->mapper.apply(e.getKey(), e.getValue()));    }    default IntStream flatMapToInt( BiFunction<? super K, ? super V,? extends IntStream> mapper) {        return entries().flatMapToInt(e->mapper.apply(e.getKey(), e.getValue()));    }    default LongStream flatMapToLong( BiFunction<? super K, ? super V,? extends LongStream> mapper) {        return entries().flatMapToLong(e->mapper.apply(e.getKey(), e.getValue()));    }    default PairStream<K,V> sortedByKey(Comparator<? super K> comparator) {        return from(entries().sorted(Map.Entry.comparingByKey(comparator)));    }    default PairStream<K,V> sortedByValue(Comparator<? super V> comparator) {        return from(entries().sorted(Map.Entry.comparingByValue(comparator)));    }    default boolean allMatch(BiPredicate<? super K,? super V> predicate) {        return entries().allMatch(e->predicate.test(e.getKey(), e.getValue()));    }    default boolean anyMatch(BiPredicate<? super K,? super V> predicate) {        return entries().anyMatch(e->predicate.test(e.getKey(), e.getValue()));    }    default boolean noneMatch(BiPredicate<? super K,? super V> predicate) {        return entries().noneMatch(e->predicate.test(e.getKey(), e.getValue()));    }    default long count() {        return entries().count();    }    Stream<Map.Entry<K,V>> entries();    default Stream<K> keys() {        return entries().map(Map.Entry::getKey);    }    default Stream<V> values() {        return entries().map(Map.Entry::getValue);    }    default Optional<Map.Entry<K,V>> maxByKey(Comparator<? super K> comparator) {        return entries().max(Map.Entry.comparingByKey(comparator));    }    default Optional<Map.Entry<K,V>> maxByValue(Comparator<? super V> comparator) {        return entries().max(Map.Entry.comparingByValue(comparator));    }    default Optional<Map.Entry<K,V>> minByKey(Comparator<? super K> comparator) {        return entries().min(Map.Entry.comparingByKey(comparator));    }    default Optional<Map.Entry<K,V>> minByValue(Comparator<? super V> comparator) {        return entries().min(Map.Entry.comparingByValue(comparator));    }    default void forEach(BiConsumer<? super K, ? super V> action) {        entries().forEach(e->action.accept(e.getKey(), e.getValue()));    }    default void forEachOrdered(BiConsumer<? super K, ? super V> action) {        entries().forEachOrdered(e->action.accept(e.getKey(), e.getValue()));    }    default Map<K,V> toMap() {        return entries().collect( Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));    }    default Map<K,V> toMap(BinaryOperator<V> valAccum) {        return entries().collect( Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, valAccum));    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/596011.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号