Program to demonstrate how to print collections in the Collections Framework on the console in Java
package com.hubberspot.example; // 1. Importing all the highly used Collection // classes, Collection and Map interface import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.Map; import java.util.TreeMap; import java.util.TreeSet; public class PrintingCollections { public static void main(String[] args) { // 2. Creating an Object of each Collection // --- Array --- int[] integers = new int[6]; // --- Array --- Collection arrayList = new ArrayList(); // --- Array --- Collection linkedList = new LinkedList(); // --- Array --- Collection hashSet = new HashSet(); // --- Array --- Collection treeSet = new TreeSet(); // --- Array --- Collection linkedHashSet = new LinkedHashSet(); // --- Array --- Map hashMap = new HashMap(); // --- Array --- Map treeMap = new TreeMap(); // --- Array --- Map linkedHashMap = new LinkedHashMap(); // 3. Adding few elements to described Collections above addElements(integers); addElements(arrayList); addElements(linkedList); addElements(hashSet); addElements(treeSet); addElements(linkedHashSet); addElements(hashMap); addElements(treeMap); addElements(linkedHashMap); // 4. Printing all the elements in Collections System.out.println("The Collection printing goes like this : \n"); // 5. Arrays are usually printed by passing whole array to // Arrays class method named as toString() System.out.println("Array : "+Arrays.toString(integers)); // arraylist and linkedlist usually displays elements // in order of their entry to Collection System.out.println("ArrayList : "+arrayList); System.out.println("LinkedList : "+linkedList); // hashset doesn't allow duplicate entries to be // inserted in to an collection System.out.println("HashSet : "+hashSet); // treeset doesn't allow duplicate entries plus it // sorts the elements added to it System.out.println("TreeSet : "+treeSet); // linkedhashset doesn't sort the elements but it // displays the order in which the elements were added System.out.println("LinkedHashSet : "+linkedHashSet); System.out.println("HashMap : "+hashMap); // treemap sorts the elements added to it System.out.println("TreeMap : "+treeMap); // linkedhashmap doesn't sort the elements but it // displays the order in which the elements were added System.out.println("LinkedHashMap : "+linkedHashMap); } // 6. For adding elements to Collection type created above static Collection addElements(Collection collection) { collection.add(3); collection.add(2); collection.add(1); collection.add(1); collection.add(2); collection.add(3); return collection; } //7. For adding elements to Map type created above static Map addElements(Map map) { map.put(6, "One"); map.put(5, "Two"); map.put(4, "Three"); map.put(3, "Four"); map.put(2, "Five"); map.put(1, "Six"); return map; } // 8. For adding elements to array type created above static int[] addElements(int[] integers) { integers[0] = 1; integers[1] = 2; integers[2] = 3; integers[3] = 4; integers[4] = 5; integers[5] = 6; return integers; } }
Output of the program :