Java by API/java.util/HashMap
Содержание
- 1 HashMap: clear()
- 2 HashMap: clone()
- 3 HashMap: containsKey(Object key)
- 4 HashMap: containsValue(Object value)
- 5 HashMap: entrySet()
- 6 HashMap: get(E o)
- 7 HashMap: keySet()
- 8 HashMap: putAll(Map<?, ?> m)
- 9 HashMap: put(E o, E o1)
- 10 HashMap: remove(Object key)
- 11 HashMap: size()
- 12 HashMap: values()
- 13 new HashMap < K, V > ()
HashMap: 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);
}
}
HashMap: clone()
import java.util.HashMap;
public class Main {
public static void main(String[] a) {
HashMap<String,String> map = new HashMap<String,String>();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
HashMap map2 = (HashMap)map.clone();
System.out.println(map2);
}
}
HashMap: containsKey(Object key)
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap<String, String> hMap = new HashMap<String, String>();
hMap.put("1", "One");
hMap.put("2", "Two");
hMap.put("3", "Three");
boolean blnExists = hMap.containsKey("3");
System.out.println("3 exists in HashMap ? : " + blnExists);
}
}
HashMap: 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"));
}
}
HashMap: entrySet()
/**
*Output:
D: 99.22
A: 3434.34
C: 1378.0
B: 123.22
E: -19.08
B"s new balance: 1123.22
*/
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class MainClass {
public static void main(String args[]) {
HashMap<String, Double> hm = new HashMap<String, Double>();
hm.put("A", new Double(3434.34));
hm.put("B", new Double(123.22));
hm.put("C", new Double(1378.00));
hm.put("D", new Double(99.22));
hm.put("E", new Double(-19.08));
Set<Map.Entry<String, Double>> set = hm.entrySet();
for (Map.Entry<String, Double> me : set) {
System.out.print(me.getKey() + ": ");
System.out.println(me.getValue());
}
System.out.println();
double balance = hm.get("B");
hm.put("B", balance + 1000);
System.out.println("B"s new balance: " + hm.get("B"));
}
}
HashMap: get(E o)
/**
*Output:
D: 99.22
A: 3434.34
C: 1378.0
B: 123.22
E: -19.08
B"s new balance: 1123.22
*/
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class MainClass {
public static void main(String args[]) {
HashMap<String, Double> hm = new HashMap<String, Double>();
hm.put("A", new Double(3434.34));
hm.put("B", new Double(123.22));
hm.put("C", new Double(1378.00));
hm.put("D", new Double(99.22));
hm.put("E", new Double(-19.08));
Set<Map.Entry<String, Double>> set = hm.entrySet();
for (Map.Entry<String, Double> me : set) {
System.out.print(me.getKey() + ": ");
System.out.println(me.getValue());
}
System.out.println();
double balance = hm.get("B");
hm.put("B", balance + 1000);
System.out.println("B"s new balance: " + hm.get("B"));
}
}
HashMap: keySet()
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
public class Main {
public static void main(String[] args) {
HashMap<String, String> hMap = new HashMap<String, String>();
hMap.put("1", "One");
hMap.put("2", "Two");
hMap.put("3", "Three");
Set st = hMap.keySet();
Iterator itr = st.iterator();
while (itr.hasNext())
System.out.println(itr.next());
// remove 2 from Set
st.remove("2");
System.out.println(hMap.containsKey("2"));
}
}
HashMap: 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);
}
}
HashMap: put(E o, E o1)
/**
*Output:
D: 99.22
A: 3434.34
C: 1378.0
B: 123.22
E: -19.08
B"s new balance: 1123.22
*/
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class MainClass {
public static void main(String args[]) {
HashMap<String, Double> hm = new HashMap<String, Double>();
hm.put("A", new Double(3434.34));
hm.put("B", new Double(123.22));
hm.put("C", new Double(1378.00));
hm.put("D", new Double(99.22));
hm.put("E", new Double(-19.08));
Set<Map.Entry<String, Double>> set = hm.entrySet();
for (Map.Entry<String, Double> me : set) {
System.out.print(me.getKey() + ": ");
System.out.println(me.getValue());
}
System.out.println();
double balance = hm.get("B");
hm.put("B", balance + 1000);
System.out.println("B"s new balance: " + hm.get("B"));
}
}
HashMap: 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);
}
}
HashMap: 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());
}
}
HashMap: 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());
}
}
}
new HashMap < K, V > ()
/**
*Output:
D: 99.22
A: 3434.34
C: 1378.0
B: 123.22
E: -19.08
B"s new balance: 1123.22
*/
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class MainClass {
public static void main(String args[]) {
HashMap<String, Double> hm = new HashMap<String, Double>();
hm.put("A", new Double(3434.34));
hm.put("B", new Double(123.22));
hm.put("C", new Double(1378.00));
hm.put("D", new Double(99.22));
hm.put("E", new Double(-19.08));
Set<Map.Entry<String, Double>> set = hm.entrySet();
for (Map.Entry<String, Double> me : set) {
System.out.print(me.getKey() + ": ");
System.out.println(me.getValue());
}
System.out.println();
double balance = hm.get("B");
hm.put("B", balance + 1000);
System.out.println("B"s new balance: " + hm.get("B"));
}
}