import java.io.File; import java.io.FileNotFoundException; import java.util.*; public class TreeSetExample { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(System.in); boolean selection = input.nextBoolean(); Set nums = new TreeSet<>(selection ? new NodeComparator() : new StringComparator()); Map numbers = new TreeMap<>(new IntComparator()); int[] mapKeys = { 1, 2, 3, 4, 5, 6 }; int[] mapValues = { 1, 2, 3, 3, 2, 1 }; int[] values = new int[] { 3, 1, 10, 9, 2, 11 }; String[] names = new String[] { "Chicago ", "Seattle ", "Portland ", "New York ", "LA ", "Vancouver" }; boolean[] markers = new boolean[] { false, false, true, false, false, true }; for (int i = 0; i < values.length; i++) { nums.add(new Node(names[i], values[i], markers[i])); } for (Node elem : nums) { System.out.println(elem); } for (int i = 0; i < mapKeys.length; i++) { numbers.put(mapKeys[i], mapValues[i]); } numbers.entrySet().forEach(entry -> System.out.println(entry.getKey() + " => " + entry.getValue())); for (Map.Entry entry : numbers.entrySet()) { System.out.println(entry.getKey() + " => " + entry.getValue()); } } static class StringComparator implements Comparator { @Override public int compare(Node nodeOne, Node nodeTwo) { return nodeTwo.name.compareTo(nodeOne.name); } } static class NodeComparator implements Comparator { @Override public int compare(Node nodeOne, Node nodeTwo) { return nodeTwo.value - nodeOne.value; } } static class IntComparator implements Comparator { @Override public int compare(Integer o1, Integer o2) { return o2 - o1; } } }