Author Archives: Stanislav_Panteleev

Java.Collections.If you want to create a HashSet from the keys of a HashMap, it’s super simple — just grab the key set and pass it to the HashSet constructor.

✅ One-liner: 💡 Example: 🧠 Notes: map.keySet() returns a live view of the map’s keys. Wrapping it in a HashSet gives you a separate copy. If you only want a read-only view, you could use map.keySet() directly.

Posted in Без рубрики | Comments Off on Java.Collections.If you want to create a HashSet from the keys of a HashMap, it’s super simple — just grab the key set and pass it to the HashSet constructor.

Java.Collections.How to convert an ArrayList to a HashSet in one line?

✅ To convert an ArrayList to a HashSet in one line, just use the HashSet constructor: 💡 Example: 🧠 Notes: ✅ Duplicates are removed automatically. ❌ Order is not preserved (because HashSet is unordered). If you want to preserve insertion … Continue reading →

Posted in Без рубрики | Comments Off on Java.Collections.How to convert an ArrayList to a HashSet in one line?

Java.Collections.How to convert a HashSet to an ArrayList in one line?

✅ One-liner: 💡 Example: 🧠 Notes: This copies all elements from the HashSet to a new ArrayList. The order is not preserved, because HashSet doesn’t maintain insertion order. If you want to preserve insertion order, and you’re starting with a … Continue reading →

Posted in Без рубрики | Comments Off on Java.Collections.How to convert a HashSet to an ArrayList in one line?

Java.Collections.How to get a List with all elements except the first and last 3 in one call from List?

✅ One-liner using subList: 🔍 Explanation: list.subList(fromIndex, toIndex) returns a view of the list from fromIndex (inclusive) to toIndex (exclusive). So: 3 skips the first 3 elements, list.size() – 3 excludes the last 3 elements.

Posted in Без рубрики | Comments Off on Java.Collections.How to get a List with all elements except the first and last 3 in one call from List?

Java.Collections.How to copy elements of any collection to an array in one line?

✅ If you want to copy elements from any collection to an array in just one line, here’s how you do it in Java:

Posted in Без рубрики | Comments Off on Java.Collections.How to copy elements of any collection to an array in one line?

Java.Collections.How to make a cache with “invalidation policy” using LinkedHashMap?

✅ Why LinkedHashMap? LinkedHashMap maintains a linked list of entries: Either in insertion order or Access order (when enabled) — which is perfect for caching. It also provides a protected method: You can override this to define your invalidation policy … Continue reading →

Posted in Без рубрики | Comments Off on Java.Collections.How to make a cache with “invalidation policy” using LinkedHashMap?

Java.Collections.Implement a symmetric difference of two collections using Collection methods (addAll(…), removeAll(…), retainAll(…)).

🔄 Let’s implement the symmetric difference of two collections in Java using only standard Collection methods like addAll(), removeAll(), and retainAll() — no custom loops, no streams, just pure Java Collections API.

Posted in Без рубрики | Comments Off on Java.Collections.Implement a symmetric difference of two collections using Collection methods (addAll(…), removeAll(…), retainAll(…)).

Java.Collections.Give an example of a collection throwing an UnsupportedOperationException.

✅ When does UnsupportedOperationException happen? It’s thrown when you try to perform a modification operation (like add(), remove(), or clear()) on a collection that doesn’t support it.

Posted in Без рубрики | Comments Off on Java.Collections.Give an example of a collection throwing an UnsupportedOperationException.

Java.Collections.Write a single-threaded program that causes a collection to throw a ConcurrentModificationException.

💣 Here’s a minimal and crystal-clear single-threaded Java program that throws a ConcurrentModificationException by modifying a collection during iteration — a classic trap!

Posted in Без рубрики | Comments Off on Java.Collections.Write a single-threaded program that causes a collection to throw a ConcurrentModificationException.

Java.Collections.How can you get a read-only collection?

✅ Use Collections.unmodifiableXxx() methods Java’s Collections class provides static factory methods to wrap a collection and prevent modifications:

Posted in Без рубрики | Comments Off on Java.Collections.How can you get a read-only collection?