I need to store certain id’s , and check if one exists there.
Either i can use concatenated string or array/List, which of them is a better and faster way.
This is how actually data is organized :
- Year 1
- Month 1
- Day 1
- Day 2
- Day 3
- Month 2
- Day 6
- Day 2
- Day 3
- Month 1
- Year 2
- Month 3
- Day 1
- Day 3
- Day 7
- Month 6
- Day 6
- Day 2
- Day 3
- Month 3
I would definitely use a collection of some form. If you only care about containment, you should use a
Set<String>of some kind (e.g.HashSet<String>orLinkedHashSet<String>, which will both give O(1) complexity unless you have a significant number of hash collisions) but for goodness’ sake don’t use a concatenated string.Your data isn’t naturally a concatenated string – it’s a collection of strings. Always keep your data in the most natural representation unless you have really good evidence that some alternative form (such as a single string) will bring you a meaningful benefit. Keeping your data in a natural representation almost always leads to clearer code which is easier to work with – and easier to optimize later, when you’ve found where the real bottlenecks are.