Lambda表达式根据主键ID将List<对象>转Map<Long, 对象>

List<YxhClientFormSubmitInfoDTO> yxhClientFormSubmitInfoDTOList = clientFormSubmitInfoResultList.getData();
Map<Long, YxhClientFormSubmitInfoDTO> submitInfoMap = yxhClientFormSubmitInfoDTOList.stream().collect(Collectors.toMap(YxhClientFormSubmitInfoDTO::getId, a -> a, (k1, k2) -> k1));

 

lambda表达式是一种匿名函数,可以用于创建简单的函数对象listmap是C++中的容器,可以存储多个元素。要将一个lambda表达式应用于list中的每个元素,并将结果存储在一个map中,可以使用以下步骤: 1. 创建一个空的map<string, map>对象,用于存储结果。 2. 使用for_each算法遍历list中的每个元素。 3. 在lambda表达式中,对于每个元素,将其换为map<string, map>的键值对,并将其插入到结果map中。 下面是一个示例代码: ```cpp #include <iostream> #include <list> #include <map> #include <algorithm> int main() { std::list<std::map<std::string, std::map<int, std::string>>> myList = { {{"key1", {{1, "value1"}, {2, "value2"}}}}, {{"key2", {{3, "value3"}, {4, "value4"}}}}, {{"key3", {{5, "value5"}, {6, "value6"}}}} }; std::map<std::string, std::map<int, std::string>> resultMap; std::for_each(myList.begin(), myList.end(), [&resultMap](const std::map<std::string, std::map<int, std::string>>& element) { for (const auto& pair : element) { resultMap.insert(pair); } }); // 输出结果 for (const auto& pair : resultMap) { std::cout << pair.first << ": "; for (const auto& innerPair : pair.second) { std::cout << innerPair.first << "->" << innerPair.second << " "; } std::cout << std::endl; } return 0; } ``` 这段代码将list中的每个元素换为map<string, map>的键值对,并将其插入到resultMap中。最后,通过遍历resultMap,可以输出换后的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值