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:

Set<KeyType> keySet = new HashSet<>(map.keySet());

💡 Example:

Map<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("cherry", 3);

Set<String> keySet = new HashSet<>(map.keySet()); // One-liner

System.out.println(keySet); // Output: [banana, cherry, apple]

🧠 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:

Set<String> set = new HashSet<>(arrayList);

💡 Example:

List<String> arrayList = List.of("apple", "banana", "apple", "cherry");

Set<String> set = new HashSet<>(arrayList); // One-liner conversion

System.out.println(set); // Output: [banana, cherry, apple] (order not guaranteed, duplicates removed)

🧠 Notes:

  • ✅ Duplicates are removed automatically.
  • ❌ Order is not preserved (because HashSet is unordered).
  • If you want to preserve insertion order, you can use:
Set<String> set = new LinkedHashSet<>(arrayList);
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:

List<String> list = new ArrayList<>(hashSet);

💡 Example:

Set<String> hashSet = Set.of("apple", "banana", "cherry");

List<String> list = new ArrayList<>(hashSet); // One-liner conversion

System.out.println(list); // Output: [apple, banana, cherry] (order not guaranteed)

🧠 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 LinkedHashSet, the same one-liner works just fine.
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:

List<T> middle = list.subList(3, list.size() - 3);

🔍 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.
Continue reading →
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:

Continue reading →
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:

protected boolean removeEldestEntry(Map.Entry<K, V> eldest)

You can override this to define your invalidation policy (e.g., max size).

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.

Continue reading →
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.

Continue reading →
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!

Continue reading →
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:

List<T> readOnlyList   = Collections.unmodifiableList(originalList);
Set<T> readOnlySet     = Collections.unmodifiableSet(originalSet);
Map<K, V> readOnlyMap  = Collections.unmodifiableMap(originalMap);
Continue reading →
Posted in Без рубрики | Comments Off on Java.Collections.How can you get a read-only collection?