| 7 | |
| 8 | public class ListSortSearch { |
| 9 | public static void main(String[] args) { |
| 10 | List<String> list = |
| 11 | new ArrayList<>(Utilities.list); |
| 12 | list.addAll(Utilities.list); |
| 13 | System.out.println(list); |
| 14 | Collections.shuffle(list, new Random(47)); |
| 15 | System.out.println("Shuffled: " + list); |
| 16 | // Use ListIterator to trim off last elements: |
| 17 | ListIterator<String> it = list.listIterator(10); |
| 18 | while(it.hasNext()) { |
| 19 | it.next(); |
| 20 | it.remove(); |
| 21 | } |
| 22 | System.out.println("Trimmed: " + list); |
| 23 | Collections.sort(list); |
| 24 | System.out.println("Sorted: " + list); |
| 25 | String key = list.get(7); |
| 26 | int index = Collections.binarySearch(list, key); |
| 27 | System.out.println( |
| 28 | "Location of " + key + " is " + index + |
| 29 | ", list.get(" + index + ") = " + |
| 30 | list.get(index)); |
| 31 | Collections.sort(list, |
| 32 | String.CASE_INSENSITIVE_ORDER); |
| 33 | System.out.println( |
| 34 | "Case-insensitive sorted: " + list); |
| 35 | key = list.get(7); |
| 36 | index = Collections.binarySearch(list, key, |
| 37 | String.CASE_INSENSITIVE_ORDER); |
| 38 | System.out.println( |
| 39 | "Location of " + key + " is " + index + |
| 40 | ", list.get(" + index + ") = " + |
| 41 | list.get(index)); |
| 42 | } |
| 43 | } |
| 44 | /* Output: |
| 45 | [one, Two, three, Four, five, six, one, one, Two, |