Here is and easy way to convert List to a Map where the index is used as key :
import java.util.*; public class Test { public static <ARG> void say(ARG arg) { System.out.println(arg); } public static HashMap<Integer, ?> list2map(List lst) { HashMap map = new HashMap(); for (int i=0; i < lst.size(); i++) { map.put(i, lst.get(i)); } return map; } public static void main(String[] args) { List a = Arrays.asList( 1,2,3,4 ); say(a); say(list2map(a)); } } Output: [1, 2, 3, 4] {0=1, 1=2, 2=3, 3=4}
You can directly test it (the code already setup for you) here in OneCompiler editor : https://onecompiler.com/java/3xh55parb
1 Response
[…] To make that work we will use code from some other posts : [Is Scalar], [join], [List to Map]. […]