-3

I understand that this question is very vague, but I'm trying to figure out a thought process to find out which collection is better to use when it comes to managing data.

I guess I'm just wondering if there is anything that screams "USE THIS ONE!", such as:

Big Data sets, lots of insertion and searching for a specific value: Best Collection Type
Big Data sets, sorting and data manipulation: Best Collection Type
Big Data sets, sorted by last add: Best Collection Type
Big Data sets, delete or move an item after x about of time: Best Collection Type
etc...

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 4
    1. use `ArrayList`, you'll be right more often than not. 2. Need to look up based on anything other than the index of an entry? Use a `HashMap`. 3. Those two will get you surprisingly far. – Joachim Sauer Nov 05 '20 at 19:34
  • The key here is to avoid maintaining parallel collections. As a general rule, the kinds of data you have and how they relate should determine your collection type, not what you want to do with it. You can do just about any operation on just about any collection type. – Charlie Armstrong Nov 05 '20 at 19:45
  • Step 1 to finding "Best Collection Type" is to identify the *candidates*. If there is only one candidate, then it is by nature the "best" choice. – Andreas Nov 05 '20 at 20:14
  • Ahhh so its not as much about what operations I want to do as much as how the data relates to each one another – Olivia Crawford Nov 05 '20 at 20:30
  • As indicated by @JoachimSauer each collection-type has it's strength (e.g. lookup or indexed) / weaknesses (e.g. performance, duplication) and must be fitted to your use-case. A standard book about "datastructures" may teach you in theory. You'll learn which for what, after reading and writing code in practice. The last 5 years I also saw `ArrayList`, `HashMap`, `HashSet` most frequently (in this order; together I saw them in ~80% of Java `Collection` implementations). – hc_dev Nov 05 '20 at 21:16

1 Answers1

1

Little research

Assuming you are asking for decision-support, rather than for an opinionated "best-practice", I did a little research to find guidelines, comparisons, or even decision-charts.

Articles explaining the benefits and usage of collections

Here you find each of the major Java Collections explained: Java Collections Cheat Sheet.

Here you find a list of questions to ask in order to choose the right one, as well as best-practice for using collections and their methods: 18 Java Collections and Generics Best Practices

Articles containing flow-charts for collection decision

I also found some decision flow-charts at similar question Rule of thumb for choosing an implementation of a Java Collection?, e.g. originally from Sergiy Kovalchuk's Blog:

Guide to Selecting Appropriate Map/Collection in Java

Or viewed from different perspectives in LogicBic's tutorial Java - Collection Interfaces and Implementations

Maybe some SO-members would like to share their most applied Collections, with practical use-cases or experience from the professional field.

hc_dev
  • 8,389
  • 1
  • 26
  • 38