Sort a List of objects by multiple fields [duplicate]

Your Comparator would look like this: public class GraduationCeremonyComparator implements Comparator<GraduationCeremony> { public int compare(GraduationCeremony o1, GraduationCeremony o2) { int value1 = o1.campus.compareTo(o2.campus); if (value1 == 0) { int value2 = o1.faculty.compareTo(o2.faculty); if (value2 == 0) { return o1.building.compareTo(o2.building); } else { return value2; } } return value1; } } Basically it continues comparing each … Read more

Do Collections.unmodifiableXXX methods violate LSP? [closed]

LSP says that every subclass must obey the same contracts as the superclass. Wether or not this is the case for Collections.unmodifiableXXX() thus depends on how this contract reads. The objects returned by Collections.unmodifiableXXX() throw an exception if one tries to call any modifying method upon them. For instance, if add() is called, an UnsupportedOperationException … Read more

IEnumerable and order

IEnumerable/IEnumerable<T> makes no guarantees about ordering, but the implementations that use IEnumerable/IEnumerable<T>may or may not guarantee ordering. For instance, if you enumerate List<T>, order is guaranteed, but if you enumerate HashSet<T> no such guarantee is provided, yet both will be enumerated using the IEnumerable<T> interface.

Empty an ArrayList or just create a new one and let the old one be garbage collected? [duplicate]

The advantage of recycling an ArrayList (e.g. by calling clear) is that you avoid the overhead of allocating a new one, and the cost of growing it … if you didn’t provide a good initialCapacity hint. The disadvantages of recycling an ArrayList include the following: The clear() method has to assign null to each (used) … Read more

Union or intersection of Java Sets

The simplest one-line solution is this: set1.addAll(set2); // Union set1.retainAll(set2); // Intersection The above solution is destructive, meaning that contents of the original set1 my change. If you don’t want to touch your existing sets, create a new set: var result = new HashSet<>(set1); // In Java 10 and above Set<Integer> result = new HashSet<>(set1); … Read more

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