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

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

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

LinkedHashMap: clear()

 

import java.util.LinkedHashMap;
public class Main {
  public static void main(String[] args) {
    LinkedHashMap<String,String> lHashMap = new LinkedHashMap<String,String>();
    lHashMap.put("1", "One");
    lHashMap.put("2", "Two");
    lHashMap.put("3", "Three");
    lHashMap.clear();
    System.out.println(lHashMap.size());
  }
}





LinkedHashMap: containsKey(Object key)

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





LinkedHashMap: containsValue(Object value)

 
       
import java.util.LinkedHashMap;
public class Main {
  public static void main(String[] args) {
    LinkedHashMap<String,String> lHashMap = new LinkedHashMap<String,String>();
    lHashMap.put("1", "One");
    lHashMap.put("2", "Two");
    lHashMap.put("3", "Three");
    boolean blnExists = lHashMap.containsValue("Two");
    System.out.println(blnExists);
  }
}





LinkedHashMap: get(Object key)

 

import java.util.LinkedHashMap;
public class Main {
  public static void main(String[] args) {
    LinkedHashMap<String,Integer> lHashMap = new LinkedHashMap<String,Integer>();
    lHashMap.put("One", new Integer(1));
    lHashMap.put("Two", new Integer(2));

    Object obj = lHashMap.get("One");
    System.out.println(obj);
  }
}





LinkedHashMap: put(K key, V value)

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





LinkedHashMap: remove(Object key)

 

import java.util.LinkedHashMap;
public class Main {
  public static void main(String[] args) {
    LinkedHashMap<String,String> lHashMap = new LinkedHashMap<String,String>();
    System.out.println(lHashMap.size());
    lHashMap.put("1", "One");
    lHashMap.put("2", "Two");
    lHashMap.put("3", "Three");
    System.out.println(lHashMap.size());
    Object obj = lHashMap.remove("2");
    System.out.println(lHashMap.size());
  }
}





LinkedHashMap: size()

 
import java.util.LinkedHashMap;
public class Main {
  public static void main(String[] args) {
    LinkedHashMap<String,String> lHashMap = new LinkedHashMap<String,String>();
    System.out.println(lHashMap.size());
    lHashMap.put("1", "One");
    lHashMap.put("2", "Two");
    lHashMap.put("3", "Three");
    System.out.println(lHashMap.size());
    Object obj = lHashMap.remove("2");
    System.out.println(lHashMap.size());
  }
}





LinkedHashMap: values()

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





new LinkedHashMap()

  
import java.util.LinkedHashMap;
public class Main {
  public static void main(String[] args) {
    LinkedHashMap<Integer, Integer> linkedMap = new LinkedHashMap<Integer, Integer>();
    for (int i = 0; i < 10; i++) {
      linkedMap.put(i, i);
    }
    System.out.println(linkedMap);
    // Least-recently used order:
    linkedMap = new LinkedHashMap<Integer, Integer>(16, 0.75f, true);
    for (int i = 0; i < 10; i++) {
      linkedMap.put(i, i);
    }
    System.out.println(linkedMap);
    for (int i = 0; i < 7; i++)
      System.out.println(linkedMap.get(i));
    System.out.println(linkedMap);
  }
}





new LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder)

  
import java.util.LinkedHashMap;
public class Main {
  public static void main(String[] args) {
    LinkedHashMap<Integer, Integer> linkedMap = new LinkedHashMap<Integer, Integer>();
    for (int i = 0; i < 10; i++) {
      linkedMap.put(i, i);
    }
    System.out.println(linkedMap);
    // Least-recently used order:
    linkedMap = new LinkedHashMap<Integer, Integer>(16, 0.75f, true);
    for (int i = 0; i < 10; i++) {
      linkedMap.put(i, i);
    }
    System.out.println(linkedMap);
    for (int i = 0; i < 7; i++)
      System.out.println(linkedMap.get(i));
    System.out.println(linkedMap);
  }
}