- 11 . java容器就是用来保存对象 除了数组外 还有集合
分为:Collection,Map 两大类
Collection:List顺序保存元素,Set不能有重复元素,Queue按照排队来
Map: 按照键值对保存元素 - 11.1 List
11.1.1 ArrayList 擅长于访问元素 但是在List的中间插入元素和移除元素就较慢
package com.zlb.list;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
class Fruit{
public Fruit(){}
}
class Apple extends Fruit{}
class Banana extends Fruit{}
class Grape extends Fruit{}
class Pineapple extends Fruit{}
class Peach extends Fruit{}
class Strawberry extends Fruit{}
class Mango extends Fruit{}
class Cherry extends Fruit{}
class Pawpaw extends Fruit{}
class Grapefruit extends Fruit{}
public class arrayList {
public static List<Fruit> list(){
ArrayList<Fruit> list = new ArrayList<Fruit>();
Fruit[] fruits = {new Banana(),new Grape(),new Pineapple(),new Peach(),new Strawberry(),new Mango()};
Collections.addAll(list, fruits);
return list;
}
public static void main(String[] args) {
Random rand = new Random(47);
List<Fruit> list = list();
System.out.println("1:"+list);
Fruit cherry = new Cherry();
list.add(cherry);
System.out.println("2:"+list);
System.out.println("3:"+list.contains(cherry));
list.remove(cherry);
Fruit fruit = list.get(2);
System.out.println("4:"+fruit+"\t"+list.indexOf(fruit));
Fruit cherry1 = new Cherry();;
System.out.println("5:"+list.indexOf(cherry1));
System.out.println("6:"+list.remove(cherry1));
System.out.println("7:"+list.remove(fruit));
System.out.println("8:"+list);
list.add(3,new Grapefruit());
System.out.println("9:"+list);
List<Fruit> sub = list.subList(0, 3);
System.out.println("10:"+sub);
System.out.println("11:"+list.containsAll(sub));
Collections.shuffle(sub, rand);
System.out.println("12:"+sub);
}
}
11.1.2 LinkList 实现了List的基本接口 在插入和移除操作时较快 但在查询时较慢
package com.zlb.list;
import java.util.LinkedList;
public class linkList {
public static void main(String[] args) {
LinkedList<Fruit> list = new LinkedList<Fruit>(arrayList.list());
//list.push(new Grape());
System.out.println("list.getFirst():"+list.getFirst());//得到表头
System.out.println("list.pop():"+list.pop());//得到表头 并移除表头
System.out.println("list.getFirst():"+list.getFirst());//得到表头
System.out.println("list.element():"+list.element());//得到元素
System.out.println("list.getLast():"+list.getLast());//得到最后一个
System.out.println("list.peek():"+list.peek());//返回表头
System.out.println("list.peekFirst():"+list.peekFirst());//移除最后一个
System.out.println("list.pop():"+list.pop());//返回表头 在移除表头
System.out.println("list.getFirst():"+list.getFirst());//得到表头
System.out.println("list.remove():"+list.remove(1));//根据索引移除元素
System.out.println("list.removeFirst():"+list.removeFirst());//移除表头
System.out.println("list.removeLast():"+list.removeLast());//移除最后一个
}
}
- 11.2 迭代器
迭代器是一个对象 用来遍历并选择序列中的对象,java中的迭代器只能单向的向前移动:
(1) 使用一个方法iterator()要求容器返回一个Iterator
(2) 使用next()会的序列中的下一个元素
(3)使用hasnext()检测序列中是否还有元素
(4)使用remove()将迭代器中新返回的元素删除
Iterator<Fruit> it = list.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
for(Fruit str : list){
System.out.println(str.toString());
}
- 11.3 Set
Set不保存重复的元素
HashSet 使用了散列存储方式 因此元素是无序的
TreeSet 使用了基于红黑树的数据结构的存储方式因此元素是有序的
package com.zlb.list;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class set {
public static void main(String[] args) {
Set<String> set = new HashSet<String>();
Collections.addAll(set, "E F G H I J K K A A B C C D".split(" "));
Iterator<String> ite = set.iterator();
while(ite.hasNext()){
System.out.print(ite.next());
}
set.add("L");
set.add("V");
set.add("W");
Set<String> set1 = new HashSet<String>(set);
//System.out.println(set.contains(set1));
System.out.println(set1.contains(set));
}
}
- 11.4 Map(以键值对的方式存储数据)
Map中的HashMap不保证映射顺序,保证基本操作(get 和 put)提供稳定的性能,那么LinkedHashMap就通过一个双重链表保证了顺序(迭代顺序)
package com.zlb.list;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class map {
public static Map<String,List<? extends Fruit>> fruit = new HashMap<String,List<? extends Fruit>>();
static {
fruit.put("Banana", Arrays.asList(new Banana(),new Grape()));
fruit.put("Pineapple", Arrays.asList(new Pineapple(),new Peach()));
fruit.put("Strawberry", Arrays.asList(new Strawberry(),new Mango(),new Cherry()));
}
public static void main(String[] args) {
System.out.println("["+fruit.keySet()+"="+fruit.values()+"]");
for(String str : fruit.keySet()){
for(Fruit frui : fruit.get(str)){
System.out.println(frui);
}
}
}
}
- 11.5 Stack (栈是一种先进后出的容器,LinkList具有实现Stack所有功能的方法)
package com.zlb.list;
import java.util.Stack;
public class stack {
public static void main(String[] args) {
Stack<String> stack = new Stack<String>();
for(String str : "My,Name,Is,Zhou,li,bin".split(",")){
stack.push(str);//压入栈 先进后出
}
while(!stack.empty()){
System.out.println(stack.pop());//返回表头 并移除表头
System.out.println(stack.peek());//返回表头
}
}
}
- 11.6 Queue(队列是一种先进先出的容器,事务放入的容器的顺序与取出的顺序是相同的,LinkList实现了Queue的接口)
package com.zlb.list;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
public class queue {
public static void printQ(Queue queue){
while(queue.peek()!=null){//返回表头
//System.out.print(queue.peek()+"\t");
//System.out.println();
System.out.print(queue.remove()+" ");//移除表头并回
}
}
public static void main(String[] args) {
Queue<Integer> queue1 = new LinkedList<Integer>();
Queue<Character> queue2 = new LinkedList<Character>();
Random rand = new Random(47);
for(int i=0;i<10;i++){
queue1.offer(rand.nextInt(i+10));
}
for(char c : "zhoulibin".toCharArray()){
queue2.offer(c);
}
// printQ(queue1);
printQ(queue2);
}
}
网友评论