implements Map : Map « java.util « Java by API

Java by API
1. com.sun.image.codec.jpeg
2. java.applet
3. java.awt
4. java.awt.datatransfer
5. java.awt.dnd
6. java.awt.event
7. java.awt.font
8. java.awt.geom
9. java.awt.image
10. java.awt.print
11. java.beans
12. java.io
13. java.lang
14. java.lang.instrument
15. java.lang.management
16. java.lang.reflect
17. java.math
18. java.net
19. java.nio
20. java.nio.channels
21. java.nio.charset
22. java.rmi.server
23. java.security
24. java.security.cert
25. java.sql
26. java.text
27. java.util
28. java.util.concurrent
29. java.util.logging
30. java.util.prefs
31. java.util.regex
32. java.util.zip
33. javax.accessibility
34. javax.comm
35. javax.naming
36. javax.net
37. javax.net.ssl
38. javax.print
39. javax.servlet
40. javax.servlet.http
41. javax.sql
42. javax.sql.rowset
43. javax.swing
44. javax.swing.border
45. javax.swing.colorchooser
46. javax.swing.event
47. javax.swing.filechooser
48. javax.swing.plaf.basic
49. javax.swing.plaf.metal
50. javax.swing.plaf.synth
51. javax.swing.table
52. javax.swing.text
53. javax.swing.text.html
54. javax.swing.text.html.parser
55. javax.swing.tree
56. javax.swing.undo
57. javax.xml
58. javax.xml.parsers
59. javax.xml.transform
60. javax.xml.transform.dom
61. javax.xml.transform.stream
62. javax.xml.validation
63. javax.xml.xpath
64. org.apache.commons.lang
65. org.apache.commons.lang.builder
66. org.apache.commons.lang.exception
67. org.apache.commons.lang.time
68. org.apache.commons.logging
69. org.apache.commons.math
70. org.eclipse.jface.action
71. org.eclipse.jface.dialogs
72. org.eclipse.jface.operation
73. org.eclipse.jface.viewers
74. org.eclipse.jface.window
75. org.eclipse.jface.wizard
76. org.eclipse.swt
77. org.eclipse.swt.browser
78. org.eclipse.swt.custom
79. org.eclipse.swt.dnd
80. org.eclipse.swt.events
81. org.eclipse.swt.graphics
82. org.eclipse.swt.layout
83. org.eclipse.swt.ole.win32
84. org.eclipse.swt.printing
85. org.eclipse.swt.program
86. org.eclipse.swt.widgets
87. org.w3c.dom
88. org.xml.sax
89. org.xml.sax.helpers
90. sun.audio
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
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 by API » java.util » Map 
implements Map

/*
You asked about Google.
They are located in: null

Key Adobe; Value Mountain View, CA
Key IBM; Value White Plains, NY
Key Learning Tree; Value Los Angeles, CA
entrySet() returns 3 Map.Entry's*/

import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

public class MainClass {

  public static void main(String[] argv) {
    Map map = new MyMap();

    map.put("Adobe""Mountain View, CA");
    map.put("Learning Tree""Los Angeles, CA");
    map.put("IBM""White Plains, NY");

    String queryString = "Google";
    System.out.println("You asked about " + queryString + ".");
    String resultString = (Stringmap.get(queryString);
    System.out.println("They are located in: " + resultString);
    System.out.println();

    Iterator k = map.keySet().iterator();
    while (k.hasNext()) {
      String key = (Stringk.next();
      System.out.println("Key " + key + "; Value " (Stringmap.get(key));
    }

    Set es = map.entrySet();
    System.out.println("entrySet() returns " + es.size() " Map.Entry's");
  }
}

class MyMap implements Map {

  private ArrayList keys;

  private ArrayList values;

  public MyMap() {
    keys = new ArrayList();
    values = new ArrayList();
  }

  /** Return the number of mappings in this Map. */
  public int size() {
    return keys.size();
  }

  /** Return true if this map is empty. */
  public boolean isEmpty() {
    return size() == 0;
  }

  /** Return true if o is contained as a Key in this Map. */
  public boolean containsKey(Object o) {
    return keys.contains(o);
  }

  /** Return true if o is contained as a Value in this Map. */
  public boolean containsValue(Object o) {
    return keys.contains(o);
  }

  /** Get the object value corresponding to key k. */
  public Object get(Object k) {
    int i = keys.indexOf(k);
    if (i == -1)
      return null;
    return values.get(i);
  }

  /**
   * Put the given pair (k, v) into this map, by maintaining "keys" in sorted
   * order.
   */
  public Object put(Object k, Object v) {
    for (int i = 0; i < keys.size(); i++) {
      Object old = keys.get(i);

      /* Does the key already exist? */
      if (((Comparablek).compareTo(keys.get(i)) == 0) {
        keys.set(i, v);
        return old;
      }

      /*
       * Did we just go past where to put it? i.e., keep keys in sorted order.
       */
      if (((Comparablek).compareTo(keys.get(i)) == +1) {
        int where = i > ? i - 0;
        keys.add(where, k);
        values.add(where, v);
        return null;
      }
    }

    // Else it goes at the end.
    keys.add(k);
    values.add(v);
    return null;
  }

  /** Put all the pairs from oldMap into this map */
  public void putAll(java.util.Map oldMap) {
    Iterator keysIter = oldMap.keySet().iterator();
    while (keysIter.hasNext()) {
      Object k = keysIter.next();
      Object v = oldMap.get(k);
      put(k, v);
    }
  }

  public Object remove(Object k) {
    int i = keys.indexOf(k);
    if (i == -1)
      return null;
    Object old = values.get(i);
    keys.remove(i);
    values.remove(i);
    return old;
  }

  public void clear() {
    keys.clear();
    values.clear();
  }

  public java.util.Set keySet() {
    return new TreeSet(keys);
  }

  public java.util.Collection values() {
    return values;
  }

  /**
   * The Map.Entry objects contained in the Set returned by entrySet().
   */
  private class MyMapEntry implements Map.Entry, Comparable {
    private Object key, value;

    MyMapEntry(Object k, Object v) {
      key = k;
      value = v;
    }

    public Object getKey() {
      return key;
    }

    public Object getValue() {
      return value;
    }

    public Object setValue(Object nv) {
      throw new UnsupportedOperationException("setValue");
    }

    public int compareTo(Object o2) {
      if (!(o2 instanceof MyMapEntry))
        throw new IllegalArgumentException("Huh? Not a MapEntry?");
      Object otherKey = ((MyMapEntryo2).getKey();
      return ((Comparablekey).compareTo((ComparableotherKey);
    }
  }

  /** The set of Map.Entry objects returned from entrySet(). */
  private class MyMapSet extends AbstractSet {
    List list;

    MyMapSet(ArrayList al) {
      list = al;
    }

    public Iterator iterator() {
      return list.iterator();
    }

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

  /**
   * Returns a set view of the mappings contained in this Map. Each element in
   * the returned set is a Map.Entry. NOT guaranteed fully to implement the
   * contract of entrySet declared in java.util.Map.
   */
  public java.util.Set entrySet() {
    if (keys.size() != values.size())
      throw new IllegalStateException("InternalError: keys and values out of sync");
    ArrayList al = new ArrayList();
    for (int i = 0; i < keys.size(); i++) {
      al.add(new MyMapEntry(keys.get(i), values.get(i)));
    }
    return new MyMapSet(al);
  }
}

           
       
Related examples in the same category
1. Map: keySet()
w__w_w__.__ja___va__2s.c__om
Home | Contact Us
Copyright 2003 - 07 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.