Searching...
Saturday, 6 October 2012

Java Collections: LinkedHashSet Example

06:25
·         Hash table and linked list implementation of the Set interface
·         LinkedHashSet differs from HashSet in that the order is maintained
·         Performance is below that of HashSet, due to the expense of maintaining the linked list
·         Its methods are not synchronized
 
 
image

Example

package mycollectiontest;

import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

public class LinkedHashSetExample {

public static void main(String[] args) {
// Create a LinkedHashSet
Set<String> lHashSet = new LinkedHashSet<String>();
lHashSet.add("Bangalore");
lHashSet.add("Chennai");
lHashSet.add("Cochin");
System.out.println(lHashSet);

// Iterating
Iterator<String> it = lHashSet.iterator();

while (it.hasNext())
System.out.println("Elements " + it.next());
}
}

Output

[Bangalore, Chennai, Cochin]
Elements Bangalore
Elements Chennai
Elements Cochin

0 comments: