{}const=>[]async()letfn</>var
BasicsJS

5 real mistakes when working with collections that juns make

Collections are the foundation in Java. List, Set, Map sound simple, but in fact beginners often fall into traps. In this article, we will analyze 5 real mistakes and tell you how not to repeat them.

К

Kodik

Author

2 min read

If you're just starting to learn Java, collections like List, Set, Map may seem simple... until they start to break your head 😅
In Kodik app we explain step by step how to work with them correctly, without pain and bugs.
And in this article, we will analyze 5 real mistakes that beginners often make. Read, find out - and don't step on these rakes yourself 💡

Collections are the foundation in Java. List, Set, Map sound simple, but in fact beginners often fall into traps. Below are five situations that I have encountered in life, in courses and in the code of beginners. And most importantly, how to avoid them.

1. ❌ Removing items from the List inside the loop

Error: Use the for-each loop and delete items from the list in it.
This causes ConcurrentModificationException.

How to avoid: Use Iterator and iterator.remove().

🔥 100,000+ students already with us

Tired of reading theory?
Time to code!

Kodik — an app where you learn to code through practice. AI mentor, interactive lessons, real projects.

🤖 AI 24/7
🎓 Certificates
💰 Free
🚀 Start learning
Joined today

2. ❌ Using == instead of .equals()

Error: Check the equality of objects through == instead of .equals().
As a result, list.contains() or remove() may not work, even if the values are the same.

How to avoid: Always use .equals() to compare objects.

3. ❌ HashMap without overriding hashCode()

Error: Keys in HashMap are objects that do not have equals() and hashCode() overridden.
As a result, get() returns null, even if the key seems to be the same.

How to avoid: Override equals() and hashCode() in the key class.

4. ❌ Waiting for order from HashSet

Error: Assume that the elements in HashSet will be displayed in the order they are added.
But HashSet does not guarantee any order.

How to avoid: If you need order, use LinkedHashSet or TreeSet.

5. ❌ NullPointerException due to uninitialized collection

Error: Try to add items to the null collection.

How to avoid: Immediately initialize collections, for example: List<String> names = new ArrayList<>();

Everyone makes mistakes with collections — but it's better to learn from others. Here's a quick cheat sheet:

  • Remove from List — use Iterator

  • Compare objects — always through .equals()

  • Working with Map — override equals and hashCode

  • Expect order — choose the correct implementation of the collection

  • Don't want to catch NullPointerException — don't forget about initialization

In Kodik app you will go through step-by-step lessons, solve problems, create projects and learn how to work with collections and other important Java tools.
And we also have Telegram community, where you can ask a question, get help and just not feel like a lonely beginner.

🎯Stop procrastinating

Liked the article?
Time to practice!

In Kodik, you don't just read — you write code immediately. Theory + practice = real skills.

Instant practice
🧠AI explains code
🏆Certificate

No registration • No card