How to Convert List to Map

How to convert List to Map?

List<Item> list;
Map<Key,Item> map = new HashMap<Key,Item>();
for (Item i : list) map.put(i.getKey(),i);

Assuming of course that each Item has a getKey() method that returns a key of the proper type.

How do I convert a Map to List in Java?

List<Value> list = new ArrayList<Value>(map.values());

assuming:

Map<Key,Value> map;

How to convert List of Map into Map of List using Java Streams

You can do this without having to instantiate an additional data structure. You first map the Maps to Map.Entry and then group by key.

var listOfMaps = List.of(
Map.of("a", 10, "b", 20),
Map.of("a", 12, "b", 22),
Map.of("a", 14, "b", 24)
);

var mapOfLists = listOfMaps.stream()
.map(Map::entrySet)
.flatMap(Set::stream)
.collect(Collectors.groupingBy(
Map.Entry::getKey,
Collectors.mapping(
Map.Entry::getValue,
Collectors.toList()
)
));

System.out.println(mapOfLists);

Output:
{a=[10, 12, 14], b=[20, 22, 24]}

convert List to map

If all you need for your list are the values of the map, you can just transform the stream using the map method and extracting the values from each stream element using the values property:

habits.stream  .map((m) => m.values.toList())  .filter((habit) {

How to convert list to Map of Map

You could use groupingBy which takes a classifier and a downstream collector. The classifier is used to create the outer map based on division and then the downstream collector (toMap) is used to generate the inner map:

list.stream()
.collect(Collectors.groupingBy(MyObject::getDivision,
Collectors.toMap(MyObject::getSubDivision, MyObject::getSize)));

How to convert list of objects to list of map

If you are using android studio, you can install a pluging to help generate toMap() and fromMap() codes. The name of the plugin is Dart Data Class

After installing the plugin, generate the helper function like this

class Day{
double open;
double high;
double low;
double close;
double volumeTo;

Day({this.open, this.high, this.low, this.close, this.volumeTo});

factory Day.fromMap(Map<String, dynamic> map) {
return new Day(
open: map['open'] as double,
high: map['high'] as double,
low: map['low'] as double,
close: map['close'] as double,
volumeTo: map['volumeTo'] as double,
);
}

Map<String, dynamic> toMap() {
// ignore: unnecessary_cast
return {
'open': this.open,
'high': this.high,
'low': this.low,
'close': this.close,
'volumeTo': this.volumeTo,
} as Map<String, dynamic>;
}
}

// this helper method helps convert to a list of Map
dynamic getListMap(List<dynamic> items) {
if (items == null) {
return null;
}
List<Map<String, dynamic>> dayItems = [];
items.forEach((element) {
dayItems.add(element.toMap());
});
return dayItems;
}

and call the function like this


void somthoing(){
var listOfMap = getListMap(historical);
}

convert list of objects to Map String, Object using map/stream/collect

I think you want this:

Map<String, Person> result = fetchedPeople.stream().collect(Collectors.toMap(Person::getID, e -> e));

I don't know if there is a way to return "the element itself" without supplying a lambda function that does that, the e -> e part.

how to convert list to map by java8?

You can't use toMap() because you have many nodeKey/nodeValue pairs to map to a single masterId value (unless you use a merge function).

This is easier to do grouping by masterId:

Map<String, Map<String, String>> result = additionalMetrics.stream()
.collect(Collectors.groupingBy(
AdditionalMetrics::getMasterId,
Collectors.toMap(AdditionalMetrics::getNodeKey,
AdditionalMetrics::getNodeValue)));


Related Topics



Leave a reply



Submit