Showing posts with label sorted set. Show all posts
Showing posts with label sorted set. Show all posts

Wednesday, May 25, 2011

Operations on SortedSet

The operations that SortedSet inherits from Set behave identically on sorted sets and normal sets with two exceptions:
  • The Iterator returned by the iterator operation traverses the sorted set in order.
  • The array returned by toArray contains the sorted set's elements in order.
Although the interface doesn't guarantee it, the toString method of the JDK's SortedSet implementations returns a string containing all the elements of the sorted set, in order.

Thursday, May 19, 2011

Range-view Operations on SortedSet

The Range-view operations are somewhat analogous to those provided by the List interface, but there is one big difference. Range-views of a sorted set remain valid even if the backing sorted set is modified directly. This is feasible because the endpoints of a range view of a sorted set are absolute points in the element-space, rather than specific elements in the backing collection (as is the case for lists). A range-view of a sorted set is really just a window onto whatever portion of the set lies in the designated part of the element-space. Changes to the range-view write back to the backing sorted set and vice-versa. Thus, it's OK to use range-views on sorted sets for long periods of time (unlike range-views on lists). Sorted sets provide three range-view operations. The first, subSet takes two endpoints (like subList). Rather than indices, the endpoints are objects. They must be comparable to the elements in the sorted set (using the set's Comparator or the natural ordering of its elements, whichever the set uses to order itself). Like subList the range is half-open, including its low endpoint but excluding the high one.
Thus, the following one-liner tells you how many words between "doorbell" and "pickle", including "doorbell" but excluding "pickle", are contained in a SortedSet of strings called dictionary:
int count = dictionary.subSet("doorbell", "pickl

See also post -Operations on SortedSet

Standard Constructors for SortedSet interface

By convention, all Collection implementations provide a standard constructor that takes a Collection, and SortedSet implementations are no exception. This constructor creates a SortedSet object that orders its elements according to their natural order. Additionally, by convention, SortedSet implementations provide two other standard constructors:

  • One that takes a Comparator and returns a new (empty) SortedSet sorted according to the specified Comparator.
  • One that takes a SortedSet and returns a new SortedSet containing the same elements as the given SortedSet, and sorted according to the same Comparator (or using the elements' natural ordering, if the specified SortedSet did too). Note that the compile-time type of the argument determines whether this constructor is invoked in preference to the ordinary Set constructor, and not the runtime type!
The first of these standard constructors is the normal way to create an empty SortedSet with an explicit Comparator. The second is similar in spirit to the standard Collection constructor: it creates a copy of a SortedSet with the same ordering, but with a programmer-specified implementation type.

Operations on SortedSet

The operations that SortedSet inherits from Set behave identically on sorted sets and normal sets with two exceptions:

  • The Iterator returned by the iterator operation traverses the sorted set in order.
  • The array returned by toArray contains the sorted set's elements in order.
Although the interface doesn't guarantee it, the toString method of the JDK's SortedSet implementations returns a string containing all the elements of the sorted set, in order.

See also post - RangeView operations on SortedSet

Wednesday, May 18, 2011

SortedSet interface methods in java

The SortedSet interface is used by TreeSet and adds additional methods to reflect that a TreeSet is sorted.
SortedSet sset;

Result Method Description
comp = sset.comparator() Returns Comparator used to compare elements. null if natural ordering used (eg, String).
obj = sset.firstKey() First element (in sorted order).
obj = sset.lastKey() Last element (in sorted order).
sset = sset.headMap(obj) Returns SortedSet of all elements less than obj.
sset = sset.tailMap(obj) Returns SortedSet of all elements greater than or equal to obj.
sset = sset.subMap(from, to) Returns SortedSet of all elements greater than or equal to from and less than to.

Sunday, March 13, 2011

The SortedSet interface

A SortedSet(in the API reference documentation)is a Set(in the API reference documentation)that maintains its elements in ascending order, sorted according to the elements' natural order, or according to a Comparator provided at SortedSet creation time. (Natural order and Comparators are discussed in the previous section, on Object Ordering.) In addition to the normal Set operations, the Set interface provides operations for:
  • Range-view: Performs arbitrary range operations on the sorted set.
  • Endpoints: Returns the first or last element in the sorted set.
  • Comparator access: Returns the Comparator used to sort the set (if any).

The SortedSet interface is shown below:

public interface SortedSet extends Set {
// Range-view
SortedSet subSet(Object fromElement, Object toElement);
SortedSet headSet(Object toElement);
SortedSet tailSet(Object fromElement);
// Endpoints
Object first();
Object last();
// Comparator access
Comparator comparator();
}





SortedSet Operations 


Standard Constructors 


Range-view Operations on SortedSet


Example – Creating a SortedSet


 

Sunday, September 26, 2010

Creating a Sorted Set

A sorted set is a set that maintains its items in a sorted order. Inserts and retrievals are more expensive in a sorted set but iterations over the set is always in order.

// Create the sorted set

SortedSet set = new TreeSet();

// Add elements to the set
set.add("b");
set.add("c");
set.add("a");

// Iterating over the elements in the set
Iterator it = set.iterator();
while (it.hasNext()) {
// Get element
Object element = it.next();
} // The elements are iterated in order: a, b, c

// Create an array containing the elements in a set (in this case a String array).
// The elements in the array are in order.
String[] array = (String[])set.toArray(new String[set.size()]);

Chitika