gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
优化下面代码: for(FlowTopicMap flowTopicMap:dmsFlowTopicMapList){ for(Topic topic:topicList){…
- User
优化下面代码: for(FlowTopicMap flowTopicMap:dmsFlowTopicMapList){ for(Topic topic:topicList){ if(ObjectUtil.equals(flowTopicMap.getTopicId(),topic.getId())){ topics.add(topic); } } }
- Assistant
可以使用流式操作来简化代码:
topics.addAll(dmsFlowTopicMapList.stream() .map(FlowTopicMap::getTopicId) .filter(id -> topicList.stream().anyMatch(topic -> ObjectUtil.equals(topic.getId(), id))) .map(id -> topicList.stream().filter(topic -> ObjectUtil.equals(topic.getId(), id)).findFirst().orElse(null)) .collect(Collectors.toList()));上述代码中,首先使用流操作获取所有符合条件的
topicId,然后对每个topicId过滤出满足条件的topic对象,并将其收集到最终的topics集合中。