Monday, 23 May 2011

ConcurrentSkipList class in java

There are two new interfaces in Java 6 SE called "NavigableMap" and "NavigableSet" which facilitate navigating through collections. NavigableSet extends SortedSet and is implemented by TreeSet and concurrentSkipListSet (a new class in Java collection).
ConcurrentSkipListSet is one of the class that implements NavigableSet and it is used to return the closest matches of elements. It includes methods to return iterators in ascending and descending orders, as well as methods that return a sorted or navigable set for a special portion of data.

NavigableSet nset = new ConcurrentSkipListSet();

nset.add("20");

nset.add("60");

nset.add("50");

nset.add("40");

nset.add("30");

nset.add("80");
// Returns an iterator over the elements in navigable set,
//in ascending order.
Iterator iterator = nset.iterator();

//set in ascending order

while (iterator.hasNext())

{ //Ascending order list

System.out.print(iterator.next() + " ");

}

//Descending order list

System.out.println("In descending order : " + nset.descendingSet() + "\n");
//remove element
nset.pollLast();

No comments:

Post a Comment