[英]Compare Set<String> with Map<Object> to get Map value in Java 8
I have Set<String> requests = new HashSet<>()
and added dynamically some string values in it.我有Set<String> requests = new HashSet<>()
并在其中动态添加了一些字符串值。
I have Map<TargetSystems> targetSystems = new HashMap<>();
我有Map<TargetSystems> targetSystems = new HashMap<>();
, targetSystems with name key and targetSystems object in value. , targetSystems 具有名称键和值中的 targetSystems 对象。
So I need those targetSystems from Map which are in in Set.所以我需要来自 Map 的那些在 Set 中的 targetSystems。
Like Map.put("LMS", TargetSystems) and many more from Database.像 Map.put("LMS", TargetSystems) 以及来自数据库的更多内容。 Set<String>
requests has same target systems name. Set<String>
请求具有相同的目标系统名称。 How can I achieve this in Java 8. I don't want to execute forEach loop into Set because I have already created thread list and looping on that.我怎样才能在 Java 8 中实现这一点。我不想在 Set 中执行 forEach 循环,因为我已经创建了线程列表并在其上循环。 So I need solution in Java 8 with Stream.filter kind of.所以我需要在 Java 8 中使用 Stream.filter 的解决方案。
Thanks in advance.提前致谢。
I am going to assume that you meant Map<String, TargetSystems>
.我将假设您的意思是Map<String, TargetSystems>
。
You can make a copy of the Map, and use Set.retainAll to limit which entries are in it:您可以制作 Map 的副本,并使用Set.retainAll来限制其中的条目:
Map<String, TargetSystems> copy = new HashMap<>(targetSystems);
copy.keySet().retainAll(requests);
Collection<TargetSystems> systemsForRequests = copy.values();
If you want to filter your targetSystems map:如果要过滤 targetSystems 映射:
Map<String, TargetSystems> targetSystemsFiltered = targetSystems.entrySet().stream()
.filter(entry -> requests.contains(entry.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
If you want just to get filtered targetSystem objects:如果您只想获取过滤的 targetSystem 对象:
Set<TargetSystems> targetSystemsFromSet = targetSystems.entrySet().stream()
.filter(entry -> requests.contains(entry.getKey()))
.map(Map.Entry::getValue)
.collect(Collectors.toSet());
I have already got an answer and code below please see it's working fine.我已经在下面得到了答案和代码,请查看它是否正常工作。 Case was that, if we have Map<Object(Its entity class type)> targetSystems and put targetSystems entity objects by the name of it's entity key.情况是,如果我们有 Map<Object(Its entity class type)> targetSystems 并按实体键的名称放置 targetSystems 实体对象。 Now I have multiple request by the same name exists in targetSystems Map, so I need to equalized that hashset value with hashmap.现在我在 targetSystems Map 中存在多个同名请求,所以我需要用 hashmap 均衡该 hashset 值。 I achieved as under:我实现如下:
Object[] objects = Map<String, Object>.values().stream().filter(ts -> Set.contains(ts.getName())).toArray(); Object[] objects = Map<String, Object>.values().stream().filter(ts -> Set.contains(ts.getName())).toArray();
I get Object[] type, so I can get my targetSystems value from Map as easy as I like.我得到 Object[] 类型,所以我可以像我喜欢的那样轻松地从 Map 获取我的 targetSystems 值。
Thanks all of you for your kind answers and thanks to stack Overflow.感谢大家的友好回答,感谢堆栈溢出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.