Converting a TreeSet to ArrayList?
How about this: new ArrayList<T>(set); For Java 7 and later, this can be simplified, as type arguments <T> can be replaced with diamond type <>: new ArrayList<>(set);
How about this: new ArrayList<T>(set); For Java 7 and later, this can be simplified, as type arguments <T> can be replaced with diamond type <>: new ArrayList<>(set);
Why do you think this approach won’t be optimized? The reverse order Comparator is simply going to be flipping the sign of the output from the actual Comparator (or output from compareTo on the Comparable objects being inserted) and I would therefore imagine it is very fast. An alternative suggestion: Rather than change the order … Read more
What would you expect a get() method on a Set to do? Sets are not indexed, so a get(int index) makes no sense. (Use a List if you want to get elements by index). get(Object obj) would also not make sense, because you’d have the object that you’re trying to get already. There is already … Read more
The Python 2.7 docs for collections.OrderedDict has a link to a OrderedDict recipe that runs on Python 2.4 or better. Edit: In regard to sorting: Use key= rather than cmp=. It tends to lead to faster code and moreover, the cmp= keyword has been eliminated in Python3. d={5:6,7:8,100:101,1:2,3:4} print(d.items()) # [(1, 2), (3, 4), (100, … Read more
I poked around TreeSet and its interfaces for a while, and the best way I found to get the index of an element is: set.headSet(element).size() headSet(element) returns the sub-TreeSet of elements less than its argument, so the size of this set will be the index of the element in question. A strange solution indeed.
This happens because a SortedSet’s Comparator is used for sorting, but removeAll relies on the equals method of each element. From the SortedSet documentation: Note that the ordering maintained by a sorted set (whether or not an explicit comparator is provided) must be consistent with equals if the sorted set is to correctly implement the … Read more
Your interviewer is right, they do not hold equivalence relation for some specific cases. It is possible that TreeSet can be equal to HashSet and not vice-versa. Here is an example: TreeSet<String> treeSet = new TreeSet<>(String.CASE_INSENSITIVE_ORDER); HashSet<String> hashSet = new HashSet<>(); treeSet.addAll(List.of(“A”, “b”)); hashSet.addAll(List.of(“A”, “B”)); System.out.println(hashSet.equals(treeSet)); // false System.out.println(treeSet.equals(hashSet)); // true The reason for this … Read more
As others have noted, there is no in-built way. But you can always subclass that TreeSet, with your constructor(s) of choice, and add in the required functionality: public class UpdateableTreeSet<T extends Updateable> extends TreeSet<T> { // definition of updateable interface Updateable{ void update(Object value); } // constructors here … // ‘update’ method; returns false if … Read more
HashSet is much faster than TreeSet (constant-time versus log-time for most operations like add, remove and contains) but offers no ordering guarantees like TreeSet. HashSet the class offers constant time performance for the basic operations (add, remove, contains and size). it does not guarantee that the order of elements will remain constant over time iteration … Read more