【问题标题】:extract values from an ArrayList of Hashmap with java8使用 java8 从 Hashmap 的 ArrayList 中提取值
【发布时间】:2016-07-21 22:41:52
【问题描述】:

我有一个 HashMap 的arrayList

我如何使用 java 8 和 .map .filter 技术来获取 HashMap 中某个值的字符串的 ArrayList

例如

字符串数据等于

[{
    "schema": [{
        "name": "#VALUE",
        "dataType": {},
        "nullable": true,
        "metadata": {
            "map": {}
        }
    }, {
        "name": "@ref",
        "dataType": {},
        "nullable": true,
        "metadata": {
            "map": {}
        }
    }],
    "values": [null, 442256]
}, {
    "schema": [{
        "name": "#VALUE",
        "dataType": {},
        "nullable": true,
        "metadata": {
            "map": {}
        }
    }, {
        "name": "@ref",
        "dataType": {},
        "nullable": true,
        "metadata": {
            "map": {}
        }
    }],
    "values": [null, 4192463331]
}, {
    "schema": [{
        "name": "#VALUE",
        "dataType": {},
        "nullable": true,
        "metadata": {
            "map": {}
        }
    }, {
        "name": "@ref",
        "dataType": {},
        "nullable": true,
        "metadata": {
            "map": {}
        }
    }],
    "values": [null, 34817060]
}, {
    "schema": [{
        "name": "#VALUE",
        "dataType": {},
        "nullable": true,
        "metadata": {
            "map": {}
        }
    }, {
        "name": "@ref",
        "dataType": {},
        "nullable": true,
        "metadata": {
            "map": {}
        }
    }],
    "values": [null, 291594905]
}]

结果

["442256","4192463331","34817060","291594905"]

我的这部分代码但不起作用

    Stream.of(mapper.readValue(mapper.writeValueAsString(data),ArrayList.class))
            .filter(c -> c instanceof ArrayList<?>)
            .map(ArrayList.class::cast)
            .map(c -> 
                    c.forEach(f -> { Stream.of(mapper.readValue(mapper.writeValueAsString(f),HashMap.class))
                                    .filter(f -> f instanceof HashMap<?,?>)
                                    .map(HashMap.class::cast)
                                    ...
                                }
                            )
                )
            .forEach(System.out::println);

有什么想法吗?

亲切

【问题讨论】:

  • 您没有提供MCVE
  • 请发布完整的可运行代码。
  • 首先,创建一个List&lt;Map&lt;String, List&lt;Object&gt;&gt;&gt;

标签: java arraylist hashmap java-8 java-stream


【解决方案1】:

您问题中的代码非常令人困惑,但如果我从字面上理解您的问题,“给定一个List&lt;Map&lt;String, String&gt;&gt;,我如何获得一个代表给定键映射中所有值的List&lt;String&gt;?”如果尽管与您的代码不匹配,这实际上是您的问题,那么这里有一个答案:

List<Map<String, String>> listOfMaps;
List<String> valuesMatchingKey = listOfMaps.stream()
    .filter(map -> map.containsKey("Key"))
    .map(map -> map.get("Key"))
    .collect(Collectors.toList());

【讨论】:

  • List>> listOfMaps = mapper.readValue(mapper.writeValueAsString(data),ArrayList.class); List valuesMatchingKey = listOfMaps.stream() .filter(map -> map.containsKey("values")) .map(map -> String.valueOf( map.get("values").get(1) ) ) .collect(Collectors.toList());谢谢它对我有用
猜你喜欢
  • 1970-01-01
  • 2012-04-28
  • 1970-01-01
  • 2014-06-14
  • 2014-03-22
  • 2017-12-23
  • 2011-05-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多