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:MyComparator.java
public static void main(String args[]) { List<String> ts = new ArrayList<String>(); ts.add("tutorial"); ts.add("from"); ts.add("java2s.com"); Collections.sort(ts); for (String element : ts) { System.out.println(element + " "); }/*from w w w . j a va 2 s . co m*/ System.out.println(); Collections.sort(ts, new MyComparator()); for (String element : ts) { System.out.println(element + " "); } }
From source file:EmpComparator.java
public static void main(String args[]) { String names[] = { "Bart", "Hugo", "Lisa", "Marge", "Homer", "Maggie", "Roy" }; // Convert to list List list = new ArrayList(Arrays.asList(names)); // 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.co 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); // Min should be Bart System.out.println(Collections.min(list)); // Max should be Roy System.out.println(Collections.max(list)); Comparator comp = Collections.reverseOrder(); // Reversed Min should be Roy System.out.println(Collections.min(list, comp)); // Reversed Max should be Bart System.out.println(Collections.max(list, comp)); }
From source file:LogTest.java
public static void main(String[] args) throws IOException { String inputfile = args[0];// w ww .j a v a 2 s. com String outputfile = args[1]; Map<String, Integer> map = new TreeMap<String, Integer>(); Scanner scanner = new Scanner(new File(inputfile)); while (scanner.hasNext()) { String word = scanner.next(); Integer count = map.get(word); count = (count == null ? 1 : count + 1); map.put(word, count); } scanner.close(); List<String> keys = new ArrayList<String>(map.keySet()); Collections.sort(keys); PrintWriter out = new PrintWriter(new FileWriter(outputfile)); for (String key : keys) out.println(key + " : " + map.get(key)); out.close(); }
From source file:Student.java
public static void main(String[] args) { List<Student> al = new ArrayList<>(); al.add(new Student("Z", 34.34)); al.add(new Student("M", 123.22)); al.add(new Student("A", 1378.00)); al.add(new Student("D", 99.22)); al.add(new Student("Q", -19.08)); Collections.sort(al); System.out.println(al);/* ww w. jav a2 s . c om*/ }
From source file:Card.java
public static void main(String args[]) { List<Card> lc = new ArrayList<Card>(); lc.add(new Card(Card.Rank.SIX, Card.Suit.CLUBS)); lc.add(new Card(Card.Rank.TEN, Card.Suit.CLUBS)); lc.add(new Card(Card.Rank.SIX, Card.Suit.HEARTS)); lc.add(new Card(Card.Rank.ACE, Card.Suit.HEARTS)); System.out.println(lc);/* www. ja v a2 s . c o m*/ Collections.sort(lc); System.out.println(lc); }
From source file:ShuffleTest.java
public static void main(String[] args) { List<Integer> numbers = new ArrayList<Integer>(); for (int i = 1; i <= 49; i++) numbers.add(i);/*from w w w . j a v a2 s . com*/ Collections.shuffle(numbers); List<Integer> winningCombination = numbers.subList(0, 6); Collections.sort(winningCombination); System.out.println(winningCombination); }
From source file:Name.java
public static void main(String[] args) { Name name = new Name("A", "C"); Name name1 = new Name("A", "B"); Name name2 = new Name("C", "F"); Name name3 = new Name("D", "A"); Name name4 = new Name("Ca", "D"); Name name5 = new Name("Cc", "Z"); Name name6 = new Name("Cc", "W"); Name nameArray[] = { name, name1, name2, name3, name4, name5, name6 }; List<Name> names = new ArrayList<Name>(Arrays.asList(nameArray)); Collections.sort(names); for (Name n : names) { System.out.println(n.getFirstName() + " " + n.getLastName()); }// w w w . jav a 2s . c om }
From source file:Employee.java
public static void main(String[] args) { String[] names = { "A", "B", "C", "D" }; double[] salaries = { 2.0, 5.0, 6.0, 4.0 }; List l = new ArrayList(); for (int i = 0; i < names.length; i++) l.add(new Employee(names[i], salaries[i])); Collections.sort(l); ListIterator liter = l.listIterator(); while (liter.hasNext()) System.out.println(liter.next()); Collections.sort(l, new Employee.SalaryComparator()); liter = l.listIterator();// w w w . jav a 2 s.co m while (liter.hasNext()) System.out.println(liter.next()); }
From source file:com.discursive.jccook.xml.bardsearch.TermFreq.java
public static void main(String[] pArgs) throws Exception { logger.info("Threshold is 200"); Integer threshold = new Integer(200); IndexReader reader = IndexReader.open("index"); TermEnum enumVar = reader.terms();/* ww w . j av a2 s . c o m*/ List termList = new ArrayList(); while (enumVar.next()) { if (enumVar.docFreq() >= threshold.intValue() && enumVar.term().field().equals("speech")) { Freq freq = new Freq(enumVar.term().text(), enumVar.docFreq()); termList.add(freq); } } Collections.sort(termList); Collections.reverse(termList); System.out.println("Frequency | Term"); Iterator iterator = termList.iterator(); while (iterator.hasNext()) { Freq freq = (Freq) iterator.next(); System.out.print(freq.frequency); System.out.println(" | " + freq.term); } }
From source file:com.ifeng.sorter.NginxApp.java
public static void main(String[] args) { String input = "src/test/resources/data/nginx_report.txt"; PrintWriter pw = null;// www . j av a 2 s. co m Map<String, List<LogBean>> resultMap = new HashMap<String, List<LogBean>>(); List<String> ips = new ArrayList<String>(); try { List<String> lines = FileUtils.readLines(new File(input)); List<LogBean> items = new ArrayList<LogBean>(); for (String line : lines) { String[] values = line.split("\t"); if (values != null && values.length == 3) {// ip total seria try { String ip = values[0].trim(); String total = values[1].trim(); String seria = values[2].trim(); LogBean bean = new LogBean(ip, Integer.parseInt(total), seria); items.add(bean); } catch (NumberFormatException e) { e.printStackTrace(); } } } Collections.sort(items); for (LogBean bean : items) { String key = bean.getIp(); if (resultMap.containsKey(key)) { resultMap.get(key).add(bean); } else { List<LogBean> keyList = new ArrayList<LogBean>(); keyList.add(bean); resultMap.put(key, keyList); ips.add(key); } } pw = new PrintWriter("src/test/resources/output/result.txt", "UTF-8"); for (String ip : ips) { List<LogBean> list = resultMap.get(ip); for (LogBean bean : list) { pw.append(bean.toString()); pw.println(); } } } catch (IOException e) { e.printStackTrace(); } finally { pw.flush(); pw.close(); } }