首页 > 代码库 > Java 之容器学习(接上篇)

Java 之容器学习(接上篇)

/**
 * 
 * 再谈容器
 * @author gao
 */
package com.gao.thinkinjava;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.TreeMap;
import java.util.TreeSet;

public class PrintingContainers {
	static void Print(Object c){
		System.out.println(c);
	}
     static Collection fill(Collection<String>collection){
    	 collection.add("rat");
    	 collection.add("cat");
    	 collection.add("dog");
    	 collection.add("dog");
    	 return collection;
     }
     static Map fill(Map<String,String>map){
    	 map.put("rat","Fuzzy");
    	 map.put("cat", "Rags");
    	 map.put("dog", "Bosco");
    	 map.put("dog", "spot");
    	 return map;
     }
     public static void main(String[]args){
    	 Print(fill(new ArrayList<String>()));
    	 Print(fill(new LinkedList<String>()));
    	 Print(fill(new HashSet<String>()));
    	 Print(fill(new TreeSet<String>()));
    	 Print(fill(new LinkedHashSet<String>()));
    	 Print(fill(new HashMap<String,String>()));
    	 Print(fill(new TreeMap<String,String>()));
    	 Print(fill(new LinkedHashMap<String,String>()));
     }
}


输出结果:

[rat, cat, dog, dog]
[rat, cat, dog, dog]
[cat, dog, rat]
[cat, dog, rat]
[rat, cat, dog]
{cat=Rags, dog=spot, rat=Fuzzy}
{cat=Rags, dog=spot, rat=Fuzzy}
{rat=Fuzzy, cat=Rags, dog=spot}