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-14-2008, 10:40 PM
Moderator
 
Join Date: Nov 2007
Posts: 1,657
Java Tip will become famous soon enoughJava Tip will become famous soon enough
Custom ArrayMap implementation
Code:
import java.io.Serializable; import java.util.AbstractMap; import java.util.AbstractSet; import java.util.ArrayList; import java.util.Iterator; import java.util.Map; import java.util.Set; public class ArrayMap extends AbstractMap implements Cloneable, Serializable { static class Entry implements Map.Entry { protected Object key, value; public Entry(Object key, Object value) { this.key = key; this.value = value; } public Object getKey() { return key; } public Object getValue() { return value; } public Object setValue(Object newValue) { Object oldValue = value; value = newValue; return oldValue; } public boolean equals(Object o) { if (!(o instanceof Map.Entry)) { return false; } Map.Entry e = (Map.Entry) o; return (key == null ? e.getKey() == null : key.equals(e.getKey())) && (value == null ? e.getValue() == null : value.equals(e .getValue())); } public int hashCode() { int keyHash = (key == null ? 0 : key.hashCode()); int valueHash = (value == null ? 0 : value.hashCode()); return keyHash ^ valueHash; } public String toString() { return key + "=" + value; } } private Set entries = null; private ArrayList list; public ArrayMap() { list = new ArrayList(); } public ArrayMap(Map map) { list = new ArrayList(); putAll(map); } public ArrayMap(int initialCapacity) { list = new ArrayList(initialCapacity); } public Set entrySet() { if (entries == null) { entries = new AbstractSet() { public void clear() { list.clear(); } public Iterator iterator() { return list.iterator(); } public int size() { return list.size(); } }; } return entries; } public Object put(Object key, Object value) { int size = list.size(); Entry entry = null; int i; if (key == null) { for (i = 0; i < size; i++) { entry = (Entry) (list.get(i)); if (entry.getKey() == null) { break; } } } else { for (i = 0; i < size; i++) { entry = (Entry) (list.get(i)); if (key.equals(entry.getKey())) { break; } } } Object oldValue = null; if (i < size) { oldValue = entry.getValue(); entry.setValue(value); } else { list.add(new Entry(key, value)); } return oldValue; } public Object clone() { return new ArrayMap(this); } public static void main(String args[]) { Map map = new ArrayMap(13); map.put("1", "One"); map.put("2", "Two"); map.put("3", "Three"); map.put("4", "Four"); map.put("5", "Five"); map.put("6", "Six"); map.put("7", "Seven"); map.put("8", "Eight"); map.put("9", "Nine"); map.put("10", "Ten"); map.put("11", "Eleven"); map.put("12", "Twelve"); map.put("13", "Thirteen"); System.out.println(map); System.out.println(map.keySet()); System.out.println(map.values()); } }
__________________
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
Custom ArraySet implementation Java Tip java.lang 0 04-14-2008 10:41 PM
VietPad 2.0 (.NET implementation) Java Tip Java Announcements 0 04-05-2008 06:32 PM
Graph DPS and BFS implementation hey New To Java 1 01-09-2008 11:19 PM
Custom tgs in JSP ravian New To Java 2 12-29-2007 07:05 PM
Help with recursive implementation toby Advanced Java 1 08-07-2007 07:57 AM


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


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