24.15. Avoiding Deadlocks

 
[Page 726 ( continued )]

22.6. Static Methods for Lists and Collections

You can use TreeSet to store sorted elements in a set. But there is no sorted list. However, the Java Collections Framework provides static methods in the Collections class that can be used to sort a list. The Collections class also contains the binarySearch , reverse , shuffle , copy , and fill methods on lists, and max , min , disjoint , and frequency methods on collections, as shown in Figure 22.14.

Figure 22.14. The Collections class contains static methods for manipulating lists and collections.
(This item is displayed on page 727 in the print version)

You can sort the comparable elements in a list in its natural order through the compareTo method in the Comparable interface. You may also specify a comparator to sort elements. For example, the following code sorts strings in a list:

 List<String> list = Arrays.asList(   "red"   ,   "green"   ,   "blue"   );  Collections.sort(list);  System.out.println(list); 

The output is [blue, green, red] .

The preceding code sorts a list in ascending order . To sort it in descending order , you may simply use the Collections.reverseOrder() method to return a Comparator object that orders the elements in reverse order. For example, the following code sorts a list of strings in descending order:


[Page 727]
 List<String> list = Arrays.asList(   "green"   ,   "red"   ,   "yellow"   ,   "blue"   ); Collections.sort(list,  Collections.reverseOrder()  ); System.out.println(list); 

The output is [yellow, red, green, blue] .

You can use the binarySearch method to search for a key in a list. The list must be presorted in increasing order. If the key is not in the list, the method returns “(insertion point + 1) . Recall that the insertion point is where the item would fall in the list if it were present. For example, the following code searches the keys in a list of integers and a list of strings:

 List<Integer> list1 = Arrays.asList(   2   ,   4   ,   7   ,   10   ,   11   ,   45   ,   50   ,   59   ,   60   ,   66   ); System.out.println(   "(1) Index: "   + Collections.binarySearch(list1,   7   )); System.out.println(   "(2) Index: "   + Collections.binarySearch(list1,   9   )); List<String> list2 = Arrays.asList(   "blue"   ,   "green"   ,   "red"   ); System.out.println(   "(3) Index: "   + Collections.binarySearch(list2,   "red"   )); System.out.println(   "(4) Index: "   + Collections.binarySearch(list2,   "cyan"   )); 

The output of the preceding code is

   (1) Index: 2     (2) Index: 4     (3) Index: 2     (4) Index: 2   


[Page 728]

You can use the reverse method to reverse the elements in a list. For example, the following code displays [blue, green, red, yellow] :

 List<String> list = Arrays.asList(   "yellow"   ,   "red"   ,   "green"   ,   "blue"   );  Collections.reverse(list);  System.out.println(list); 

You can use the shuffle(List) method to randomly reorder the elements in a list. For example, the following code shuffles the elements in list :

 List<String> list = Arrays.asList(   "yellow"   ,   "red"   ,   "green"   ,   "blue"   );  Collections.shuffle(list);  System.out.println(list); 

You can also use the shuffle(List, Random) method to randomly reorder the elements in a list with a specified Random object. Using a specified Random object is useful to generate a list with identical sequences of elements for the same original list. For example, the following code shuffles the elements in list :

 List<String> list1 = Arrays.asList(   "yellow"   ,   "red"   ,   "green"   ,   "blue"   ); List<String> list2 = Arrays.asList(   "yellow"   ,   "red"   ,   "green"   ,   "blue"   );  Collections.shuffle(list1,   new   Random(   20   ));   Collections.shuffle(list2,   new   Random(   20   ));  System.out.println(list1); System.out.println(list2); 

You will see that list1 and list2 have the same sequence of elements before and after the shuffling.

You can use the copy(det, src) method to copy all the elements from a source list to a destination list on the same index. The destination must be as long as the source list. If it is longer, the remaining elements in the source list are not affected. For example, the following code copies list2 to list1 :

 List<String> list1 = Arrays.asList(   "yellow"   ,   "red"   ,   "green"   ,   "blue"   ); List<String> list2 = Arrays.asList(   "white"   ,   "black"   );  Collections.copy(list1, list2);  System.out.println(list1); 

The output for list1 is [white, black, green, blue] . The copy method performs a shallow copy. Only the references of the elements from the source list are copied .

You can use the nCopies(int n, Object o) method to create an immutable list that consists of n copies of the specified object. For example, the following code creates a list with five Calendar objects:

 List<GregorianCalendar> list1 =  Collections.nCopies   (   5   ,   new   GregorianCalendar(   2005   ,     ,   1   ));  

The list created from the nCopies method is immutable, so you cannot add, remove, or update elements in the list. All the elements have the same references.

You can use the fill(List list, Object o) method to replace all the elements in the list with the specified element. For example, the following code displays [black, black, black] :

 List<String> list = Arrays.asList(   "red"   ,   "green"   ,   "blue"   );  Collections.fill(list,   "black"   );  System.out.println(list); 


[Page 729]

You can use the max and min methods for finding the maximum and minimum elements in a collection. The elements must be comparable using the Comparable interface or the Comparator interface. For example, the following code displays the largest and smallest strings in a collection:

 Collection<String> collection = Arrays.asList(   "red"   ,   "green"   ,   "blue"   ); System.out.println(  Collections.max(collection)  ); System.out.println(  Collections.min(collection)  ); 

The disjoint(collection1, collection2) method returns true if the two collections have no elements in common. For example, in the following code, disjoint(collection1, collection2) returns false , but disjoint(collection1, collection3) returns true :

 Collection<String> collection1 = Arrays.asList(   "red"   ,   "cyan"   ); Collection<String> collection2 = Arrays.asList(   "red"   ,   "blue"   ); Collection<String> collection3 = Arrays.asList(   "pink"   ,   "tan"   ); System.out.println(  Collections.disjoint(collection1, collection2)  ); System.out.println(  Collections.disjoint(collection1, collection3)  ); 

The frequency(collection, element) method finds the number of occurrences of the element in the collection. For example, frequency(collection, "red") returns 2 in the following code:

 Collection<String> collection = Arrays.asList(   "red"   ,   "cyan"   ,   "red"   ); System.out.println(Collections.frequency(collection,   "red"   )); 

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net