How to convert a list collection into a dictionary in Java?

Java 8Object Oriented ProgrammingProgramming

Following is an example to convert a list collection into a dictionary in Java.

Example

 Live Demo

import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Hashtable;

public class CollectionDictionary {
   public static void main(String[] args) {
      ArrayList<String> list = new ArrayList<String>();
      list.add("JavaFx");
      list.add("Java");
      list.add("WebGL");
      list.add("OpenCV");
      System.out.println(list);
      Dictionary dictionary = new Hashtable();
      Hashtable<Integer, String> hashTable = new Hashtable<Integer, String>();
      hashTable.put(1, list.get(0));
      hashTable.put(2, list.get(1));
      hashTable.put(3, list.get(2));
      hashTable.put(4, list.get(3));
      System.out.println(hashTable);
   }
}

Output

[JavaFx, Java, WebGL, OpenCV]
{4=OpenCV, 3=WebGL, 2=Java, 1=JavaFx}
raja
Published on 17-Jan-2018 11:22:11
Advertisements