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