List of usage examples for java.util TreeSet TreeSet
public TreeSet(SortedSet<E> s)
From source file:Main.java
public static void main(String[] argv) throws Exception { List stuff = Arrays.asList(new String[] { "a", "b" }); List list = new ArrayList(stuff); List list2 = new LinkedList(list); Set set = new HashSet(stuff); Set set2 = new TreeSet(set); Map map = new HashMap(); Map map2 = new TreeMap(map); }
From source file:MainClass.java
public static void main(String[] args) { // Create two sets. Set s1 = new HashSet(); s1.add("A");//from w ww. ja va 2 s . com s1.add("B"); s1.add("C"); Set s2 = new HashSet(); s2.add("A"); s2.add("B"); Set union = new TreeSet(s1); union.addAll(s2); // now contains the union print("union", union); Set intersect = new TreeSet(s1); intersect.retainAll(s2); print("intersection", intersect); }
From source file:ItemSet.java
public static void main(String args[]) { String names[] = { "Item 1", "Item 2", "Item 3" }; Set moons = new HashSet(); int namesLen = names.length; int index;/*from ww w . j a va 2 s.c o m*/ for (int i = 0; i < 100; i++) { index = (int) (Math.random() * namesLen); moons.add(names[index]); } Iterator it = moons.iterator(); while (it.hasNext()) { System.out.println(it.next()); } System.out.println("---"); Set orderedMoons = new TreeSet(moons); it = orderedMoons.iterator(); while (it.hasNext()) { System.out.println(it.next()); } }
From source file:Main.java
public static void main(String[] a) { Map<String, String> yourMap = new HashMap<String, String>(); yourMap.put("1", "one"); yourMap.put("2", "two"); yourMap.put("3", "three"); Map<String, Object> map = new LinkedHashMap<String, Object>(); List<String> keyList = new ArrayList<String>(yourMap.keySet()); List<String> valueList = new ArrayList<String>(yourMap.values()); Set<String> sortedSet = new TreeSet<String>(valueList); Object[] sortedArray = sortedSet.toArray(); int size = sortedArray.length; for (int i = 0; i < size; i++) { map.put(keyList.get(valueList.indexOf(sortedArray[i])), sortedArray[i]); }/*from w w w . java2s .c om*/ Set ref = map.keySet(); Iterator it = ref.iterator(); while (it.hasNext()) { String i = (String) it.next(); System.out.println(i); } }
From source file:Main.java
public static void main(String[] args) { Comparator<String> ipComparator = new Comparator<String>() { @Override//www .j a v a 2 s . com public int compare(String ip1, String ip2) { return toNumeric(ip1).compareTo(toNumeric(ip2)); } }; SortedSet<String> ips = new TreeSet<String>(ipComparator); ips.addAll(Arrays.asList("192.168.0.1", "192.168.0.2", "192.168.0.9", "9.9.9.9", "127.0.0.1")); System.out.println(ips); }
From source file:Company.java
public static void main(String args[]) { Employee emps[] = { new Employee("Finance", "Degree, Debbie"), new Employee("Finance", "Grade, Geri"), new Employee("Finance", "Extent, Ester"), new Employee("Engineering", "Measure, Mary"), new Employee("Engineering", "Amount, Anastasia"), new Employee("Engineering", "Ratio, Ringo"), new Employee("Sales", "Stint, Sarah"), new Employee("Sales", "Pitch, Paula"), new Employee("Support", "Rate, Rhoda"), }; Set set = new TreeSet(Arrays.asList(emps)); System.out.println(set);// w w w. j a va 2 s. co m Set set2 = new TreeSet(Collections.reverseOrder()); set2.addAll(Arrays.asList(emps)); System.out.println(set2); Set set3 = new TreeSet(new EmpComparator()); for (int i = 0, n = emps.length; i < n; i++) { set3.add(emps[i]); } System.out.println(set3); }
From source file:Main.java
public static void main(String args[]) { Employee emps[] = { new Employee("Finance", "Degree, Debbie"), new Employee("Finance", "Grade, Geri"), new Employee("Finance", "Extent, Ester"), new Employee("Engineering", "Measure, Mary"), new Employee("Engineering", "Amount, Anastasia"), new Employee("Engineering", "Ratio, Ringo"), new Employee("Sales", "Stint, Sarah"), new Employee("Sales", "Pitch, Paula"), new Employee("Support", "Rate, Rhoda"), }; SortedSet set = new TreeSet(Arrays.asList(emps)); System.out.println(set);//from w w w . j a v a 2s . c o m try { Object last = set.last(); boolean first = true; while (true) { if (!first) { System.out.print(", "); } System.out.println(last); last = set.headSet(last).last(); } } catch (NoSuchElementException e) { System.out.println(); } Set subset = set.headSet(emps[4]); subset.add(emps[5]); }
From source file:UsingFontFactoryPDF.java
public static void main(String[] args) { Document document = new Document(); try {//from w ww.j av a 2 s.c o m PdfWriter.getInstance(document, new FileOutputStream("UsingFontFactoryPDF.pdf")); document.open(); Paragraph p = new Paragraph("Font Families", FontFactory.getFont(FontFactory.HELVETICA, 16f)); document.add(p); FontFactory.registerDirectories(); TreeSet families = new TreeSet(FontFactory.getRegisteredFamilies()); for (Iterator i = families.iterator(); i.hasNext();) { p = new Paragraph((String) i.next()); document.add(p); } } catch (Exception e) { System.err.println(e.getMessage()); } document.close(); }
From source file:Person.java
public static void main(String args[]) { Person emps[] = { new Person("Debbie", "Degree"), new Person("Geri", "Grade"), new Person("Ester", "Extent"), new Person("Mary", "Measure"), new Person("Anastasia", "Amount") }; Set set = new TreeSet(Arrays.asList(emps)); System.out.println(set);// w w w . j a v a 2 s .c o m }
From source file:Employee.java
public static void main(String args[]) { Employee emps[] = { new Employee("Finance", "A"), new Employee("Finance", "B"), new Employee("Finance", "C"), new Employee("Engineering", "D"), new Employee("Engineering", "E"), new Employee("Engineering", "F"), new Employee("Sales", "G"), new Employee("Sales", "H"), new Employee("Support", "I"), }; Set set = new TreeSet(Arrays.asList(emps)); System.out.println(set);/* w w w. ja v a2 s. co m*/ }