English 中文(简体)
通过在 Java绘制地图
原标题:Streams for iterating through Map of List of Map in Java

I have a map that contains Integer as key and (List of Map of String as key and boolean as the value) as value. Map<Int, List<Map<String, Boolean>>>, I want to populate a set that has Int as key of the outer map based on condition.

Myservice.java


public Set<Integer> getValue(String input){
        Map<String, Boolean> in1 = new HashMap<>();
        in1.put("test_1", true);
        Map<String, Boolean> in2 = new HashMap<>();
        in2.put("test_2", false);
        Map<String, Boolean> in3 = new HashMap<>();
        in2.put("test_3", false);
        Map<String, Boolean> in4 = new HashMap<>();
        in2.put("test_4", true);
        List<Map<String, Boolean>> l1 = new ArrayList<>();
        l1.add(in1);
        l1.add(in2);
        List<Map<String, Boolean>> l2 = new ArrayList<>();
        l2.add(in3);
        l2.add(in4);
        Map<Integer, List<Map<String,Boolean>>> map = new HashMap();
        map.put(123, l1);
        map.put(345, l2);

        Set<Integer> result = new HashSet<>();
        for(Map.Entry<Integer, List<Map<String, Boolean>>> entry : map.entrySet()){
            for(Map<String, Boolean> m: entry.getValue() ){
                if(m.containsKey(input) && m.get(input) == true){
                    result.add(entry.getKey());
                }
            }
        }
        return result;
    }

因此,我基本上想先从外部地图上抹去,以便绘制内部地图,然后绘制内部地图,以核实投入是否存在,并添加到一套内容。 我如何利用java 8溪流做到这一点?

我尝试了 lo,但我要用 Java流取代它。

最佳回答

This produced the same results as your code in a test that passed "test_1", etc. into it.

map.entrySet().stream()
    .filter(entry -> entry.getValue().stream()
        .anyMatch(m -> m.getOrDefault(input, false)))
    .map(Map.Entry::getKey)
    .collect(Collectors.toSet());
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签