Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-12-2008, 10:45 PM
Moderator
 
Join Date: Nov 2007
Posts: 1,657
Java Tip will become famous soon enoughJava Tip will become famous soon enough
Soft HashMap
Code:
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 (!(o 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 (!(o 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) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : 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 (!(o 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) ? 0 : v.hashCode())); } return h; } } }
__________________
Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums! (closes on July 27, 2008)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
HashMap and ComboBox banie AWT / Swing 2 03-26-2008 01:58 AM
ArrayList into hashMap Preethi New To Java 2 02-11-2008 10:13 AM
Hashmap dirtycash New To Java 5 12-03-2007 04:58 AM
what is hashmap gabriel New To Java 5 08-03-2007 03:23 PM
Soft References rickcharles_b Advanced Java 0 06-20-2007 10:27 PM


All times are GMT +3. The time now is 01:55 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org