Linked Questions
32 questions linked to/from How to convert List to Map?
10 votes
6 answers
48k views
Java: how to convert a List<?> to a Map<String,?> [duplicate]
I would like to find a way to take the object specific routine below and abstract it into a method that you can pass a class, list, and fieldname to get back a Map. If I could get a general pointer on ...
14 votes
3 answers
119k views
Converting ArrayList to HashMap [duplicate]
Possible Duplicate: Java: How to convert List to Map I have arrayList ArrayList<Product> productList = new ArrayList<Product>(); productList = getProducts(); //Fetch the result ...
1 vote
3 answers
7k views
Is there a way to convert Array into a Map? [duplicate]
I have an array which I want to convert to Map such that the first element in the array is key and second element is value and so on. I am trying something like below: Arrays.asList("Tamilnadu", "...
0 votes
4 answers
3k views
Convert an array into map using stream [duplicate]
I have a list of integers [1,2,3,4,5] and I want to convert it into a map after applying a multiplication function (*5) like this: {1 = 5, 2 = 10, 3 = 15, 4 = 20, 5 = 25} I was able to use stream ...
0 votes
1 answer
1k views
Java 11 Convert List of Strings to Map<UUID,String> [duplicate]
I am trying to convert the list of strings to Map as below but am getting a compilation error no instance(s) of type variable(s) K, T exist so that UUID conforms to Function<? super T, ? extends K&...
-3 votes
1 answer
2k views
Java: How to convert a list to HashMap (key being the list) in one line [duplicate]
I have an arraylist of Strings: ArrayList<String> list = Arrays.asList("A", "B", "C", "D"); I would like to initialize a map HashMap<String, List<Integer>> with the element of my ...
1 vote
1 answer
1k views
Update map value using java Streams [duplicate]
I need help in making this java code of adding value to map efficient by making use of Java streams to perform this put to map action class Pair{ String key; int value; } void addtoMap(...
-1 votes
1 answer
752 views
Is it possible to initialize the keys and values of a HashMap from a List of keys, setting values to 0 [duplicate]
So I have an array list: static List<Integer> KEYS = Arrays.asList(KEY1, KEY2, KEY3); and I need to create a HashMap<Integer, Double> whereby the keys are KEYS, and the values are ...
0 votes
2 answers
158 views
Java List<String> to Map<String, Long> convertion [duplicate]
is there any way to convert Java List to Map in Java 8? I have a List which I want to add the contents to Map
0 votes
1 answer
97 views
java convert List<Map> to Map<List> [duplicate]
I've below List<Map> as below List<Map<String,String>> list = {"key1":"1234"} {"key1":&...
-1 votes
2 answers
116 views
Collect values from List of objects to Map [duplicate]
I have class: class Employee { private Integer id; private String name; //getters/setters } Also I've an Arraylist with employees: List<Employee> employees = new Arraylist<>(); ...
1 vote
0 answers
61 views
How to build a Map with properties/variable values of a Object laying in a List/Set [duplicate]
im trying to find ways how i can create a Map with certain property/variable values of a object. My english is far from being perfect but i will try to go into details. Lets say we have a simple Java ...
387 votes
4 answers
271k views
Java 8 lambdas, Function.identity() or t->t
I have a question regarding the usage of the Function.identity() method. Imagine the following code: Arrays.asList("a", "b", "c") .stream() .map(Function.identity()) // <- ...
181 votes
6 answers
136k views
Java 8: performance of Streams vs Collections
I'm new to Java 8. I still don't know the API in depth, but I've made a small informal benchmark to compare the performance of the new Streams API vs the good old Collections. The test consists in ...
14 votes
1 answer
29k views
How to populate a HashMap using a Lambda Expression
There is one class (SomeOrders), which has few fields like Id, Summary, Amount, etc... The requirement is to collect Id as key and Summary as value to a HashMap from an input List of SomeOrder ...