We would like to know how to convert List to Hashmap using the index as key.
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; //w w w . j ava 2 s.c om public class Main { public static void main(String[] args) { List<String> aList = new ArrayList<String>(); Map<Integer, String> aMap = new HashMap<Integer, String>(); aList.add("A"); aList.add("B"); for (int i = 0; i < aList.size(); i++) { aMap.put(i + 1, aList.get(i)); } System.out.println(aMap.toString()); } }
The code above generates the following result.