Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
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-16-2008, 11:29 PM
Moderator
 
Join Date: Nov 2007
Posts: 1,411
Java Tip will become famous soon enoughJava Tip will become famous soon enough
A Map implemented with ArrayLists
Code:
import java.util.AbstractMap; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; public class SlowMap extends AbstractMap { private List keys = new ArrayList(), values = new ArrayList(); public Object put(Object key, Object value) { Object result = get(key); if (!keys.contains(key)) { keys.add(key); values.add(value); } else values.set(keys.indexOf(key), value); return result; } public Object get(Object key) { if (!keys.contains(key)) return null; return values.get(keys.indexOf(key)); } public Set entrySet() { Set entries = new HashSet(); Iterator ki = keys.iterator(), vi = values.iterator(); while (ki.hasNext()) entries.add(new MPair(ki.next(), vi.next())); return entries; } public String toString() { StringBuffer s = new StringBuffer("{"); Iterator ki = keys.iterator(), vi = values.iterator(); while (ki.hasNext()) { s.append(ki.next() + "=" + vi.next()); if (ki.hasNext()) s.append(", "); } s.append("}"); return s.toString(); } public static void main(String[] args) { SlowMap m = new SlowMap(); m.put("Adobe", "Mountain View, CA"); m.put("IBM", "White Plains, NY"); m.put("Learning Tree", "Los Angeles, CA"); m.put("Microsoft", "Redmond, WA"); m.put("Netscape", "Mountain View, CA"); m.put("O'Reilly", "Sebastopol, CA"); m.put("Sun", "Mountain View, CA"); System.out.println(m); } } class MPair implements Entry, Comparable { Object key, value; MPair(Object k, Object v) { key = k; value = v; } public Object getKey() { return key; } public Object getValue() { return value; } public Object setValue(Object v){ Object result = value; value = v; return result; } public boolean equals(Object o) { return key.equals(((MPair)o).key); } public int compareTo(Object rv) { return ((Comparable)key).compareTo( ((MPair)rv).key); } }
__________________
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.
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
recursively searching through arraylists newtojava7 New To Java 1 03-17-2008 03:36 AM
arraylists problem newtojava7 New To Java 1 03-12-2008 08:38 AM


All times are GMT +3. The time now is 07:40 AM.


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