ArrayList – How to modify a member of an object?
You can do this: myList.get(3).setEmail(“new email”);
You can do this: myList.get(3).setEmail(“new email”);
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
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
Mutable means you can alter the collection in-place. So, if you have a collection c and you append an element with +=, then c has changed, and so has every other reference to that collection. Immutable means that the collection object never changes; instead, you build new collection objects with operations such as + or … Read more
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.
How about: Map map1 = …; Map map2 = …; Map result = new …(map1); result.keySet().retainAll(map2.keySet()); or: Map map1 = …; Map map2 = …; Set result = new …(map1.keySet()); result.retainAll(map2.keySet());
I think this is more of a style question, but lets give some thoughts: It seems to be common practice to not use such a CONST collector object. In that sense: doing so might surprise some readers, and surprising readers is rarely a good thing to do. Then: few code can be just “copied” around … Read more
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
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
Simply use : mapA.equals(mapB); Compares the specified object with this map for equality. Returns true if the given object is also a map and the two maps represent the same mappings