#a2

Deep Dive: All about Exceptions

Overview Exceptions are a mechanism for java code to signal extraordinary conditions (such as virtual machine errors like running out of memory, code bugs such as passing a negative number to a method that doesn’t accept negative numbers, alternative ways a method can exit, such as an attempt to read from a network socket aborting because the other end severed the connection, and even intentional control flow such as aborting a thread ASAP). They...

21 minute(s) read

Finding hash collisions in Java Strings

In ##java, a question came up about generating unique identifiers based on Strings, with someone suggesting that hashCode() would generate generally usable numbers, without any guarantee of uniqueness. However, duplicate hash codes can be generated from very small strings, with ordinary character sets – see this stackoverflow answer – and therefore I thought it’d be interesting to find other short strings with the same hash values. It was discussed how prone to collisions the Strings hashCode...

2 minute(s) read

Guava's Equivalence: strategy for equals()/hashCode()

Introduction Given a class C, if you want to implement ordering, you have two choices: Have your class implement Comparable<c> (or Comparable<x>, where X is a superclass of C). Define a Comparator<c> to use with this class (or a Comparator<x>, where X is a superclass of C). In fact, many classes in the JDK will have you supply a Comparator if your class...

4 minute(s) read

PriorityQueue and mutable item behavior

From today, in Java: a user asked if changing the value of an item in a PriorityQueue would still result in the queue being correctly ordered. (He explained that he was doing this to prioritize based on counters, which makes sense. It’s worth noting that this blog entry is not about solving his problem, but about the behavior of PriorityQueue.) Getting items out of a PriorityQueue requires peek() and/or poll(). Let’s take...

4 minute(s) read