小编Raj*_*ate的帖子

如何编写 Collectors.toMap() 的合并函数

UserMst是对象,users也是我从中获取 LoginId && TenantId 的列表。

LinkedHashMap<String, String> Tmap = users.stream().collect(Collectors.toMap(
                    UserMst::getLoginId, UserMst::getTenantId,
                        (x, y)-> ((x=="1") ? "Rocks" : (x=="2")
                                           ? "Mocks" : (x=="3")
                                           ? "Docs"  : (x=="4")
                                           ? "Pocks" : "")
                                   + " ,  " + ( (y=="1") ? "Rocks": (y=="2")
                                                         ? "Mocks": (y=="3")
                                                         ? "Docs" : (y=="4")
                                                         ? "Pocks": ""),
                        LinkedHashMap::new));


///////////////////

LinkedHashMap<String, String> Tmap =
    users.stream().collect(
        Collectors.toMap( UserMst::getLoginId, UserMst::getTenantId,
        (x, y)-> x + ", " + y, LinkedHashMap::new)); 

```

It gives output as answer:: …
Run Code Online (Sandbox Code Playgroud)

java collections java-stream

0
推荐指数
1
解决办法
4013
查看次数

标签 统计

collections ×1

java ×1

java-stream ×1