Why and when to use TreeMap

Let’s say you want to implement a dictionary and print it in alphabetical order, you can use a combination of a TreeMap and a TreeSet: public static void main(String args[]) { Map<String, Set<String>> dictionary = new TreeMap<>(); Set<String> a = new TreeSet<>(Arrays.asList(“Actual”, “Arrival”, “Actuary”)); Set<String> b = new TreeSet<>(Arrays.asList(“Bump”, “Bravo”, “Basic”)); dictionary.put(“B”, b); dictionary.put(“A”, a); … Read more

Java TreeMap Comparator

You can not sort TreeMap on values. A Red-Black tree based NavigableMap implementation. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used You will need to provide comparator for Comparator<? super K> so your comparator should compare … Read more

Different font sizes in the same annotation of matplotlib

Make your plot first, then use ax.annotate, iterating over your x coordinates, y coordinates, labels and fontsizes. import matplotlib.pyplot as plt X = [1,2,3,4,5] Y = [1,1,1,1,1] labels=”ABCDE” sizes = [10, 15, 20, 25, 30] fig, ax = plt.subplots() ax.scatter(X, Y) for x, y, label, size in zip(X, Y, labels, sizes): ax.annotate(label, (x, y), fontsize=size) … Read more

How to use SortedMap interface or TreeMap in Java?

I would use TreeMap, which implements SortedMap. It is designed exactly for that. Example: Map<Integer, String> map = new TreeMap<Integer, String>(); // Add Items to the TreeMap map.put(1, “One”); map.put(2, “Two”); map.put(3, “Three”); // Iterate over them for (Map.Entry<Integer, String> entry : map.entrySet()) { System.out.println(entry.getKey() + ” => ” + entry.getValue()); } See the Java … Read more

Why is there not a concurrent TreeMap? [closed]

Why there is the non-concurrent TreeMap on one side and the ConcurrentSkipListMap on one other? I suspect this was done because making a tree structure concurrent was too difficult or suffered from locking performance problems. In terms of ordered collections, SkipLists are very simple data structures and provide similar behavior and performance to trees so … Read more

Which data structure would you use: TreeMap or HashMap? (Java) [duplicate]

TreeMap seems a no-brainer to me – simply because of the “in alphabetical order” requirement. HashMap has no ordering when you iterate through it; TreeMap iterates in the natural key order. EDIT: I think Konrad’s comment may have been suggesting “use HashMap, then sort.” This is good because although we’ll have N iterations initially, we’ll … Read more

Java List Sorting: Is there a way to keep a list permantly sorted automatically like TreeMap?

You can change the behaviour of ArrayList List<MyType> list = new ArrayList<MyType>() { public boolean add(MyType mt) { super.add(mt); Collections.sort(list, comparator); return true; } }; Note: a PriorityQueue is NOT a List, if you didn’t care what type of collection it was, the simplest would be to use a TreeSet, which is just like a … Read more

tech