Generics and automatic packing and unpacking of the integrated use

 Generic JDK1.5 started

While a set of objects defined in the definition when the type of collection

You can specify when defining Collection

You may be used to specify the cycle Iteratorr

benefit:

  Enhance readability and stability of the program.

import java.util.List;
import java.util.LinkedList;
import java.util.Collections;
import java.util.*;
public class Test {
	public static void main(String[] args) {
		Map<String,Integer> m1 = new HashMap<String,Integer>();Map m2<String,Integer> = new TreeMap<String,Integer>();
		//m1.put("one",new Integer(1));
		m1.put("one",1);
		//m1.put("two",new Integer(2));
		m1.put("two",2);
		//m1.put("three",new Integer(3));
		m1.put("three",3);
		
		//m2.put("A",new Integer(1));
		m2.put("A",1);
		//m2.put("B",new Integer(2));
		m2.put("B",2);
		System.out.println(m1.size());
		System.out.println(m1.containsKey("one"));
		System.out.println(m2.containsValue(1));
		//(m2.containsValue(new Integer(1)));
		
		if(m1.containsKey("two")) {
			//int i = ((Integer)m1.get("two")).intValue();
			//int i = (Integer)m1.get("two");
			int i = m1.get("two");
			System.out.println(i);
		}
     }
}

 

import java.util.*;
public class TestArgsWords {
	//private static final Integer ONE = new Integer(1);
	private static final int ONE = 1;
	public static void main(String[] args) {
		Map<String,Integer> m = new HashMap<String,Integer>();
		for(int i = 0;i<args.length;i++) {
			
			if(!m.containsKey(args[i])) {
				m.put(args[i],ONE);
			} else {
				int freq = m.get(args[i]);
				m.put(args[i],freq+1);
			}
			
			
			
			
			
			
			
			
			/*
			//Integer freq = (Integer)m.get(args[i]);
			int freq = (Integer)m.get(args[i]) == null ? 0 :(Integer)m.get(args[i]) ;
			//m.put(args[i],(freq == null ? ONE :new Integer(freq.intValue()+1)));
			m.put(args[i],freq == 0 ? ONE : freq + 1);
			*/
			
		
		
		
		}
		System.out.println(m.size()+" distinct words detected");
		System.out.println(m); 
	}
}

 

Guess you like

Origin www.cnblogs.com/lsswudi/p/11368704.html