r/javahelp • • May 07 '20

Codeless Java Developers: what are the common data structures you usually use on production

To professional java dev here, what are the common data structure youve been using and which data structure you strongly suggest for beginners, like me, to study carefully

40 Upvotes

38 comments sorted by

View all comments

5

u/MrSquicky May 07 '20

Everyone is going to tell you to use the the standard Collections framework. It's really good, to the point that I forget that it is not a built in part of languages and get frustrated when I'm working with languages that don't have it. You're mostly going to use Lists for basically extensible arrays and Maps for associated data, and sometimes Sets for when you want a unique collection of something and don't care about accessing by index (although you can with the the right subclass).

Coupled with that, learn how to use Streams, which are built to work very well with Collections.

A few other useful structures are:

  1. Map<Key, List<Values>> - great for grouping a list of items based on some criteria. I used to use Guava's Multimap for this, but with streams, I'm usually using the Collectors.groupingBy, which goes right to the Map and it is awesome. Also with streams, you can chain your Collectors.groupingBy to have as many layers of grouping that you want, so like Map<Year, Map<Month, Map<Region, Records>>> or whatever.

  2. A counting set or bag. Basically a map from objects to how many times it occurs. Guava's Multiset is still my go to for this.

  3. A Range. If you ever find yourself writing a startX and endX as separate properties in a model, you're probably doing something wrong. Ranges are a much superior solution, in large part because they encapsulate the logic you want for a range in them. Also really important, they can be used in things like RangeMaps, which let you register ranges for your keys but do lookups based on a single value. So, like you could create a scheduling thing that has an entry for 9:00-11:30, 2:00-2:15, 9:00am-5:00pm and then just enter a time and it will show you all the ongoing events. I think there are implementations out there, but I wrote my own for this that at some point or another I'll put up on Github because I was frustrated with what was out there.

  4. A nested set model. When I wrote this, I called it a boundary tree, but I've found that the accepted name is nested set. The idea here is that you can easily determine where something falls in a hierarchy by checking additional number boundary properties. So, each item has a range that marks its start and end and each level of the hierarchy is expanded when something is added to it. So, if I have an item that starts at say 60 and ends at 75, I can tell immediately what all of its parents are as they all have to contain 60 and I know that all of its children are contained in 60 - 75. This lets you do a single lookup instead of chaining lookup parent, lookup parent, lookup parent, etc.

---

Outside of those, if you're talking about data modelling, you want to consider your relationships and your total object graph.

These are both really large topics in and of themselves. The basic concept is to understand the semantic type of your relationships, not just their physical structure and consider how they make up coherent graphs of objects. I actually disagree with a fair bit of it, but Eric Evan's Domain Driven Design is still the best resource I've seen for understanding this.

1

u/knoam May 08 '20

I haven't used nested sets before, but otherwise this comment is like you read my mind.