Java Map store comma separated values with numbered key

Description

Java Map store comma separated values with numbered key

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class Main {
  public static void main(String[] args) {
    String text = "CSS,HTML,C++,Java,Javascript";
    //from  w  w  w  . j  a  va  2 s .  c o  m
    String[] keyValue = text.split(",");
    
    Map<Integer, String> myMap = new HashMap<Integer, String>();
    for (int i = 0; i < keyValue.length; i++) {
      myMap.put(i, keyValue[i]);
    }
    
    Set<Integer> keys = myMap.keySet();
    Iterator<Integer> itr = keys.iterator();

    while (itr.hasNext()) {
      Integer key = (Integer) itr.next();
      String value = (String) myMap.get(key);
      System.out.println(key + " - " + value);
    }
  }
}



PreviousNext

Related