TreeSet giving incorrect output – Java8

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

Is it possible that TreeSet equals HashSet but not HashSet equals TreeSet

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

maintaining TreeSet sort as object changes value

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 vs Treeset

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)