Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。
想改善这个问题吗?更新问题,以便将其作为on-topic用于堆栈溢出。
6年前关闭。
Improve this question
我有如下数据:
(以上数据的每一行都是一个对象,其字段“key”和“value”全部在一个列表
我想将数据构造为
如何从数据对象实现上述
****** 除了 ******
我对使用纯JDK提供的类或接口来实现结果而不是使用外部库更感兴趣。有什么帮助吗?
想改善这个问题吗?更新问题,以便将其作为on-topic用于堆栈溢出。
6年前关闭。
Improve this question
我有如下数据:
Key value
----- ------
car toyota
car bmw
car honda
fruit apple
fruit banana
computer acer
computer asus
computer ibm
...
(以上数据的每一行都是一个对象,其字段“key”和“value”全部在一个列表
List<DataObject>
中)我想将数据构造为
Map<String, List<String>>
,如下所示: "car" : ["toyota", "bmw", "honda"]
"fruit" : ["apple","banana"]
"computer" : ["acer","asus","ibm"]
如何从数据对象实现上述
Map
结构?****** 除了 ******
我对使用纯JDK提供的类或接口来实现结果而不是使用外部库更感兴趣。有什么帮助吗?
最佳答案
Map<String, List<String>> myMaps = new HashMap<String, List<String>>();
for (DataObject item : myList) {
if (!myMaps.containsKey(item.getKey())) {
myMaps.put(item.getKey(), new ArrayList<String>());
}
myMaps.get(item.getKey()).add(item.getValue());
}