Posts

Showing posts from April, 2013

Consistent with equals and unique values in Set

So the other day one of my friends was trying to solve a problem and he decided to share his problem with me. So based on my understanding of what he was trying to do was, that he had 2 disparate groups of data stored in Lists, the two lists were likely to have duplicates. His objective was to consolidate the two lists such that the consolidated list would only have unique entries. The lists had custom data structures, so straightforward methods of adding elements to a Set by itself wasn't enough, nor was it the situation where you add all the elements to one big list and somehow use the removeAll method. My first reaction to this problem was, hmm why not exploit the consistent with equals concept with a Comparator . Now this may or may not be a good solution, it was a fun one for me to knock up and it builds on my last post about Sorting in Java. The problem Say we are getting two streams of data, which may or may not contain duplicates and our goal is to get data that is ...

Fun with sorting (Java)

So every once in a while my boss makes an interesting sorting request, something like, i want the list of users sorted by locked, then production, then date joined and then name. Now sorting is generally a fun little problem in any capacity and some of the fancier requests from my boss make it even more interesting. So as with most other things, even for sorting the general rule to only write something from scratch if there is absolutely nothing out there applies. I mean seriously even on those really boring days in the office where you are doing the most repetitive of tasks, it is generally a good idea to resist the urge to make things more interesting by writing something from scratch or worse configuring your servers to support Gopher . Ok back to the point, so.....sorting in Java is pretty easy actually, all you need to do is call the Collections.Sort() method to a collection of objects that implement Comparable or better pass a specific ...