List of usage examples for java.util Collections sort
@SuppressWarnings("unchecked") public static <T extends Comparable<? super T>> void sort(List<T> list)
From source file:NameSort.java
public static void main(String args[]) { String n[] = new String[] { "John", "Lennon", "Karl", "Marx", "Groucho", "Marx", "Oscar", "Grouch" }; List l = Arrays.asList(n); Collections.sort(l); System.out.println(l);//from w ww . ja v a 2 s .com }
From source file:Main.java
public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("Java"); list.add("R"); list.add("CSS"); list.add("XML"); Collections.sort(list); System.out.println("List: " + list); Collections.reverse(list);/* w ww . ja v a2 s . c o m*/ System.out.println("List: " + list); }
From source file:Main.java
public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("Java"); list.add("R"); list.add("CSS"); list.add("XML"); Collections.sort(list); System.out.println("List: " + list); // Rotate elements by 2 Collections.rotate(list, 2);/*w w w . j a v a 2s . co m*/ System.out.println("After Rotating by 2: " + list); }
From source file:Main.java
public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("Java"); list.add("R"); list.add("CSS"); list.add("XML"); Collections.sort(list); System.out.println("List: " + list); Collections.shuffle(list);//from w ww . j a v a 2 s.co m System.out.println("List: " + list); Collections.shuffle(list); System.out.println("List: " + list); }
From source file:Main.java
public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("Java"); list.add("R"); list.add("CSS"); list.add("XML"); Collections.sort(list); System.out.println("List: " + list); int index = Collections.binarySearch(list, "CSS"); System.out.println("CSS in List is at " + index); index = Collections.binarySearch(list, "Javascript"); System.out.println("Javascript in List is at " + index); }
From source file:SearchTest.java
public static void main(String args[]) { String simpsons[] = { "Bart", "Hugo", "Lisa", "Marge", "Homer", "Maggie", "Roy" }; // Convert to list ArrayList list = new ArrayList(Arrays.asList(simpsons)); // Ensure list sorted Collections.sort(list); System.out.println("Sorted list: [length: " + list.size() + "]"); System.out.println(list);//from w ww . j a v a2 s .c o m // Search for element in list int index = Collections.binarySearch(list, "Maggie"); System.out.println("Found Maggie @ " + index); // Search for element not in list index = Collections.binarySearch(list, "Jimbo Jones"); System.out.println("Didn't find Jimbo Jones @ " + index); // Insert int newIndex = -index - 1; list.add(newIndex, "Jimbo Jones"); System.out.println("With Jimbo Jones added: [length: " + list.size() + "]"); System.out.println(list); }
From source file:Main.java
public static void main(String[] args) { ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add("1"); arrayList.add("4"); arrayList.add("2"); arrayList.add("5"); arrayList.add("3"); Collections.sort(arrayList); System.out.println("Sorted ArrayList contains : " + arrayList); int index = Collections.binarySearch(arrayList, "4"); System.out.println("Element found at : " + index); }
From source file:Main.java
public static void main(String[] args) { ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add("1"); arrayList.add("3"); arrayList.add("5"); arrayList.add("2"); arrayList.add("4"); Collections.sort(arrayList); for (String str : arrayList) System.out.println(str);//w ww . j av a2s . c o m }
From source file:MainClass.java
public static void main(String args[]) { String str[] = { "B", "H", "L", "M", "I", "N", "R" }; // Convert to list List list = new ArrayList(Arrays.asList(str)); // Ensure list sorted Collections.sort(list); System.out.println("Sorted list: [length: " + list.size() + "]"); System.out.println(list);//w w w . jav a 2s . c o m // Search for element in list int index = Collections.binarySearch(list, "M"); System.out.println("Found M @ " + index); // Search for element not in list index = Collections.binarySearch(list, "J"); System.out.println("Didn't find J @ " + index); // Insert int newIndex = -index - 1; list.add(newIndex, "J"); System.out.println("With J added: [length: " + list.size() + "]"); System.out.println(list); }
From source file:Main.java
public static void main(String[] args) { List<String> list = new LinkedList<String>(); list.add("a"); list.add("b"); list.add("c"); list.add("d"); list.add("e"); list.add("f"); Collections.sort(list); System.out.println(list);/*from w ww. j a v a2 s.co m*/ int index = Collections.binarySearch(list, "c"); if (index > 0) { System.out.println("Found at index = " + index); String month = (String) list.get(index); System.out.println(month); } }