Loading topic
List and the Collections framework, and how equals and hashCode work. It covers the Comparable interface, what compareTo must return, and how the JDK uses it. The full rules of the comparison contra5 min readequals and hashCode. You should also have seen Comparable.compareTo and Comparator.compare at the signature level. Here you learn the rules both methods must obey, why sorting code relies on them, and how a violation sho6 min readequals and hashCode. It also assumes you can read a lambda or method reference. The goal is to make your own class sortable by default, so that Collections.sort(list), TreeSet and PriorityQueue work 6 min readComparable interface and the rules a comparison must follow, both covered in earlier chapters. Lambdas and the factory methods such as comparing get their own chapters. H6 min readComparable and the compare contract require. The earlier chapters cover those. Here you write Comparator instances in their shortest correct forms and learn where each form is safe to use.5 min readComparator is a function that takes two objects and returns a negative number, zero or a positive number. Writing that function by hand for every sort is tedious and easy to get wrong. This chapter covers the factory and combinator methods on Comparator that build 5 min readComparable class, how to build a Comparator with lambdas and factory methods, and how List and arrays work. Here you will apply those orderings with the sorting APIs the JDK provides. You will see what each API mutates, which orderings it accepts, a4 min readComparator with a lambda or method reference, and that you know Comparator.comparing, thenComparing and reversed. It also assumes you know how List.sort works. The chapter explains why nulls break a comparison and how Comparator.nullsFirst and `Compar4 min readComparator with the factory methods, you know the compare contract (sign of the result, consistency, transitivity), and you know how equals and hashCode drive HashSet and HashMap. Here you will see how TreeSet, TreeMap and PriorityQueue use orderi4 min readComparable class and a Comparator, and that you know the equals and hashCode contract. It covers a rule that is easy to miss: ordering and equality are two separate definitions of "the same", and some collections use one while others use the other.5 min readComparable class and a Comparator, and that you know the compare contract: the sign of the result, symmetry, transitivity, and consistency with equals. The bugs below all break that contract or the assumptions behind it. They are hard to find because the co5 min readList and TreeSet basics, and how equals and hashCode work. It also assumes you have read the earlier chapters on compareTo, compare, the comparison contract and the Comparator factor5 min read