简体   繁体   English

比较集<String>带地图<Object>在 Java 8 中获取 Map 值

[英]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.

相关问题 如何在列表中获取最大值<map<string, object> > 在 Java8 </map<string,> - How to get max value in List<Map<String, Object>> at Java8 Java 在地图上打印值对象<String, Object> - Java Print Value Object on Map<String, Object> 地图<String, Map<Object, Object> &gt; 如何在java中动态获取值对 - Map<String, Map<Object, Object>> how to get the value pair dynamically in java 使用Set或Map将对象排序为Java值 - Using Set or Map for object ordered to a value Java 如何从java地图中获取价值:地图 <String,ArrayList<String> &gt;? - How to get value from the java map : Map<String,ArrayList<String>>? 字符串与Java Map中的键进行比较 - String compare with keys in Map in Java 使用Java 8进行过滤:地图 <String, Set<Object> &gt;从地图 <String, Set<Object> &gt;基于对象的属性 - Filtering with Java 8: Map<String, Set<Object>> from Map<String, Set<Object>> based on an attribute of Object SpringBoot / Java:如何计算项目并在对象映射中获取结果<String,Map<String, Map<String,Long> &gt;&gt; - SpringBoot / Java : How to count Item and get the result in an object Map<String,Map<String, Map<String,Long>>> 比较两张地图<String, List<String> &gt; 并获取地图键 + 地图值作为差异 - Compare two Maps<String, List<String>> and get map key + map value as differences 放<Object>到地图<String, Set<Long> &gt; java8 - Set<Object> to Map<String, Set<Long>> java8
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM