Soft HashMap : HashTable Map « Collections Data Structure « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Collections Data Structure » HashTable MapScreenshots 
Soft HashMap


import java.util.*;
import java.lang.ref.*;

public class SoftHashMap extends AbstractMap implements Map {

  private Set entrySet = null;
  private Map hash;
  private ReferenceQueue queue = new ReferenceQueue();

  static private class SoftKey extends SoftReference {
    private int hash;

    private SoftKey(Object k) {
      super(k);
      hash = k.hashCode();
    }
    private static SoftKey create(Object k) {
      if (k == null) {
        return null;
      else {
        return new SoftKey(k);
      }
    }
    private SoftKey(Object k, ReferenceQueue q) {
      super(k, q);
      hash = k.hashCode();
    }
    private static SoftKey create(Object k, ReferenceQueue q) {
      if (k == null) {
        return null;
      else {
        return new SoftKey(k, q);
      }
    }
    public boolean equals(Object o) {
      if (this == o) {
        return true;
      else if (!(instanceof SoftKey)) {
        return false;
      }
      Object t = this.get();
      Object u = ((SoftKey)o).get();
      if ((t == null|| (u == null)) {
        return false;
      else if (t == u) {
        return true;
      else {
        return t.equals(u);
      }
    }
    public int hashCode() {
      return hash;
    }
  }

  private void processQueue() {
    SoftKey sk;
    while ((sk = (SoftKey)queue.poll()) != null) {
      hash.remove(sk);
    }
  }

  public SoftHashMap() {
    hash = new HashMap();
  }

  public SoftHashMap(Map t) {
    this(Math.max(2*t.size()11)0.75f);
    putAll(t);
  }

  public SoftHashMap(int initialCapacity) {
    hash = new HashMap(initialCapacity);
  }

  public SoftHashMap(int initialCapacity, float loadFactor) {
    hash = new HashMap(initialCapacity, loadFactor);
  }

  public int size() {
    return entrySet().size();
  }

  public boolean isEmpty() {
    return entrySet().isEmpty();
  }

  public boolean containsKey(Object key) {
    return hash.containsKey(SoftKey.create(key));
  }

  public Object get(Object key) {
    return hash.get(SoftKey.create(key));
  }

  public Object put(Object key, Object value) {
    processQueue();
    return hash.put(SoftKey.create(key, queue), value);
  }

  public Object remove(Object key) {
    processQueue();
    return hash.remove(SoftKey.create(key));
  }

  public void clear() {
    processQueue();
    hash.clear();
  }

  private static class Entry implements Map.Entry {
    private Map.Entry ent;
    private Object key;

    Entry(Map.Entry ent, Object key) {
      this.ent = ent;
      this.key = key;
    }

    public Object getKey() {
      return key;
    }

    public Object getValue() {
      return ent.getValue();
    }

    public Object setValue(Object value) {
      return ent.setValue(value);
    }

    public boolean equals(Object o) {
      if (!(instanceof Map.Entry)) {
        return false;
      else {
        Map.Entry e = (Map.Entry)o;
        Object value = getValue();
        return (key==null ? e.getKey()==null : key.equals(e.getKey())) &&
               (value==null ? e.getValue()==null : value.equals(e.getValue()));
      }
    }

    public int hashCode() {
      Object value = getValue();
      return (((key == null: key.hashCode())
        ((value == null: value.hashCode()));
    }

  }

  public Set entrySet() {
    if (entrySet == null) {
      entrySet = new EntrySet();
    }
    return entrySet;
  }

  private class EntrySet extends AbstractSet {
    Set set = hash.entrySet();

    public Iterator iterator() {

      return new Iterator() {
        Iterator iter = set.iterator();
        Entry next = null;

        public boolean hasNext() {
          while (iter.hasNext()) {
            Map.Entry ent = (Map.Entry)iter.next();
            SoftKey sk = (SoftKey)ent.getKey();
            Object k = null;
            if ((sk != null&& ((k = sk.get()) == null)) {
              /* Soft key has been cleared by GC */
              continue;
            }
            next = new Entry(ent, k);
            return true;
          }
          return false;
        }

        public Object next() {
          if ((next == null&& !hasNext()) {
            throw new NoSuchElementException();
          }
          Entry element = next;
          next = null;
          return element;
        }

        public void remove() {
          iter.remove();
        }
      };
    }

    public boolean isEmpty() {
      return !(iterator().hasNext());
    }

    public int size() {
      int size = 0;
      for (Iterator i = iterator(); i.hasNext(); i.next(), size++);
      return size;
    }

    public boolean remove(Object o) {
      processQueue();
      if (!(instanceof Map.Entry)) {
        return false;
      }
      Map.Entry e = (Map.Entry)o;
      Object ev = e.getValue();
      SoftKey sk = SoftKey.create(e.getKey());
      Object hv = hash.get(sk);
      if ((hv == null)
          ((ev == null&& hash.containsKey(sk)) : hv.equals(ev)) {
        hash.remove(sk);
        return true;
      }
      return false;
    }

    public int hashCode() {
      int h = 0;
      for (Iterator i = set.iterator(); i.hasNext();) {
        Map.Entry ent = (Map.Entry)i.next();
        SoftKey sk = (SoftKey)ent.getKey();
        Object v;
        if (sk == null) {
          continue;
        }
        h += (sk.hashCode()
          (((v = ent.getValue()) == null: v.hashCode()));
      }
      return h;
    }
  }
}


           
       
Related examples in the same category
1. Associates keys with valuesAssociates keys with values
2. A simple Map implementationA simple Map implementation
3. Hash table with separate chaining
4. Hash table with linear probingHash table with linear probing
5. Hash table with double hashingHash table with double hashing
6. Working with Key-Value Pairs in a Hashtable
7. Demonstrate the Hashtable class, and an Enumeration
8. Demonstrate the HashMap class, and an IteratorDemonstrate the HashMap class, and an Iterator
9. MultiMap extends AbstractMap
10. Array Map extends AbstractMapArray Map extends AbstractMap
11. Demonstrating the WeakHashMapDemonstrating the WeakHashMap
12. Use treemapUse treemap
13. Sorting Elements in a TreeMapSorting Elements in a TreeMap
14. What you can do with a TreeMapWhat you can do with a TreeMap
15. A Map implemented with ArrayLists
16. Simple demonstration of HashMapSimple demonstration of HashMap
w__ww___.___ja__va___2s__.__c___om__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.