Author Archives: Stanislav_Panteleev

Java.Core.How can you get synchronized objects of standard collections?

When you want to make standard Java collections thread-safe, you can use synchronized wrappers provided by the Collections utility class. Let’s go through how to get synchronized (thread-safe) versions of standard collections like List, Set, Map, and SortedMap.

Posted in Без рубрики | Comments Off on Java.Core.How can you get synchronized objects of standard collections?

Java.Collections.What are the ways to iterate over list elements?

✅ 1. Enhanced for loop (for-each loop) Clean and readable Can’t modify the list (no remove() here)

Posted in Без рубрики | Comments Off on Java.Collections.What are the ways to iterate over list elements?

Java.Collections.There is a special class for Enum. java.util.EnumSet. Why? Why were the authors not satisfied with HashSet or TreeSet?

🧠 EnumSet is a high-performance, memory-efficient set specifically optimized for use with enums. Let’s dive into why the authors of Java created it instead of just using HashSet or TreeSet.

Posted in Без рубрики | Comments Off on Java.Collections.There is a special class for Enum. java.util.EnumSet. Why? Why were the authors not satisfied with HashSet or TreeSet?

Java.Collections.How does LinkedHashSet differ from HashSet?

🌟 Let’s compare LinkedHashSet vs HashSet, since they’re closely related — but behave differently in one key way. 🔍 ✅ Quick Answer: LinkedHashSet is just like HashSet, but it maintains insertion order of the elements.

Posted in Без рубрики | Comments Off on Java.Collections.How does LinkedHashSet differ from HashSet?

Java.Collections.What happens if you add elements to a TreeSet in ascending order?

🧠 Quick Answer: If you add elements to a TreeSet in ascending order, it still works perfectly, and the set remains balanced internally, because TreeSet uses a red-black tree, which self-balances on every insertion.

Posted in Без рубрики | Comments Off on Java.Collections.What happens if you add elements to a TreeSet in ascending order?

Java.Collections.What is the difference between TreeSet and HashSet?

🌳 vs 🧩 — TreeSet and HashSet are both implementations of the Set interface in Java, but they have very different behaviors under the hood. Let’s compare them clearly and concisely:

Posted in Без рубрики | Comments Off on Java.Collections.What is the difference between TreeSet and HashSet?

Java.Collections.How to iterate over all key-value pairs in a Map?

Let’s wrap it up with the classic: iterating over all key-value pairs in a Map<K, V>. ✅ 1. Enhanced for loop using entrySet() (most common way) map.entrySet() returns a Set<Map.Entry<K, V>> Each Map.Entry holds both a key and a value.

Posted in Без рубрики | Comments Off on Java.Collections.How to iterate over all key-value pairs in a Map?

Java.Collections.How to iterate over all values ​​in a Map?

✅ 1. Enhanced for loop using map.values() map.values() returns a Collection<V> of all the values. This is the simplest and most common way.

Posted in Без рубрики | Comments Off on Java.Collections.How to iterate over all values ​​in a Map?

Java.Core.How to iterate over all keys in a Map?

Iterating over all keys in a Map is very common, and Java makes it super easy. Here are 4 common ways to do it with a HashMap (or any Map<K, V>):

Posted in Без рубрики | Comments Off on Java.Core.How to iterate over all keys in a Map?

Java.Collections.Will HashMap work if all added keys have the same hashCode()?

✅ Yes, HashMap will still work If all keys have the same hashCode(), the HashMap will: Map every key to the same bucket (because index = hash & (table.length – 1) will always be the same). Store all entries in … Continue reading →

Posted in Без рубрики | Comments Off on Java.Collections.Will HashMap work if all added keys have the same hashCode()?