An overview of java-Collections-List,Set,Map,Queue Interface are explained in this post. For complete understanding please refer to Sun Certified Programmer for Java 6 Study Guide available in the scribd.com.
Please click on this Ebook Link->Kathy-sierra-PDFBooks
In Collection Framework they are mainly 4 interfaces which mentioned as follows:
List,Set,Map,Queue
List:
1234
List is a type of Collection useful to store duplicate objects.In List elements are stored in index based.List allows null value.ArrayList,Vector and LinkedList are the classes implementing the List interface.
List Example:
1234567891011121314151617181920212223242526
package org.totalqa.collections;import java.util.ArrayList;public class ListExample { public static void main(String[] args) { ArrayList aList = new ArrayList(); aList.add("test"); aList.add(123); aList.add(null); aList.add(55.22); System.out.println("Size of the list " + aList.size()); for(int i=0;i<aList.size();i++) { System.out.println("Element at index "+ i + " is " + aList.get(i)); } //Printing list of values using Enhanced For loop System.out.println("Printing the List of values using for-each"); for(Object o:aList) { System.out.println("Element is " + o); } }}
List Console Output:
12345678910
Size of the list 4Element at index 0 is testElement at index 1 is 123Element at index 2 is nullElement at index 3 is 55.22Printing the List of values using for-eachElement is testElement is 123Element is nullElement is 55.22
1 2 3 4 |
List is a type of Collection useful to store duplicate objects. In List elements are stored in index based. List allows null value. ArrayList,Vector and LinkedList are the classes implementing the List interface. |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package org.totalqa.collections; import java.util.ArrayList; public class ListExample { public static void main(String[] args) { ArrayList aList = new ArrayList(); aList.add("test"); aList.add(123); aList.add(null); aList.add(55.22); System.out.println("Size of the list " + aList.size()); for(int i=0;i<aList.size();i++) { System.out.println("Element at index "+ i + " is " + aList.get(i)); } //Printing list of values using Enhanced For loop System.out.println("Printing the List of values using for-each"); for(Object o:aList) { System.out.println("Element is " + o); } } } |
1 2 3 4 5 6 7 8 9 10 |
Size of the list 4 Element at index 0 is test Element at index 1 is 123 Element at index 2 is null Element at index 3 is 55.22 Printing the List of values using for-each Element is test Element is 123 Element is null Element is 55.22 |
Set Interface:
1 2 3 4 |
Set is type of Collection useful to store unique objects Set Elements are stored without indexing. Set does not allow duplicate elements. Set allows null values. |
Set Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package org.totalqa.collections; import java.util.HashSet; public class SetExample { public static void main(String[] args) { HashSet hSet = new HashSet(); hSet.add("test"); hSet.add(123); hSet.add(null); hSet.add(55.22); System.out.println("Size of the list " + hSet.size()); //Printing list of values using Enhanced For loop System.out.println("Printing the List of values using for-each"); for(Object o:hSet) { System.out.println("Element is " + o); } } } |
Set Console Output:
1 2 3 4 5 6 |
Size of the Set 4 Printing the Set of values using for-each Element is null Element is test Element is 123 Element is 55.22 |
Queue Interface:
1 2 3 4 5 6 7 |
Queue is type of collection allows duplicate Objects. Queue Elements are stored without index Queue implements FIFO(Fist In First Out) Queue does not allow null values PriorityQueue is a Class implementing the Queue Interface PriorityQueue is autosorted. poll() method in queue removes the head element and the queue size reduced by 1 |
Queue Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package org.totalqa.collections; import java.util.PriorityQueue; public class QueueExample { public static void main(String[] args) { PriorityQueue pQueue = new PriorityQueue(); pQueue.add(123); pQueue.add(46); pQueue.add(78); pQueue.add(99); System.out.println("Size of the Queue " + pQueue.size()); System.out.println("Queue Elements" + pQueue); System.out.println("First Element in the Queue"+pQueue.poll()); System.out.println("Size of the Queue " + pQueue.size()); System.out.println("First Element in the Queue"+pQueue.poll()); System.out.println("Size of the Queue " + pQueue.size()); } } |
Queue Console Output:
1 2 3 4 5 6 |
Size of the Queue 4 Queue Elements[46, 99, 78, 123] First Element in the Queue46 Size of the Queue 3 First Element in the Queue78 Size of the Queue 2 |
Map Interface:
1 2 3 4 |
Map is type of collection allows key-value pair. In Map, Key is Object type and it is unique. In Map, Value is referenced using Key. Map is not related to Collection Interface. |
Map Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package org.totalqa.collections; import java.util.HashMap; public class MapExample { public static void main(String[] args) { HashMap hMap = new HashMap(); hMap.put(123,"name1"); hMap.put("key",12.34); hMap.put(null, "123"); System.out.println("Map Elements ::" + hMap); System.out.println("Value is ::" + hMap.get(null)); System.out.println("Keys are ::"+ hMap.keySet()); System.out.println("Values are ::"+ hMap.values()); } } |
Iterator Interface:
1 2 3 4 5 6 7 8 |
Collection API provides an iterator to Iterate each element of any collection type. Iterator has 3 methods: next():This method iterates to next Element and returns the element. If there is no next Element then NoSuchElementException thrown. hasNext():This method checks whether next element exists or not. If next element is present it returns true otherwise it returns false. remove():This method removes the Element from the Collection. When an Element is removed,the previous element point to the next Element. |
Iterator Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package org.totalqa.collections; import java.util.HashSet; import java.util.Iterator; public class IteratorExample { public static void main(String[] args) { HashSet hSet = new HashSet(); hSet.add("test"); hSet.add(123); hSet.add(null); hSet.add(55.22); System.out.println("Elements of the Set are " + hSet); Iterator itr = hSet.iterator(); while(itr.hasNext()) { System.out.println("Element is " + itr.next()); } } } |
Generics
1 2 3 4 |
If all the Elements in the Collection of are of Same Type, then we should use Generics. If a Collection is declared with Generics,we cannot store elements apart from Generic Type. |
Generics Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package org.totalqa.collections; import java.util.ArrayList; public class GenericsExample { public static void main(String[] args) { ArrayList<String> aList = new ArrayList<String>(); aList.add("test"); aList.add("test1"); aList.add(null); aList.add("test2"); System.out.println("Size of the list " + aList.size()); for(int i=0;i<aList.size();i++) { System.out.println("Element at index "+ i + " is " + aList.get(i)); } //Printing list of values using Enhanced For loop System.out.println("Printing the List of values using for-each"); for(Object o:aList) { System.out.println("Element is " + o); } } } |
Really Good information. nicely collated
Thanks for the comments.
Pingback: Sorting the Dynamic HTML Table List in Selenium WebDriver & Java - TotalQA
Pingback: [Solved]Error in Launching Eclipse on a MAC-Failed to find a Main Class - Total-QA
Very useful information. Thank you