1 import java.util.*; 2 public class TestMap { 3 public static void main(String args[]) { 4 Map m1 = new HashMap(); 5 Map m2 = new TreeMap(); 6 //m1.put("one",new Integer(1)); 7 m1.put("one", 1); 8 //m1.put("two",new Integer(2)); 9 m1.put("two", 2); 10 //m1.put("three",new Integer(3)); 11 m1.put("three", 3); 12 //m2.put("A",new Integer(1)); 13 m2.put("A", 1); 14 //m2.put("B",new Integer(2)); 15 m2.put("B", 2); 16 System.out.println(m1.size()); 17 System.out.println(m1.containsKey("one")); 18 System.out.println 19 //(m2.containsValue(new Integer(1))); 20 (m2.containsValue(1)); 21 if(m1.containsKey("two")) { 22 //int i = ((Integer)m1.get("two")).intValue(); 23 int i = (Integer)m1.get("two"); 24 System.out.println(i); 25 } 26 Map m3 = new HashMap(m1); 27 m3.putAll(m2); 28 System.out.println(m3); 29 } 30 }
1 import java.util.*; 2 public class TestArgsWords { 3 //private static final Integer ONE = new Integer(1); 4 private static final int ONE = 1; 5 public static void main(String args[]) { 6 Map m = new HashMap(); 7 for (int i = 0; i < args.length; i++) { 8 //Integer freq = (Integer) m.get(args[i]); 9 10 int freq = (Integer) m.get(args[i]) == null ? 0 : (Integer) m.get(args[i]); 11 //m.put(args[i],(freq == null? ONE : new Integer(freq.intValue() + 1))); 12 m.put(args[i], freq==0 ? ONE : freq + 1); 13 } 14 System.out.println 15 (m.size() + " distinct words detected:"); 16 System.out.println(m); 17 } 18 }
时间: 2024-12-28 00:50:59