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

Sorting a collection of objects [duplicate]

Collections.sort can be called with a custom comparator. And that comparator can be implemented to allow sorting in different sort orders. Here’s an example (for your Person model – with age as an Integer): public class FlexiblePersonComparator implements Comparator<Person> { public enum Order {Name, Age, Country} private Order sortingBy = Name; @Override public int compare(Person … Read more

Classical set operations for java.util.Collection

Intersection is done with Collection.retainAll; subtraction with Collection.removeAll; union with Collection.addAll. In each case, as Set will act like a set and a List will act like a list. As mutable objects, they operate in place. You’ll need to explicitly copy if you want to retain the original mutable object unmutated.

Why does Java let you cast to a collection? [duplicate]

It’s not because they’re collection classes, it’s because they’re interfaces. Foo doesn’t implement them, but subclasses of it could. So it’s not a compile-time error, since those methods may be valid for subclasses. At runtime, if this isn’t of a class that implements those interfaces, naturally it’s a runtime error. If you change List<String> to … Read more

ArrayList Vs LinkedList

Remember that big-O complexity describes asymptotic behaviour and may not reflect actual implementation speed. It describes how the cost of each operation grows with the size of the list, not the speed of each operation. For example, the following implementation of add is O(1) but is not fast: public class MyList extends LinkedList { public … Read more

Return multiple values from ES6 map() function

With using only one reduce() you can do this. you don’t need map(). better approach is this: const values = [1,2,3,4]; const newValues= values.reduce((acc, cur) => { return acc.concat([cur*cur , cur*cur*cur, cur+1]); // or acc.push([cur*cur , cur*cur*cur, cur+1]); return acc; }, []); console.log(‘newValues=”, newValues) EDIT: The better approach is just using a flatMap (as @ori-drori … Read more

Rule of thumb for choosing an implementation of a Java Collection?

I really like this cheat sheet from Sergiy Kovalchuk’s blog entry, but unfortunately it is offline. However, the Wayback Machine has a historical copy: More detailed was Alexander Zagniotov’s flowchart, also offline therefor also a historical copy of the blog: Excerpt from the blog on concerns raised in comments: “This cheat sheet doesn’t include rarely … Read more

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