java8 简化到映射的转换

我有这样的类结构:


public class A {

    private List<B> bs;

...//getters

}


public class C {

    private Long id;

...//getters

}


public class B {


    private Long idOfC;

...//more stuff

}

B::getIdOfC matchs C::getId


在更好的设计中,B将只包含对C的引用,而不是它的id(我无法更改它),所以这就是为什么现在我需要创建一个映射,所以我的方法签名看起来像这样


public Map<A, List<C>> convert(Collection<A> collection)

在这个转换方法中,有一个


List<C> getCsByIds(List<Long> id) 

后来用于将其与B.idOfC匹配,但是应该只有一次对此方法的调用,因为它非常昂贵。


所以如果我这样去:


 List<B> bs = Arrays.asList(new B(10L), new B(11L)); //10L and 11L are the values of idOfC

   List<A> as = Arrays.asList(bs);

   //And assuming getCsByIds returns Arrays.asList(new C(10L), new C(11L), new C(12L));

然后


    Map<A, List<C>> map = convert(as);

    map.values().get(0) 

返回类似的东西Arrays.asList(new C(10L), new C(11L))


在我看来,这样做的方法非常庞大:


    public Map<A, List<C>> convert(Collection<A> as) {

    List<Long> cIds = as.stream()

            .flatMap(a -> a.getBs().stream())

            .map(B::getCId)

            .collect(Collectors.toList());


    //single call to gsCsByIds

    Map<Long, C> csMap = getCsByIds(cIds)

            .stream()

            .collect(Collectors.toMap(C::getId, Function.identity()));


    //a whole new map is created by iterating over the list called "as"

    Map<A, List<C>> csByAs = new HashMap<>();

    if (!csMap.isEmpty()) {

        for (A a : as) {

            Set<C> cs = getCsFromMap(csMap, a.getBs());

            if (!cs.isEmpty()) {

                csByAs.put(a, new ArrayList<>(cs));

            }

        }

    }


    return csByAs;

}


private Set<B> getCsFromMap(Map<Long, C> cMap, List<B> bs) {

    return bs.stream()

            .map(b -> cMap.get(b.getIdOfc()))

            .collect(Collectors.toSet());

}

有没有办法让它更简单???


梦里花落0921
浏览 95回答 3
3回答

www说

如果调用成本很高,那么您最初的想法可以自行执行。它可以进一步缩短为:getCsByIdspublic Map<A, List<C>> convert(Collection<A> as) {&nbsp; &nbsp; List<Long> cIds = as.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .flatMap(a -> a.getBs().stream())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(B::getIdOfC)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());&nbsp; &nbsp; Map<Long, C> csMap = getCsByIds(cIds).stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toMap(C::getId, Function.identity()));&nbsp; &nbsp; return as.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toMap(Function.identity(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a -> a.getBs().stream().map(b -> csMap.get(b.getIdOfC()))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList()), (a, b) -> b));}您可以在其中相应地选择合并功能。(a,b) -> b

翻阅古今

也许只是直接迭代 As?(手头没有编译器,所以也许代码段还没有编译就绪)public Map<A, List<C>> convert(Collection<A> as) {&nbsp; Map<A, List<C>> result = new HashMap<>();&nbsp; for(A a: as){&nbsp; &nbsp; &nbsp;List<Long> cIds = a.getBs().stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.map(B::getIdOfC)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.collect(Collectors.toList());&nbsp; &nbsp; &nbsp;result.put(a, getCsByIds(cIds));&nbsp; }&nbsp; return result;}

慕哥9229398

这样的事情难道行不通吗?我没有编译器,所以我无法真正测试它public Map<A, List<C>> convert(Collection<A> as) {&nbsp; &nbsp; return as.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.collect(Collectors.toMap(Function::identity,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;a -> a.getBs().stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.map(B::getIdOfC)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.flatMap(id -> getCsByIds(asList(id))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.values()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.stream())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.collect(Collectors.toList())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java