
What I Learned Moving from Java OOP to Collections
A personal reflection on how learning Java Collections changed the way I think about data structures, object equality, uniqueness, and ordering. I share how concepts like Set, equals(), hashCode(), and Comparable became practical once I started applying them to real requirements.
When I first started learning Java, most of my attention was on object-oriented programming. I was thinking about classes, objects, constructors, inheritance, encapsulation, and how different parts of a program could interact with each other.
At that point, choosing how to store multiple objects did not feel like an important design decision. If I needed several values, my first thought was usually an array or an
ArrayList .Learning Collections changed that.
Instead of asking only “How can I store these objects?”, I started asking questions like:
- Can there be duplicates?
- Does the order matter?
- Do I need to search for something frequently?
- Should the elements be automatically sorted?
- What makes two objects equal?
Those questions made data structures feel less like syntax I had to memorize and more like decisions that communicate what a program is supposed to do.
Where I Started
Before learning Collections, I saw an
ArrayList as a solution for almost everything involving multiple objects.For example, imagine an application that stores usernames. I could easily write:
List<String> usernames = new ArrayList<>();
Technically, that works.
But what if the requirement says that every username must be unique?
Before studying Collections, I probably would have kept the list and manually checked whether the username was already there before adding it.
After learning about
Set , I realized that the data structure itself could express the rule:Set<String> usernames = new HashSet<>();
That was one of the first moments when Collections started making sense to me beyond just knowing their methods.
A
List says that duplicates can exist and that position may matter. A Set says that uniqueness matters.The choice itself communicates part of the business rule.
What Changed
The biggest change in my thinking was realizing that choosing a collection is not just about convenience.
It is about requirements.
I used to look at collections mainly by asking which methods they provided. Now I try to think about the behavior I need first.
If insertion order matters, I might need one structure. If uniqueness matters, I might need another. If I need key-value lookup, I should probably be thinking about a
Map . If I need elements to stay sorted, that introduces another decision.This made Collections feel much closer to problem solving than I expected.
Three Concepts That Finally Clicked
1. Set and uniqueness
Set was probably the easiest concept to understand theoretically: it does not allow duplicate elements.What became interesting was understanding why that matters in real programs.
Suppose I am storing the IDs of users who already completed an activity. If an ID appears twice, that second occurrence gives me no new information.
A
List could store those values, but a Set represents the requirement more clearly.Instead of writing extra logic to prevent duplicates, I can choose a structure where duplicates are already against the rules.
That changed the way I viewed data structures. Sometimes the best structure is not the one that simply can store the data. It is the one whose behavior matches the meaning of the data.
2. equals() and hashCode() became real
I had already seen
equals() and hashCode() while learning OOP, but for some time they felt theoretical.I knew they were related to comparing objects, but I did not fully understand why I would care so much about implementing them correctly.
Then I started using
HashSet .If I create two different objects that represent the same logical entity, should Java consider them duplicates?
For example, imagine two
Student objects representing a student with the same ID:Student student1 = new Student(10, "Anna");Student student2 = new Student(10, "Anna");
From a business perspective, I may want those objects to represent the same student.
But
HashSet cannot understand my business rules automatically.That is where
equals() and hashCode() stopped feeling like isolated OOP concepts. They directly affected how a collection behaved.I started understanding that defining equality is a design decision.
If I say that two students are equal when their IDs are equal, that decision can affect duplicate detection, searching, and the behavior of hash-based collections.
Suddenly, an OOP concept I had already studied was connected to something practical.
3. Comparable made more sense with TreeSet
I had a similar experience with
Comparable .At first, implementing
compareTo() felt like another Java interface I was supposed to learn.Then I saw how
TreeSet needed an ordering rule.If I have objects such as products, Java cannot automatically know whether I want them sorted by name, price, ID, or something else.
For example:
class Product implements Comparable<Product> { private String name; private double price;
@Override public int compareTo(Product other) { return name.compareTo(other.name); }}
Now the comparison has a purpose.
I am not implementing
Comparable just because Java has an interface called Comparable . I am defining what the natural ordering of my objects should be.That distinction helped the concept click for me.
One Mistake I Made
One mistake I made while learning Collections was focusing too much on the collection classes themselves.
I initially thought the challenge would be remembering things like:
add()remove()contains()
But those methods were not actually the difficult part.
The harder part was understanding the rules behind the data.
For example, when working with a
HashSet , knowing how to call add() is easy. Understanding why two objects that look identical might still both appear in the set is much more important.That problem forces you to think about object equality,
equals() , and hashCode() .The same thing happened with sorted collections. Calling
add() on a TreeSet is simple. Deciding how objects should be compared is the real design problem.Collections made me realize that knowing Java syntax and knowing how to model a problem are two different skills.
How I Think About Data Structures Now
I still have a lot to learn about Java Collections, but my starting question has changed.
Before, I would think:
“Which collection do I know how to use?”
Now I try to think:
“What properties does this data need?”
If duplicates are meaningful, a
List might make sense.If duplicates should never exist, I think about a
Set .If I need to associate one value with another, I think about a
Map .If ordering is part of the requirement, I ask what kind of ordering is needed and who should define it.
Learning Collections also helped connect topics that previously felt separate.
equals() , hashCode() , interfaces, generics, and object comparison started interacting with each other instead of existing as isolated Java concepts.That has probably been the most useful lesson for me so far.
Moving from OOP into Collections did not just teach me new classes from the Java API. It changed how I think about representing data and how much information a good data structure choice can communicate about the problem I am trying to solve.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article