Suppose we have a requirement, from sorted set elements [5,10,15,20] we want few things like this
- Retrieve the element which is immediately greater than or lower than element 15
- Retrieve all elements greater than or lower than 10
Example
import java.util.concurrent.*;
import java.util.*;
class SkipListSetTest
{
public static void main(String[] args)
{
ConcurrentSkipListSet csls = new ConcurrentSkipListSet();
csls.add(15);
csls.add(20);
csls.add(5);
csls.add(10);
System.out.println("Elements in the collections are");
for(Integer i: csls)
{
System.out.println(i);
}
/* Retrieve immediate element less than or equal to the given element */
System.out.println("Floor "+csls.floor(12));
/* Retrieve immediate element greater than or equal to the given element */
System.out.println("Ceiling "+csls.ceiling(12));
/* Retrieve immediate element less than the given element */
System.out.println("Lower "+csls.lower(10));
/* Retrieve immediate element greater than the given element */
System.out.println("heigher "+csls.higher(10));
System.out.println("Head Elements ");
Set cslsHeadView = csls.headSet(10);
//HeadSet excludes the given element
for(Integer i: cslsHeadView)
{
System.out.println(i);
}
Set cslsTailView = csls.tailSet(10);
//TailSet includes the given element
System.out.println("Tail Elements");
for(Integer i: cslsTailView)
{
System.out.println(i);
}
}
}
Output:
Elements in the collections are
5
10
15
20
Floor 10
Ceiling 15
Lower 5
heigher 15
Head Elements
5
Tail Elements
10
15
20
NavigableMap and ConcurrentSkipListMap
NaviagableMap is similar to NaviagableSet. In NavigableSet, methods use to return values, but in NaviagableMap methods used to return the key,value pair.ConcurrentSkipListMap is the one of the class which implements NaviagableMap.
import java.util.*;
import java.util.concurrent.*;
class NavigableMapExample
{
public static void main(String[] args)
{
NavigableMap nm = new ConcurrentSkipListMap();
nm.put(1,"One");
nm.put(2,"Two");
nm.put(3,"Three");
nm.put(4,"Four");
nm.put(5,"Five");
/* Retrieves the key,value pair immediately lesser than the given key */
Map.Entry ae = nm.lowerEntry(5);
/* Map.Entry is a Static interface nested inside Map
interface,just use to hold key and value */
System.out.println("Key" + ae.getKey());
System.out.println("Value"+ ae.getValue());
/* Retrieves key,value pairs equal to and greater then the given key */
SortedMap mm = nm.tailMap(3);
Set s = mm.keySet();
System.out.println("Tail elements are");
for(Integer i:s)
{
System.out.println("Key "+ i + "Value "+ mm.get(i));
}
}
}
output
Key 4
Value Four
Tail elements are
Key 3 Value Three
Key 4 Value Four
Key 5 Value Five
Notes :
floorEntry method retrieves less than or equal to the givenkey (or) null
lowerEntry method retrieves always less than the givenkey (or) null
headMap method retrieves all elements less than the givenkey
tailMap method retrieves all elements greater than or equal to the givenkey
AbstractMap.SimpleEntry and AbstractMap.SimpleImmutableEntry
AbstractMap.SimpleEntry and AbstractMap.SimpleImmutableEntry are a static classes nested inside abstractMap class.The instance of this classes use to hold the key,value pair of one single entry in a Map.The difference between these two classes is that former one allow us to set the value and later one if we try to set the value, it throws UnsupportedOperationException
Modified classes
Classes are modified to implement the new interfaces in the existing classes.LinkedList is modified to implement Deque. TreeSet is modified to implement NavigableSet.TreeMap is modified to implement NavigableMap.In Collections 2 new methods newSetFromMap and asLifoQueue are added.




1 comments:
What is untitled ? First Be clear it.
mate tee
Post a Comment