Java by API/java.util/Hashtable — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 17:43, 31 мая 2010

Hashtable: clear()

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





Hashtable: clone()

  
import java.util.Hashtable;
public class Main {
  public static void main(String[] s) {
    Hashtable<String,String> table = new Hashtable<String,String>();
    table.put("key1", "value1");
    table.put("key2", "value2");
    table.put("key3", "value3");
    Hashtable tableCopy = (Hashtable)table.clone();
    System.out.println(tableCopy);
  }
}





Hashtable: containsKey(Object key)

 
import java.util.Hashtable;
public class Main {
  public static void main(String[] args) {
    Hashtable<String, String> ht = new Hashtable<String, String>();
    ht.put("1", "One");
    ht.put("2", "Two");
    ht.put("3", "Three");
    boolean blnExists = ht.containsKey("2");
    System.out.println("2 exists in Hashtable ? : " + blnExists);
  }
}





Hashtable: contains(Object value)

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





Hashtable: elements()

 

import java.util.Collection;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
public class Main {
  public static void main(String[] args) {
    Hashtable<String, String> ht = new Hashtable<String,String>();
    ht.put("1", "One");
    ht.put("2", "Two");
    ht.put("3", "Three");
    Collection c = ht.values();
    Iterator itr = c.iterator();
    while (itr.hasNext()){
      System.out.println(itr.next());
    }
    c.remove("One");
    Enumeration e = ht.elements();
    while (e.hasMoreElements()){
      System.out.println(e.nextElement());
    }
  }
}





Hashtable: entrySet()

  
/*
{three=four, two=three, four=five, one=two}
4
three : four
two : three
four : five
one : two
three : four
two : three
four : five
one : two
 */
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class MainClass {
  public static void main(String args[]) throws Exception {
    Hashtable hash = new Hashtable(89);
    hash.put("one", "two");
    hash.put("two", "three");
    hash.put("three", "four");
    hash.put("four", "five");
    System.out.println(hash);
    System.out.println(hash.size());
    Enumeration e = hash.keys();
    while (e.hasMoreElements()) {
      String key = (String) e.nextElement();
      System.out.println(key + " : " + hash.get(key));
    }
    Set set = hash.entrySet();
    Iterator it = set.iterator();
    while (it.hasNext()) {
      Map.Entry entry = (Map.Entry) it.next();
      System.out.println(entry.getKey() + " : " + entry.getValue());
    }
  }
}





Hashtable: get(E e)

  
/*
 * Output: 
Beijing is located in China
 */
import java.util.Hashtable;
public class MainClass {
  public static void main(String args[]) {
    Hashtable ht = new Hashtable();
    ht.put("Tokyo", "Japan");
    ht.put("Beijing", "China");
    ht.put("Bangkok", "Thailand");
    String city = "Beijing";
    String country = (String) ht.get(city);
    if (country != null)
      System.out.println(city + " is located in " + country);
    else
      System.out.println(city + " is not located in the hashtable");
  }
}





Hashtable: isEmpty()

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





Hashtable: iterator()

  
/**
 *Output:
 A: 4.34
E: -9.08
D: 9.22
C: 8.0
B: 3.22
A"s new balance: 1004.34
 */
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;
public class MainClass {
  public static void main(String args[]) {
    Hashtable<String, Double> balance = new Hashtable<String, Double>();
    String str;
    double bal;
    balance.put("A", 4.34);
    balance.put("B", 3.22);
    balance.put("C", 8.00);
    balance.put("D", 9.22);
    balance.put("E", -9.08);
    Set<String> set = balance.keySet();
    Iterator<String> itr = set.iterator();
    while (itr.hasNext()) {
      str = itr.next();
      System.out.println(str + ": " + balance.get(str));
    }
    System.out.println();
    bal = balance.get("A");
    balance.put("A", bal + 1000);
    System.out.println("A"s new balance: " + balance.get("A"));
  }
}





Hashtable: keys()

  
/*
 * Output: 
key = apple; value = red
key = strawberry; value = red
 */
import java.util.Enumeration;
import java.util.Hashtable;
public class MainClass {
  public static void main(String args[]) {
    Hashtable hashtable = new Hashtable();
    hashtable.put("apple", "red");
    hashtable.put("strawberry", "red");
    Enumeration e = hashtable.keys();
    while(e.hasMoreElements()) {
      Object k = e.nextElement();
      Object v = hashtable.get(k);
      System.out.println("key = " + k + "; value = " + v);
    } 
  }
}





Hashtable: keySet()

  
/**
 *Output:
 A: 4.34
E: -9.08
D: 9.22
C: 8.0
B: 3.22
A"s new balance: 1004.34
 */
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;
public class MainClass {
  public static void main(String args[]) {
    Hashtable<String, Double> balance = new Hashtable<String, Double>();
    String str;
    double bal;
    balance.put("A", 4.34);
    balance.put("B", 3.22);
    balance.put("C", 8.00);
    balance.put("D", 9.22);
    balance.put("E", -9.08);
    Set<String> set = balance.keySet();
    Iterator<String> itr = set.iterator();
    while (itr.hasNext()) {
      str = itr.next();
      System.out.println(str + ": " + balance.get(str));
    }
    System.out.println();
    bal = balance.get("A");
    balance.put("A", bal + 1000);
    System.out.println("A"s new balance: " + balance.get("A"));
  }
}





Hashtable: putAll(Map<K,V> t)

  

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





Hashtable: put(K key, V value)

  
/*
 * Output:
 * 
a: a
b: 2.0
c: b
d: 30

 * 
 *  
 */
import java.util.Dictionary;
import java.util.Hashtable;
public class MainClass {
  public static void main(String args[]) {
    Hashtable ht = new Hashtable();
    ht.put("a", "a");
    ht.put("b", new Double(2));
    ht.put("c", "b");
    ht.put("d", new Integer(30));
    show(ht);
  }
  static void show(Dictionary d) {
    System.out.println("a: " + d.get("a"));
    System.out.println("b: " + d.get("b"));
    System.out.println("c: " + d.get("c"));
    System.out.println("d: " + d.get("d"));
  }
}





Hashtable: remove(Object key)

  

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





Hashtable: size()

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





Hashtable: values()

  

import java.util.Enumeration;
import java.util.Hashtable;
public class Main {
  public static void main(String[] s) {
    Hashtable<String,String> table = new Hashtable<String,String>();
    table.put("key1", "value1");
    table.put("key2", "value2");
    table.put("key3", "value3");
    Enumeration e = table.elements();
    while (e.hasMoreElements()) {
      String key = (String) e.nextElement();
      System.out.println(key + " : " + table.get(key));
    }
    System.out.println(table.values());
  }
}





new Hashtable()

  
/*
 * Output:
 * 
a: a
b: 2.0
c: b
d: 30

 * 
 *  
 */
import java.util.Dictionary;
import java.util.Hashtable;
public class MainClass {
  public static void main(String args[]) {
    Hashtable ht = new Hashtable();
    ht.put("a", "a");
    ht.put("b", new Double(2));
    ht.put("c", "b");
    ht.put("d", new Integer(30));
    show(ht);
  }
  static void show(Dictionary d) {
    System.out.println("a: " + d.get("a"));
    System.out.println("b: " + d.get("b"));
    System.out.println("c: " + d.get("c"));
    System.out.println("d: " + d.get("d"));
  }
}





new Hashtable < K, V > ()

  
/**
 *Output:
A: 3434.34
 */
import java.util.Enumeration;
import java.util.Hashtable;
public class MainClass {
  public static void main(String args[]) {
    Hashtable<String, Double> balance = new Hashtable<String, Double>();
    Enumeration<String> names;
    String str;
    balance.put("A", 3434.34);
    names = balance.keys();
    while (names.hasMoreElements()) {
      str = names.nextElement();
      System.out.println(str + ": " + balance.get(str));
    }
    System.out.println();
  }
}