Java Map create from List using the index as key

Description

Java Map create from List using the index as key

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
  public static void main(String[] args) {
    List<String> aList = new ArrayList<String>();
    aList.add("CSS");
    aList.add("HTML");
    //from w  w  w .  jav  a 2s . c o  m
    
    Map<Integer, String> aMap = new HashMap<Integer, String>();

    
    for (int i = 0; i < aList.size(); i++) {
      aMap.put(i + 1, aList.get(i));
    }
    
    System.out.println(aMap.toString());
  }
}



PreviousNext

Related