Java by API/java.util/Map

Материал из Java эксперт
Версия от 14:10, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

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 = (String) map.get(queryString);
    System.out.println("They are located in: " + resultString);
    System.out.println();
    Iterator k = map.keySet().iterator();
    while (k.hasNext()) {
      String key = (String) k.next();
      System.out.println("Key " + key + "; Value " + (String) map.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 (((Comparable) k).rupareTo(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 (((Comparable) k).rupareTo(keys.get(i)) == +1) {
        int where = i > 0 ? i - 1 : 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 = ((MyMapEntry) o2).getKey();
      return ((Comparable) key).rupareTo((Comparable) otherKey);
    }
  }
  /** 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);
  }
}





java.util.Map.Entry<K, V>

 
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Main {
  public static void main(String[] a) {
    Map<String,String> map = new HashMap<String,String>();
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");
    Set set = map.entrySet();
    Iterator iter = set.iterator();
    while (iter.hasNext()) {
      Map.Entry entry = (Map.Entry) iter.next();
      System.out.println(entry.getKey() + " -- " + entry.getValue());
    }
  }
}





Map: clear()

 
import java.util.HashMap;
import java.util.Map;
public class Main {
  public static void main(String[] a) {
    Map<String,String> map = new HashMap<String,String>();
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");
    map.clear();
    System.out.println(map);
  }
}





Map: containsKey(Object key)

 

import java.util.HashMap;
import java.util.Map;
class Counter {
  int i = 1;
  public String toString() {
    return Integer.toString(i);
  }
}
public class Main {
  public static void main(String[] args) {
    Map hm = new HashMap();
    for (int i = 0; i < 10; i++) {
      Integer r = new Integer(20);
      if (hm.containsKey(r))
        ((Counter) hm.get(r)).i++;
      else
        hm.put(r, new Counter());
    }
    System.out.println(hm);
  }
}





Map: containsValue(Object value)

 
import java.util.HashMap;
import java.util.Map;
public class Main {
  public static void main(String[] a) {
    Map<String,String> map = new HashMap<String,String>();
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");
    System.out.println(map.containsKey("key1"));
    System.out.println(map.containsValue("value2"));
  }
}





Map.Entry: getKey()

 
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
public class Main {
  public static void main(String[] a) {
    Properties props = System.getProperties();
    Iterator iter = props.entrySet().iterator();
    while (iter.hasNext()) {
      Map.Entry entry = (Map.Entry) iter.next();
      System.out.println(entry.getKey() + " -- " + entry.getValue());
    }
  }
}





Map.Entry: getValue()

 
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Main {
  public static void main(String[] a) {
    Map<String,String> map = new HashMap<String,String>();
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");
    Set set = map.entrySet();
    Iterator iter = set.iterator();
    while (iter.hasNext()) {
      Map.Entry entry = (Map.Entry) iter.next();
      System.out.println(entry.getKey() + " -- " + entry.getValue());
    }
  }
}





Map: equals(Object o)

 
import java.util.HashMap;
import java.util.Map;
public class Main {
  public static void main(String[] a) {
    Map<String,String> map = new HashMap<String,String>();
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");
    Map<String,String> map2 = new HashMap<String,String>();
    map2.put("key2", "value2");
    map2.put("key1", "value1");
    map2.put("key3", "value3");
    System.out.println(map2.equals(map2));
  }
}





Map: get(Object key)

 
import java.util.HashMap;
import java.util.Map;
public class Main{
  public static void main(String[] a) {
    Map<String,String> map = new HashMap<String,String>();
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");
    System.out.println(map.get("key2"));
  }
}





Map: keySet()

 
/*
Key Adobe; Value Mountain View, CA
Key IBM; Value White Plains, NY
Key Learning Tree; Value Los Angeles, CA
*/       
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class MainClass {
  public static void main(String[] argv) {
    Map map = new HashMap();
    map.put("Adobe", "Mountain View, CA");
    map.put("IBM", "White Plains, NY");
    map.put("Learning Tree", "Los Angeles, CA");
    Iterator k = map.keySet().iterator();
    while (k.hasNext()) {
      String key = (String) k.next();
      System.out.println("Key " + key + "; Value " + (String) map.get(key));
    }
  }
}





Map: putAll(Map<?, ?> m)

 
import java.util.HashMap;
import java.util.Map;
public class Main {
  public static void main(String[] a) {
    Map<String,String> map = new HashMap<String,String>();
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");
    map.put(null, null);
    Map<String,String> map2 = new HashMap<String,String>();
    map2.put("key4", "value4");
    map2.put("key5", "value5");
    map2.put("key6", "value6");
    map.putAll(map2);
    System.out.println(map);
  }
}





Map: put(T key, T value)

 
import java.util.HashMap;
import java.util.Map;
public class Main {
  public static void main(String[] a) {
    Map<String, String> map = new HashMap<String, String>();
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");
    System.out.println(map);
  }
}





Map: remove(Object key)

 
import java.util.HashMap;
import java.util.Map;
public class Main {
  public static void main(String[] a) {
    Map<String,String> map = new HashMap<String,String>();
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");
    map.remove("key3");
    System.out.println(map);
  }
}





Map: size()

 
import java.util.HashMap;
import java.util.Map;
public class Main{
  public static void main(String[] a) {
    Map<String,String> map = new HashMap<String,String>();
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");
    System.out.println(map.size());
  }
}





Map: values()

 
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class Main {
  public static void main(String[] a) {
    Map<String,String> map = new HashMap<String,String>();
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");
    Collection set = map.values();
    Iterator iter = set.iterator();
    while (iter.hasNext()) {
      System.out.println(iter.next());
    }
  }
}