Java.Collections.WeakHashMap uses WeakReferences. Why not create a SoftHashMap on SoftReferences?

✅ Difference Between WeakReference and SoftReference

FeatureWeakReferenceSoftReference
GC behaviorCollected as soon as no strong refsCollected only when memory is low
LifespanShortLonger (survives longer in memory)
Use caseAutomatic cleanupMemory-sensitive cache

So in theory:

✅ WeakHashMap → removes entries aggressively
✅ SoftHashMap → removes entries only if memory is tight → perfect for caching

Continue reading →
Posted in Без рубрики | Comments Off on Java.Collections.WeakHashMap uses WeakReferences. Why not create a SoftHashMap on SoftReferences?

Java.Collections.Strong references, weak, soft, phantom

This is one of the most fundamental and fascinating topics in Java memory management. Let’s go through all 4 types of references in Java, how the Garbage Collector (GC) treats them, and when you’d use each one.

Continue reading →
Posted in Без рубрики | Comments Off on Java.Collections.Strong references, weak, soft, phantom

Java.Collections.What is weak reference ?

In Java, a weak reference is a type of reference that does not prevent an object from being garbage collected.

✅ In plain terms:

If an object is only weakly referenced (i.e., no strong references point to it), then the GC is free to collect it at any time — even if it’s still “reachable” through that weak reference.

Continue reading →
Posted in Без рубрики | Comments Off on Java.Collections.What is weak reference ?

Java.Collections.What is the difference between HashMap and WeakHashMap? What is WeakHashMap used for?

Let’s talk about HashMap vs WeakHashMap, and why the second one is super useful in certain cases.


🔍 Main Difference

FeatureHashMapWeakHashMap
Key reference typeStrong referencesWeak references
Garbage collectionKeys are never GC’d if referencedKeys can be GC’d if not strongly referenced elsewhere
When key is GC’dKey-value pair stays in mapKey-value pair is automatically removed
Use caseGeneral-purposeCaches, memory-sensitive mappings
Continue reading →
Posted in Без рубрики | Comments Off on Java.Collections.What is the difference between HashMap and WeakHashMap? What is WeakHashMap used for?

Java.Collections.What is the difference between HashMap and IdentityHashMap? What is IdentityHashMap used for?

Let’s get into the difference between HashMap and IdentityHashMap, and when to use this strange beast of a map.


🎯 TL;DR:

✅ HashMap compares keys using .equals()
✅ IdentityHashMap compares keys using == (object reference equality)

That’s the core difference — but it leads to some very different behaviors.

Continue reading →
Posted in Без рубрики | Comments Off on Java.Collections.What is the difference between HashMap and IdentityHashMap? What is IdentityHashMap used for?

Java.Collections.Why do we need HashMap if we have Hashtable?

🧓 First, a bit of history

  • Hashtable is part of the original Java 1.0.
  • HashMap was introduced in Java 1.2 as part of the new Collections Framework.
Continue reading →
Posted in Без рубрики | Comments Off on Java.Collections.Why do we need HashMap if we have Hashtable?

Java.Collections.Stack is considered “obsolete”. What is recommended to replace it? Why?

in modern Java, the Stack class is generally considered obsolete or legacy. Let’s break it down 👇


🛑 Why Stack is considered obsolete

public class Stack<E> extends Vector<E>

😬 Problems:

  • It extends Vector, which is:
    • A synchronized, thread-safe collection — unnecessary overhead for most use cases.
    • An old-school structure (from Java 1.0 era).
  • Stack doesn’t implement the Deque interface, which is now the preferred way to model stacks and queues in Java.
  • Less flexible — fewer stack-friendly methods, no double-ended operations.
Continue reading →
Posted in Без рубрики | Comments Off on Java.Collections.Stack is considered “obsolete”. What is recommended to replace it? Why?

Java.Collections.What can PriorityQueue do?

The PriorityQueue in Java is a powerful tool for managing elements based on priority rather than insertion order — super useful in many real-world scenarios like scheduling, simulations, and pathfinding algorithms (like Dijkstra!).

Continue reading →
Posted in Без рубрики | Comments Off on Java.Collections.What can PriorityQueue do?

Java.Collections.How to iterate through LinkedList elements in reverse order without using slow get(index)?

🔄 Reversely iterating over a LinkedList without using get(index) is a smart move — because get(i) on a LinkedList is O(n), and doing it in a loop would give you O(n²) total time 😱.

✅ Best way: Use DescendingIterator

LinkedList implements the Deque interface, and Deque provides:

Iterator<E> descendingIterator();

This gives you a fast, memory-efficient reverse iterator, using internal node traversal (no index lookups).

Continue reading →
Posted in Без рубрики | Comments Off on Java.Collections.How to iterate through LinkedList elements in reverse order without using slow get(index)?

Java.Collections.Is LinkedList singly linked, doubly linked, or quadruple linked?

✅ Java’s LinkedList is Doubly Linked

Each node in Java’s LinkedList contains:

  • A reference to the next node
  • A reference to the previous node
  • A value

So it’s a classic doubly linked list.

Continue reading →
Posted in Без рубрики | Comments Off on Java.Collections.Is LinkedList singly linked, doubly linked, or quadruple linked?