Java Reference
In-Depth Information
SortedSet < String > tail = s . tailSet ( first + '\0' );
System . out . println ( tail );
// all elements but last
SortedSet < String > head = s . headSet ( last );
System . out . println ( head );
SortedSet < String > middle = s . subSet ( first + '\0' , last );
System . out . println ( middle );
The addition of \0 characters is needed because the tail
Set() and related methods use the successor of an element,
which for strings is the string value with a NULL character
(ASCII code 0) appended.
The List Interface
A List is an ordered collection of objects. Each element of a list has a position in
the list, and the List interface defines methods to query or set the element at a par‐
ticular position, or index . In this respect, a List is like an array whose size changes
as needed to accommodate the number of elements it contains. Unlike sets, lists
allow duplicate elements.
In addition to its index-based get() and set() methods, the List interface defines
methods to add or remove an element at a particular index and also defines meth‐
ods to return the index of the first or last occurrence of a particular value in the list.
The add() and remove() methods inherited from Collection are defined to
append to the list and to remove the first occurrence of the specified value from the
list. The inherited addAll() appends all elements in the specified collection to the
end of the list, and another version inserts the elements at a specified index. The
retainAll() and removeAll() methods behave as they do for any Collection ,
retaining or removing multiple occurrences of the same value, if needed.
The List interface does not define methods that operate on a range of list indexes.
Instead, it defines a single subList() method that returns a List object that repre‐
sents just the specified range of the original list. The sublist is backed by the parent
list, and any changes made to the sublist are immediately visible in the parent list.
Examples of subList() and the other basic List manipulation methods are shown
// Create lists to work with
List < String > l = new ArrayList < String >( Arrays . asList ( args ));
List < String > words = Arrays . asList ( "hello" , "world" );
// Querying and setting elements by index
String first = l . get ( 0 ); // First element of list
String last = l . get ( l . size - 1 ); // Last element of list
Search WWH ::

Custom Search