Java容器
2014-04-19 13:51
337 查看
第十一章:持有对象(Thinking in Java)
为了随时随地创建任意多的对象,java中引入容器的概念。
使用容器时使用泛型,使得一些问题在编译时就被发现,更安全。
Java容器类用途是“保存对象”,可以分为两类:
1.Collection:独立的元素的序列。List按照插入顺序保存元素,Set不允许重复,Queue是队列(这样说似乎不是十分准确,但又有一定道理吧)
2.Map:成对出现。ArrayList有索引。
我们其实真正做的经常是向上转型成接口:
添加一组元素,直接看例子:
容器的打印直接打印就好了,默认的打印输出效果也不错:
List:ArrayList随机访问,LinkedList插入删除,特征集较大:
迭代器,这是个好东西,应该好好理解理解:
ListIterator:继承自Iterator,可以双向移动,可以返回前后索引,可以使用Set()方法替换最后一次访问的元素:
LinkedList:添加了使其用作栈、队列、和双端队列的方法,其中有许多方法是很相似的:
Set:不重复元素:
Map:
Map可以扩展到多维:
Collection和Iterator:java.util.AbstractCollection提供了Collection的默认实现。一个不是Collection的外部类难以实现Collection,但可以使用Iterator:对比以下代码:
任何实现了Iterator的类都能用于foreach,因为Iterable接口包含了能产生Iterator的iterator()方法,Iterable接口能被foreach用来在序列中移动:
适配器方法:在这里,添加一个方法,返回Iterable的对象:
为了随时随地创建任意多的对象,java中引入容器的概念。
使用容器时使用泛型,使得一些问题在编译时就被发现,更安全。
Java容器类用途是“保存对象”,可以分为两类:
1.Collection:独立的元素的序列。List按照插入顺序保存元素,Set不允许重复,Queue是队列(这样说似乎不是十分准确,但又有一定道理吧)
2.Map:成对出现。ArrayList有索引。
我们其实真正做的经常是向上转型成接口:
List<apple> apples = new ArrayList<Apple>(); List<Apple> apples = new LinkedList<Apple>();这样改起来方便。
添加一组元素,直接看例子:
package holding; // thinking in java P220 //: holding/AddingGroups.java // Adding groups of elements to Collection objects. import java.util.*; public class AddingGroups { public static void main(String[] args) { Collection<Integer> collection = new ArrayList<Integer>(Arrays.asList(1, 2, 3, 4, 5)); //asList接受用逗号分隔的元素列表 Integer[] moreInts = { 6, 7, 8, 9, 10 }; collection.addAll(Arrays.asList(moreInts)); //asList接受数组 // Runs significantly faster, but you can't // construct a Collection this way: Collections.addAll(collection, 11, 12, 13, 14, 15); Collections.addAll(collection, moreInts); //addAll接受Collection、数组、逗号分隔元素列表 // Produces a list "backed by" an array: List<Integer> list = Arrays.asList(16, 17, 18, 19, 20); list.set(1, 99); // OK -- modify an element // list.add(21); // Runtime error because the // underlying array cannot be resized. } } ///:~初始化Map只能用Map。
容器的打印直接打印就好了,默认的打印输出效果也不错:
package holding; //thinking in java P221 //: holding/PrintingContainers.java // Containers print themselves automatically. import java.util.*; import static net.mindview.util.Print.*; 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; } public static void main(String[] args) { print(fill(new ArrayList<String>())); print(fill(new LinkedList<String>())); //Set print(fill(new HashSet<String>())); print(fill(new TreeSet<String>())); print(fill(new LinkedHashSet<String>())); //Map print(fill(new HashMap<String,String>())); print(fill(new TreeMap<String,String>())); print(fill(new LinkedHashMap<String,String>())); } } /* Output: [rat, cat, dog, dog] [rat, cat, dog, dog] [dog, cat, rat] [cat, dog, rat] [rat, cat, dog] {dog=Spot, cat=Rags, rat=Fuzzy} {cat=Rags, dog=Spot, rat=Fuzzy} {rat=Fuzzy, cat=Rags, dog=Spot} *///:~
List:ArrayList随机访问,LinkedList插入删除,特征集较大:
package holding; //thinking in java P223 //: holding/ListFeatures.java import typeinfo.pets.*; import java.util.*; import static net.mindview.util.Print.*; public class ListFeatures { public static void main(String[] args) { Random rand = new Random(47); List<Pet> pets = Pets.arrayList(7); print("1: " + pets); Hamster h = new Hamster(); pets.add(h); // Automatically resizes print("2: " + pets); print("3: " + pets.contains(h)); pets.remove(h); // Remove by object Pet p = pets.get(2); print("4: " + p + " " + pets.indexOf(p)); Pet cymric = new Cymric(); print("5: " + pets.indexOf(cymric)); print("6: " + pets.remove(cymric)); // Must be the exact object: print("7: " + pets.remove(p)); print("8: " + pets); pets.add(3, new Mouse()); // Insert at an index print("9: " + pets); List<Pet> sub = pets.subList(1, 4); print("subList: " + sub); print("10: " + pets.containsAll(sub)); Collections.sort(sub); // In-place sort print("sorted subList: " + sub); // Order is not important in containsAll(): print("11: " + pets.containsAll(sub)); Collections.shuffle(sub, rand); // Mix it up print("shuffled subList: " + sub); print("12: " + pets.containsAll(sub)); List<Pet> copy = new ArrayList<Pet>(pets); sub = Arrays.asList(pets.get(1), pets.get(4)); print("sub: " + sub); copy.retainAll(sub); print("13: " + copy); copy = new ArrayList<Pet>(pets); // Get a fresh copy copy.remove(2); // Remove by index print("14: " + copy); copy.removeAll(sub); // Only removes exact objects print("15: " + copy); copy.set(1, new Mouse()); // Replace an element print("16: " + copy); copy.addAll(2, sub); // Insert a list in the middle print("17: " + copy); print("18: " + pets.isEmpty()); pets.clear(); // Remove all elements print("19: " + pets); print("20: " + pets.isEmpty()); pets.addAll(Pets.arrayList(4)); print("21: " + pets); Object[] o = pets.toArray(); print("22: " + o[3]); Pet[] pa = pets.toArray(new Pet[0]); print("23: " + pa[3].id()); } } /* Output: 1: [Rat, Manx, Cymric, Mutt, Pug, Cymric, Pug] 2: [Rat, Manx, Cymric, Mutt, Pug, Cymric, Pug, Hamster] 3: true 4: Cymric 2 5: -1 6: false 7: true 8: [Rat, Manx, Mutt, Pug, Cymric, Pug] 9: [Rat, Manx, Mutt, Mouse, Pug, Cymric, Pug] subList: [Manx, Mutt, Mouse] 10: true sorted subList: [Manx, Mouse, Mutt] 11: true shuffled subList: [Mouse, Manx, Mutt] 12: true sub: [Mouse, Pug] 13: [Mouse, Pug] 14: [Rat, Mouse, Mutt, Pug, Cymric, Pug] 15: [Rat, Mutt, Cymric, Pug] 16: [Rat, Mouse, Cymric, Pug] 17: [Rat, Mouse, Mouse, Pug, Cymric, Pug] 18: false 19: [] 20: true 21: [Manx, Cymric, Rat, EgyptianMau] 22: EgyptianMau 23: 14 *///:~
迭代器,这是个好东西,应该好好理解理解:
package holding; //thinking in java P226 //: holding/SimpleIteration.java import typeinfo.pets.*; import java.util.*; public class SimpleIteration { public static void main(String[] args) { List<Pet> pets = Pets.arrayList(12); Iterator<Pet> it = pets.iterator(); /** * pets是arrayList的对象,arrayList已经实现了接口iterator,则 * it就可以直接使用.next()和.hasNext((). */ while(it.hasNext()) { Pet p = it.next(); System.out.print(p.id() + ":" + p + " "); } System.out.println(); // A simpler approach, when possible: for(Pet p : pets)//foreach也很好用 System.out.print(p.id() + ":" + p + " "); System.out.println(); // An Iterator can also remove elements: it = pets.iterator(); for(int i = 0; i < 6; i++) { it.next(); it.remove(); } System.out.println(pets); } } /* Output: 0:Rat 1:Manx 2:Cymric 3:Mutt 4:Pug 5:Cymric 6:Pug 7:Manx 8:Cymric 9:Rat 10:EgyptianMau 11:Hamster 0:Rat 1:Manx 2:Cymric 3:Mutt 4:Pug 5:Cymric 6:Pug 7:Manx 8:Cymric 9:Rat 10:EgyptianMau 11:Hamster [Pug, Manx, Cymric, Rat, EgyptianMau, Hamster] *///:~
ListIterator:继承自Iterator,可以双向移动,可以返回前后索引,可以使用Set()方法替换最后一次访问的元素:
package holding; //thinking in java P228 //: holding/ListIteration.java import typeinfo.pets.*; import java.util.*; public class ListIteration { public static void main(String[] args) { List<Pet> pets = Pets.arrayList(8); ListIterator<Pet> it = pets.listIterator();//创建listIterator对象 while(it.hasNext()) System.out.print(it.next() + ", " + it.nextIndex() + ", " + it.previousIndex() + "; "); System.out.println(); // Backwards: while(it.hasPrevious()) System.out.print(it.previous().id() + " "); System.out.println(); System.out.println(pets); it = pets.listIterator(3);//直接指向索引为3的元素的ListIterator while(it.hasNext()) { it.next(); it.set(Pets.randomPet());//替换 } System.out.println(pets); } } /* Output: Rat, 1, 0; Manx, 2, 1; Cymric, 3, 2; Mutt, 4, 3; Pug, 5, 4; Cymric, 6, 5; Pug, 7, 6; Manx, 8, 7; 7 6 5 4 3 2 1 0 [Rat, Manx, Cymric, Mutt, Pug, Cymric, Pug, Manx] [Rat, Manx, Cymric, Cymric, Rat, EgyptianMau, Hamster, EgyptianMau] *///:~
LinkedList:添加了使其用作栈、队列、和双端队列的方法,其中有许多方法是很相似的:
package holding; //thinking in java P229 //: holding/LinkedListFeatures.java import typeinfo.pets.*; import java.util.*; import static net.mindview.util.Print.*; public class LinkedListFeatures { public static void main(String[] args) { LinkedList<Pet> pets = new LinkedList<Pet>(Pets.arrayList(5)); print(pets); // Identical: print("pets.getFirst(): " + pets.getFirst());//返回列表的头//空抛出异常 print("pets.element(): " + pets.element()); // Only differs in empty-list behavior: print("pets.peek(): " + pets.peek());//空返回null // Identical; remove and return the first element: print("pets.remove(): " + pets.remove());//移除或返回列表的头//空抛出异常 print("pets.removeFirst(): " + pets.removeFirst()); // Only differs in empty-list behavior: print("pets.poll(): " + pets.poll());//空返回null print(pets); pets.addFirst(new Rat());//将某元素插入到列表的尾部 print("After addFirst(): " + pets); pets.offer(Pets.randomPet()); print("After offer(): " + pets); pets.add(Pets.randomPet()); print("After add(): " + pets); pets.addLast(new Hamster()); print("After addLast(): " + pets); print("pets.removeLast(): " + pets.removeLast()); } } /* Output: [Rat, Manx, Cymric, Mutt, Pug] pets.getFirst(): Rat pets.element(): Rat pets.peek(): Rat pets.remove(): Rat pets.removeFirst(): Manx pets.poll(): Cymric [Mutt, Pug] After addFirst(): [Rat, Mutt, Pug] After offer(): [Rat, Mutt, Pug, Cymric] After add(): [Rat, Mutt, Pug, Cymric, Pug] After addLast(): [Rat, Mutt, Pug, Cymric, Pug, Hamster] pets.removeLast(): Hamster *///:~
Set:不重复元素:
package holding; //thinking in java P232 //: holding/SortedSetOfInteger.java import java.util.*; public class SortedSetOfInteger { public static void main(String[] args) { Random rand = new Random(47); SortedSet<Integer> intset = new TreeSet<Integer>(); for(int i = 0; i < 10000; i++) intset.add(rand.nextInt(30)); System.out.println(intset); } } /* Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29] *///:~
Map:
package holding; //thinking in java P234 //: holding/PetMap.java import typeinfo.pets.*; import java.util.*; import static net.mindview.util.Print.*; public class PetMap { public static void main(String[] args) { Map<String,Pet> petMap = new HashMap<String,Pet>(); petMap.put("My Cat", new Cat("Molly")); petMap.put("My Dog", new Dog("Ginger")); petMap.put("My Hamster", new Hamster("Bosco")); print(petMap); Pet dog = petMap.get("My Dog"); print(dog); print(petMap.containsKey("My Dog")); print(petMap.containsValue(dog)); } } /* Output: {My Cat=Cat Molly, My Hamster=Hamster Bosco, My Dog=Dog Ginger} Dog Ginger true true *///:~
Map可以扩展到多维:
package holding; //thinking in java P235 //: holding/MapOfList.java import typeinfo.pets.*; import java.util.*; import static net.mindview.util.Print.*; 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 Cymric("Molly"),new Mutt("Spot"))); petPeople.put(new Person("Kate"), Arrays.asList(new Cat("Shackleton"), new Cat("Elsie May"), new Dog("Margrett"))); petPeople.put(new Person("Marilyn"), Arrays.asList( new Pug("Louie aka Louis Snorkelstein Dupree"), new Cat("Stanford aka Stinky el Negro"), new Cat("Pinkola"))); petPeople.put(new Person("Luke"), Arrays.asList(new Rat("Fuzzy"), new Rat("Fizzy"))); petPeople.put(new Person("Isaac"), Arrays.asList(new Rat("Freckly"))); } public static void main(String[] args) { print("People: " + petPeople.keySet()); print("Pets: " + petPeople.values()); for(Person person : petPeople.keySet()) { print(person + " has:"); for(Pet pet : petPeople.get(person)) print(" " + pet); } } } /* Output: People: [Person Luke, Person Marilyn, Person Isaac, Person Dawn, Person Kate] Pets: [[Rat Fuzzy, Rat Fizzy], [Pug Louie aka Louis Snorkelstein Dupree, Cat Stanford aka Stinky el Negro, Cat Pinkola], [Rat Freckly], [Cymric Molly, Mutt Spot], [Cat Shackleton, Cat Elsie May, Dog Margrett]] Person Luke has: Rat Fuzzy Rat Fizzy Person Marilyn has: Pug Louie aka Louis Snorkelstein Dupree Cat Stanford aka Stinky el Negro Cat Pinkola Person Isaac has: Rat Freckly Person Dawn has: Cymric Molly Mutt Spot Person Kate has: Cat Shackleton Cat Elsie May Dog Margrett *///:~Queue(队列)在LinkedList基础上添加了element(),offer(),peek(),poll(),remove()方法,(Queue是接口,LinkedList实现了这个接口):
package holding; //thinking in java P236 //: holding/QueueDemo.java // Upcasting to a Queue from a LinkedList. import java.util.*; public class QueueDemo { public static void printQ(Queue<?> queue) { while(queue.peek() != null) System.out.print(queue.remove() + " "); System.out.println(); } public static void main(String[] args) { 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); } } /* Output: 8 1 1 1 5 14 3 1 0 1 B r o n t o s a u r u s *///:~
Collection和Iterator:java.util.AbstractCollection提供了Collection的默认实现。一个不是Collection的外部类难以实现Collection,但可以使用Iterator:对比以下代码:
package holding; //thinking in java P240 //: holding/CollectionSequence.java import typeinfo.pets.*; import java.util.*; public class CollectionSequence extends AbstractCollection<Pet> { private Pet[] pets = Pets.createArray(8); public int size() { return pets.length; }//必须要实现iterator()和size() public Iterator<Pet> iterator() { return new Iterator<Pet>() { private int index = 0; public boolean hasNext() { return index < pets.length; } public Pet next() { return pets[index++]; } public void remove() { // Not implemented throw new UnsupportedOperationException(); } }; } public static void main(String[] args) { CollectionSequence c = new CollectionSequence(); InterfaceVsIterator.display(c); InterfaceVsIterator.display(c.iterator()); } } /* Output: 0:Rat 1:Manx 2:Cymric 3:Mutt 4:Pug 5:Cymric 6:Pug 7:Manx 0:Rat 1:Manx 2:Cymric 3:Mutt 4:Pug 5:Cymric 6:Pug 7:Manx *///:~ package holding; //thinking in java P240 //: holding/NonCollectionSequence.java import typeinfo.pets.*; import java.util.*; class PetSequence { protected Pet[] pets = Pets.createArray(8); } public class NonCollectionSequence extends PetSequence { //如果已经继承了其他的类那就不能继承AbstractCollection,那就创建迭代器吧 public Iterator<Pet> iterator() { return new Iterator<Pet>() { private int index = 0; public boolean hasNext() { return index < pets.length; } public Pet next() { return pets[index++]; } public void remove() { // Not implemented throw new UnsupportedOperationException(); } }; } public static void main(String[] args) { NonCollectionSequence nc = new NonCollectionSequence(); InterfaceVsIterator.display(nc.iterator()); } } /* Output: 0:Rat 1:Manx 2:Cymric 3:Mutt 4:Pug 5:Cymric 6:Pug 7:Manx *///:~
任何实现了Iterator的类都能用于foreach,因为Iterable接口包含了能产生Iterator的iterator()方法,Iterable接口能被foreach用来在序列中移动:
package holding; //thinking in java P241 //: holding/IterableClass.java // Anything Iterable works with foreach. import java.util.*; 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() { // Not implemented throw new UnsupportedOperationException(); } }; } public static void main(String[] args) { for(String s : new IterableClass()) System.out.print(s + " "); } } /* Output: And that is how we know the Earth to be banana-shaped. *///:~所有的Collection是Iterable的,但不包括Map,数组可以用于foreach,但是数组不是Iterable的。
适配器方法:在这里,添加一个方法,返回Iterable的对象:
package holding; //thinking in java P243 //: holding/AdapterMethodIdiom.java // The "Adapter Method" idiom allows you to use foreach // with additional kinds of Iterables. import java.util.*; class ReversibleArrayList<T> extends ArrayList<T> { public ReversibleArrayList(Collection<T> c) { super(c); } public Iterable<T> reversed() { return new Iterable<T>() { public Iterator<T> iterator() { return new Iterator<T>() { int current = size() - 1; public boolean hasNext() { return current > -1; } public T next() { return get(current--); } public void remove() { // Not implemented throw new UnsupportedOperationException(); } }; } }; } } public class AdapterMethodIdiom { public static void main(String[] args) { ReversibleArrayList<String> ral = new ReversibleArrayList<String>( Arrays.asList("To be or not to be".split(" "))); // Grabs the ordinary iterator via iterator(): for(String s : ral) System.out.print(s + " "); System.out.println(); // Hand it the Iterable of your choice for(String s : ral.reversed()) System.out.print(s + " "); } } /* Output: To be or not to be be to not or be To *///:~
相关文章推荐
- java图形图片选择应用程序
- org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document fro
- velocity学习记录之一(velocity基本概念)
- eclipse启动报错,让查看.metadata/.log日志
- spring aop xml方式
- JAVA static关键字小结
- 运用Hashtables--Java
- MyEclipse 8.5 修改默认工作空间和设置提示选择工作空间的对话框
- JAVA泛型详解(二)
- JAVA WEB项目中各种路径的获取
- java接口interface前的修饰
- JAVA中堆栈和内存分配原理
- Spring事务传播机制&隔离级别
- java中的POJO是什么意思?
- Java中弱引用、软引用、虚引用及强引用的区别
- 最大子数组java实现
- Installing the JDK with JAVA
- java使用过程中知识点记录
- java中synchronized的用法详解
- ubuntu IDE(eclipse ,intellij)安装