// Example of common mistakes with soft and weak references in Java
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.HashMap;
public class ReferenceExample {
static HashMap> weakReferenceMap = new HashMap<>();
static HashMap> softReferenceMap = new HashMap<>();
public static void main(String[] args) {
Object heavyObject = new Object();
softReferenceMap.put("softRef", new SoftReference<>(heavyObject));
weakReferenceMap.put("weakRef", new WeakReference<>(heavyObject));
// Mistake: Checking the state of the reference immediately after putting the object
System.out.println("Soft Reference: " + softReferenceMap.get("softRef").get()); // May return null if JVM cleaned up
System.out.println("Weak Reference: " + weakReferenceMap.get("weakRef").get()); // Likely to be null due to garbage collection
// Mistake: Not handling the potential for null values
Object retrievedSoft = softReferenceMap.get("softRef").get();
if (retrievedSoft != null) {
System.out.println("Using soft reference object.");
} else {
System.out.println("Soft reference has been cleared.");
}
Object retrievedWeak = weakReferenceMap.get("weakRef").get();
if (retrievedWeak != null) {
System.out.println("Using weak reference object.");
} else {
System.out.println("Weak reference has been cleared.");
}
}
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?