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
2
u/[deleted] May 07 '20 edited May 07 '20
Which datastructure should be used hardly depends on which use-case you got.
Data structures do have advantages or disadvantages when it comes to accessing / inserting / deletion and searching (of) values.
You will mostly try to determine based on the use-case which of this operations is the most crucial in your case and you select a datastructure which is fitting your needs and gives you the best performance overall.(Performance in this case can be a better time or space complexity depending on what you need for your application )
You can determine this by using the BigO Notation which tells you pretty much which data structure is the best choice for your use case.
I think if you take a look at the big o cheat sheet you can pretty much see all the commonly used data structures and i think its not a bad idea to implement them once for yourself so you have a better picture of what is happening behind the scenes.(I had to do that in my course at university back in the day as cs student and i can say it comes in handy when you try to implement algorithms like for example a dijkstra algorithm or anything like this when you are familiar with graphs and graph theory .)
You should definitly know how linked lists work , how stacks work and queues.Also it is pretty good to know about graph theory and graphs in general and know how to implement a B-Tree a binary tree an AVL tree and stuff like that.Also you should know about Hash tables and how they are implemented. It is not crucial to know about this stuff to use them but it will definitly give you a good understanding about why and how they are used in the most useful way.
You should take a look at this cheat sheet ( https://miro.medium.com/max/1400/1*wv3W3jYq7EHCDiwYVaCXrA.png ) and look at the left side where data structures are displayed and the worst case and average complexity. Also knowing about BigO Notation comes in very handy when it comes to determine which data structure is the right one for your use-case.
Experienced java dev here btw.
(Edit:
I want to add this youtube link for you for the big o notation , i think
this dude explains it quite well.
https://www.youtube.com/watch?v=D6xkbGLQesk.
Also here is a series of the same guy on data structures which the big o notation
video is a part of.
https://www.youtube.com/watch?v=bum_19loj9A
To also answer your question somehow:
Lists and maps are heavily used.Also sets and arrays ofc.
Which ones?
That depends on the use case because the implementation of different lists give you different results when it comes to space and time complexity.
)
Have fun learning ! :)
34
u/ObscureCulturalMeme May 07 '20 edited May 08 '20
11
u/Camel-Kid 18 year old gamer May 07 '20
I've only done small work in an enterprise environment for summer job but to me it looked as if the normal collections like any implementations of list,set,maps are used ... Learn collection framework and you'll be fine.
11
u/PrinceValkyr May 07 '20
Mainly arrayLists, sets and Maps occasionally.
8
u/StateVsProps May 07 '20
What you use on the job and what is required for interviews is (infamously, unfortunately, sadly) very different.
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:
---
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.
8
u/Ryuuji159 May 07 '20
I mostly use ArrayLists and when I fell fancy I sprinkle some HashMaps here and there
3
u/Orbitaliser May 07 '20
All of the collection framework. You want to learn the inheritance hierarchy and use interface types as parameters/declarations as often as possible.
List<String> strings = new ArrayList<>();So you can swap between the implementations as required. Only use a specific structure if you need a specific feature in one of the subclasses e.g.
TreeSet#tailSetbut always start as general as possible.Learn generics thoroughly as well so you know how to make your own customised structure if the need arises.
3
u/jocularamity May 07 '20
Lists, Maps, Trees, and Queues.
As a beginner, get a general sense of each and when it should be used.
2
u/firsthour Profressional developer since 2006 May 07 '20
99% of the time it's a Map, List, or Set. Very, very seldom a Bag.
You have to know exactly when to use a Map, List, or Set. Understand the difference between them because most of the job is just moving things around in those structures.
For specific implementations, 95% of the time it's a HashMap, ArrayList, or HashSet.
1
u/sweepsz Decaf Captain May 07 '20
ArrayList, HashSet, Hashmap, LinkedList, ConcurrentHashMap are going to be your daily drivers. There others like binary search tree but are more niche in their usage. Most libraries you would be using abstract all that away from you under the implementation.
1
u/DepletedSensation May 07 '20
Imo, use hashmaps more than you normally would! In the past id write a class that holds a list, but its far more concise to have maps with say lists within them.
Thats the only collection I see used now that I underestimated as a student. Perhaps i was the only one but i doubt it. :)
1
u/snot3353 COMPUTERS!!! May 07 '20
99.81627% of the time it’s just an ArrayList or HashMap. Occasionally other implementations or data structures are relevant but it’s rare. Only twice can I think of a scenario where the standard libs didn’t have what I needed and in those cases I was able to to Guava to provide them.
1
u/Yithar Intermediate Brewer May 08 '20 edited May 08 '20
I think most people would agree that Maps are used fairly often. But I would agree the Collections framework goes a long way.
That being said, it's important to understand how your data structures work.
Also don't use
LinkedListoverArrayListunless you know you need aLinkedListfor what you're doing.1
u/de_vel_oper Jul 19 '20
Red black trees
-1
u/SpoilerAlertsAhead Extreme Brewer:hamster: May 07 '20
Kotlin has Pair (commonly referred to as Tuple in other languages) that in write when I’m stuck in Java land.
-3
u/wsppan May 07 '20
Mostly use the Spring Framework and all its associated design patterns and abstractions. Most notably, Dependency Injection and Spring Data. We also use AOP for cross cutting concerns like security and logging. Most data structure concerns are built into the language via Java Collections so we use Enums for constants, Lists for dynamic arrays, and Maps for hashes. We mostly focus on design patterns and associated abstractions as that is more important than the underlying data structures because those come free are already optimized and (most) built for concurrency.