有人会帮助我通过流和过滤来获取映射值的数组吗?

public class TheMap extends HashMap<String, String> {
    public TheMap(String name, String title) {
        super.put("name", name);
        super.put("title", title);
    }

    public static void main(final String[] args) {
        Map<Long, Map<String, String>>map = new HashMap<>();

        map.put(0L, null);
        map.put(1L, new TheMap("jane", "engineer"));
        map.put(2L, new TheMap("john", "engineer"));
        map.put(3L, new TheMap(null, "manager"));
        map.put(4L, new TheMap("who", null));
        map.put(5L, new TheMap(null, null));
    }
}

我正在寻找的结果是只有这两个条目的ArrayList<TheMap>:
TheMap("jane", "engineer")
TheMap("john", "engineer")

基本上,使用TheMap名称和none-null检索title

最佳答案

如果需要TheMap的arrayList,请尝试以下方式:

ArrayList<TheMap> as = map.values()
   .stream()
   .filter(v -> v != null && v.get("name") != null && v.get("title") != null)
   .map(m -> (TheMap)m)
   .collect(Collectors.toCollection(ArrayList::new)));

09-12 03:52