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

Build HashSet from a vector in Rust

Because the operation does not need to consume the vector¹, I think it should not consume it. That only leads to extra copying somewhere else in the program: use std::collections::HashSet; use std::iter::FromIterator; fn hashset(data: &[u8]) -> HashSet<u8> { HashSet::from_iter(data.iter().cloned()) } Call it like hashset(&v) where v is a Vec<u8> or other thing that coerces to … Read more

How to retrieve actual item from HashSet?

This is actually a huge omission in the set of collections. You would need either a Dictionary of keys only or a HashSet that allows for the retrieval of object references. So many people have asked for it, why it doesn’t get fixed is beyond me. Without third-party libraries the best workaround is to use … Read more

Make HashSet case-insensitive

The HashSet<T> constructor has an overload that lets you pass in a custom IEqualityComparer<string>. There are a few of these defined for you already in the static StringComparer class, a few of which ignore case. For example: var set = new HashSet<string>(StringComparer.OrdinalIgnoreCase); set.Add(“john”); Debug.Assert(set.Contains(“JohN”)); You’ll have to make this change at the time of constructing … Read more

The HashSet.removeAll method is surprisingly slow

The behaviour is (somewhat) documented in the javadoc: This implementation determines which is the smaller of this set and the specified collection, by invoking the size method on each. If this set has fewer elements, then the implementation iterates over this set, checking each element returned by the iterator in turn to see if it … Read more

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