JAVA容器对象整理
容器对象
1. 容器
1.1 泛型和类型安全
由于ArrayList等容器保存的是object类型.所以通过get()方法拿出来的对象还需要转型到你想要的类型,比如你定义储存Apple类对象的ArrayList容器appleList,你不但可以add Apple类型的对象,也可以add() Orange 类型的对象,因为无论是Apple类还是Orange类在放进容器里的时候已经向上转型为Object类型了.所以当程序运行时通过get方法,把本来是Orange类型强转型为Apple类型的时候就会抛出异常.
j2se5 引进泛型的机制,通过给容器指定类型,比如上述例子,ArrayList appleList<Apple>.这样编译器就会知道这个容器是专门储存Apple类,当add()方法储存Orange类时就会报错.
1.2 基本概念
1.2.1 Collection
独立元素
1.2.2 Map
键值对
1.3 添加一组元素
Collection<Integer> collection = new ArrayList<Integer>(Array.asList(1,2,3,4,5));Integer[] moreInrs = {6,7,8,9,10};collection.addAll(Arrays.asList(moreInts));Collections.addAll(collection,11,12,13,14,15);Collections.addAll(collection,moreInts);
import java.util.*;public class PrintingContainers { 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; } static void print(String collName,Collection collection){ System.out.println(collName+":"+collection); } static void print(String mapName,Map map){ System.out.println(mapName+":"+map); } public static void main(String[] args){ //List示例 print("ArrayList",fill(new ArrayList<String>())); print("LinkedList",fill(new LinkedList<String>())); //Set示例 print("HashSet",fill(new HashSet<String>())); print("TreeSet",fill(new TreeSet<String>())); print("LinkedHashSet",fill(new LinkedHashSet<String>())); //Map示例 print("HashMap",fill(new HashMap<String,String>())); print("TreeMap",fill(new TreeMap<String,String>())); print("LinkedHashMap",fill(new LinkedHashMap<String,String>())); }}/* Output:ArrayList:[rat, cat, dog, dog]LinkedList:[rat, cat, dog, dog]HashSet:[cat, dog, rat]TreeSet:[cat, dog, rat]LinkedHashSet:[rat, cat, dog]HashMap:{cat=Rags, dog=spot, rat=Fuzzy}TreeMap:{cat=Rags, dog=spot, rat=Fuzzy}LinkedHashMap:{rat=Fuzzy, cat=Rags, dog=spot}*/
import java.util.*;/* * 类名之后的<T>告诉编译器这将是一个参数化类型,是通用范型的应用,在运行时指定. */public class Stack<T> { private LinkedList<T> storage = new LinkedList<T>(); public void push(T v){ storage.addFirst(v);} public T peek(){ return storage.getFirst();} public T pop() {return storage.removeFirst();} public boolean empty(){return storage.isEmpty();} public String toString(){return storage.toString();}}
public class StackTest {/** * 在实际应用中看出,Stack<String>指定给泛型T为String类型,同 理也可以指定为其它类型 */public static void main(String[] args) {// TODO Auto-generated method stub Stack<String> stack = new Stack<String>(); for(String s:"My dog has fleas".split(" ")) stack.push(s); while(!stack.empty()) System.out.print(stack.pop()+" ");}}
import java.util.LinkedList;import java.util.Queue;import java.util.Random;public class QueueDemo {/** * 这里用java.util库中已经实现好的queue做例子 */public static void printQ(Queue queue){while(queue.peek()!=null) System.out.print(queue.remove()+" "); System.out.println();}public static void main(String[] args) { // TODO Auto-generated method stub Queue<Integer> queue = new LinkedList<Integer>(); Random rand = new Random(47); for(int i =0;i<10;i++) queue.offer(rand.nextInt(i+10)); printQ(queue); Queue<Character> qc =new LinkedList<Character>(); for(char c:"Brontosaurus".toCharArray()) qc.offer(c); printQ(qc); }}1.5.2.2.1 PriorityQueue
import java.util.*;public class PriorityQueueDemo {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubPriorityQueue<Integer> priorityQueue = new PriorityQueue<Integer>();Random rand = new Random(47);for(int i =0;i<10;i++)priorityQueue.offer(rand.nextInt(i+10));QueueDemo.printQ(priorityQueue);/* * 如果是数字队列的话 priorityQueue会默认按数字从小到大排序,Collections.reverseOrder()为产生反序的Comparator * 通过重载Comparator方法来按照自己的形式作比较 */List<Integer> ints = Arrays.asList(25,22,20,18,14,9,3,1,1,2,3,9,14,18,21,23,25);priorityQueue = new PriorityQueue<Integer>(ints);QueueDemo.printQ(priorityQueue);priorityQueue = new PriorityQueue<Integer>(ints.size(),Collections.reverseOrder());priorityQueue.addAll(ints);QueueDemo.printQ(priorityQueue);/* * 从下面例子看出,空格也包含在字母比较的优先级内 */String fact = "EDUCATION SHOULD ESCHEW OBFUSCATION";List<String> strings = Arrays.asList(fact.split(""));PriorityQueue<String> stringPQ = new PriorityQueue<String>(strings);QueueDemo.printQ(stringPQ);stringPQ = new PriorityQueue<String>(strings.size(),Collections.reverseOrder());stringPQ.addAll(strings);QueueDemo.printQ(stringPQ);//加入了hashset是为了消除重复元素作一个例子Set<Character> charSet = new HashSet<Character>();for(char c:fact.toCharArray())charSet.add(c);PriorityQueue<Character> characterPQ = new PriorityQueue<Character>(charSet);QueueDemo.printQ(characterPQ);}}
import java.util.*;public class Statistics {public static void main(String[] args) {// TODO Auto-generated method stubRandom rand = new Random(47);Map<Integer,Integer> m = new HashMap<Integer,Integer>();for(int i = 0;i<10000;i++){int r = rand.nextInt(40);Integer freq = m.get(r);m.put(r, freq == null?1:freq+1); //如果键不在map中,则freq返回null,表示第一次被找到,每次roll到相同的值则值递增并更新该键值对的值.}System.out.println(m);}}
import java.util.*;import cn.edu.zen.eleven.container.*;public class MapOfList {public static Map<Person,List<? extends Pet>> petPeople = new HashMap<Person,List<? extends Pet>>();static{petPeople.put(new Person("Dawn"),Arrays.asList(new Cat(1,"black"),new Dog(2,"B&W")));petPeople.put(new Person("Kate"), Arrays.asList(new Cat(3,"White"),new Cat(4,"Yellow"),new Dog(5,"Brown")));}public static void main(String[] args) {// TODO Auto-generated method stubSystem.out.println("People: "+petPeople.keySet());System.out.println("Pets:"+petPeople.values());/*Map可以返回它的Set,它的值的Collection,或它的键值对Set,keySet()方法产生了由petPeople中的所有键组成的Set.它在ForEach语句中被用来遍历Map*/for(Person person : petPeople.keySet()){System.out.print(person + " has:");for(Pet pet:petPeople.get(person))System.out.print(" "+pet);System.out.println();}}}
import java.util.*;public class InterfaceVsIterator { //Iterator和Collection都可以将display()方法与底层容器实现解耦public static void display(Iterator<Pet> it){while(it.hasNext()) System.out.print(it.next());System.out.println(); }public static void display(Collection<Pet> pets){for(Pet p:pets)System.out.print(p);System.out.println();}public static void main(String[] args) {// TODO Auto-generated method stubList<Pet> petList = (List<Pet>) Pets.arrayList();Set<Pet> petSet = new HashSet<Pet>(petList);Map<String,Pet> petMap = new LinkedHashMap<String,Pet>();for(Pet pet:petList)petMap.put(pet.petName, pet);display(petList);display(petSet);display(petList.iterator());display(petSet.iterator());System.out.print(petMap);System.out.println();System.out.print(petMap.keySet());System.out.println();display(petMap.values());display(petMap.values().iterator());}}//但是要实现一个不是Collection的外部类时,由于让它去实现Collection接口可能非常困难和麻烦,因此使用iterator会变得非常吸引人.import java.util.*;public class PetSequence { protected List<Pet> pets = Arrays.asList(new Pet(1,"Cat"),new Pet(2,"Dog"),new Pet(3,"Rabbit"),new Pet(4,"Fish"),new Pet(5,"Bird"));}public class NonCollectionSequence extends PetSequence {public Iterator<Pet> iterator(){return new Iterator<Pet>(){private int index = 0;public boolean hasNext(){return index<pets.size();}public Pet next(){return pets.get(index++);}public void remove(){throw new UnsupportedOperationException();}};}public static void main(String[] args){NonCollectionSequence nc =new NonCollectionSequence();Iterator it =nc.iterator();while(it.hasNext())System.out.println(it.next());}}
import java.util.Iterator;public class IterableClass implements Iterable<String> {protected String[] words = ("And that is how"+" we know the Earth to be banana-shaped.").split(" ");public Iterator<String> iterator() {return new Iterator<String>(){private int index = 0;public boolean hasNext(){return index<words.length;}public String next(){ return words[index++];}public void remove(){ throw new UnsupportedOperationException(); }};}public static void main(String[] args){for(String s:new IterableClass())System.out.print(s+" ");}}
//: holding/EnvironmentVariables.javaimport java.util.*;public class EnvironmentVariables { public static void main(String[] args) { for(Map.Entry entry: System.getenv().entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue()); } }} /* (Execute to see output) *///:~3.1 适配器方法惯用法
import java.util.*;public class MultiIterableClass extends IterableClass {public Iterable<String> reversed() { return new Iterable<String>() { public Iterator<String> iterator() { return new Iterator<String>() { int current = words.length - 1; public boolean hasNext() { return current > -1; } public String next() { return words[current--]; } public void remove() { // Not implemented throw new UnsupportedOperationException(); } }; } };}/*注意randomize()并没有返回它自己的Iterator,只返回被打乱顺序的List中的Iterator.Collection.shuffe()方法并没有打乱原来的数组,因为randomized方法将Arrays.asList()的结果包装起来.相当于备份了一个list的副本上打乱顺序*/public Iterable<String> randomized() { return new Iterable<String>() { public Iterator<String> iterator() { List<String> shuffled = new ArrayList<String>(Arrays.asList(words)); Collections.shuffle(shuffled, new Random(47)); return shuffled.iterator(); } };}public static void main(String[] args) { MultiIterableClass mic = new MultiIterableClass(); for(String s : mic.reversed()) System.out.print(s + " "); System.out.println(); for(String s : mic.randomized()) System.out.print(s + " "); System.out.println(); for(String s : mic) System.out.print(s + " ");}}