Java容器

容器

1、基本概念

  (1)Collection。一个独立元素的序列,这些元素都服从一条或多条规则。List必须按照插入的顺序保存元素;Set不能有重复元素;Queue按照排队规则来确定对象产生的顺序。

  (2)Map。一组成对的“键值对”对象,允许你使用键来查找值。ArrayList允许你使用数字来查找值。

 

2、添加一组元素

  (1)Arrays.asList()方法接受一个数组或是一个用逗号分隔的元素列表,并将其转换为一个List对象。

1 Collection<Integer> collection = new ArrayList<Integer>(Arrays.asList(1,2,3,4,5,6));
2         Integer[] moreInts = {7,8,9};
3         collection.addAll(Arrays.asList(moreInts));
4 List<Integer> list = Arrays.asList(16,17,18);

  

  (2)Collections.addAll()方法接受一个Collection对象,以及一个数组或是一个用逗号分割的列表,将元素添加到Collection中。

1 Collections.addAll(collection,10,11,12);
2 Collections.addAll(collection,moreInts);

  

  (3)Collection的addAll()成员方法,只能接受另一个Collection对象作为参数。

1 Integer[] moreInts = {7,8,9};
2 collection.addAll(Arrays.asList(moreInts));

 

3、List

  List可以将元素维护在特定的序列中。

  (1)ArrayList:类似顺序表,可以随机访问元素,插入和移除元素时较慢。

  (2)LinkedList:链表,提供了优化的顺序访问,插入和删除操作代价小。

 

方法名作用
subList(1,3) 从较大列表中截取序号从1到3的元素生成一个新列表(包括索引值为1的元素,不包括索引值为3的元素)
Collection.shuffle() 随机打乱一个顺序数组
retainAll() 一种有效的“交集”操作

 

4、LinkedList

  LinkedList和ArrayList一样实现了基本的List接口,但是它执行在List中间插入和移除时比ArrayList更高效,但在随机访问操作方面却要逊色一些。

方法名作用
getFirst() 返回列表的头;List为空时抛出NoSuchElementException
element() 返回列表的头;List为空时抛出NoSuchElementException
peek() 返回列表的头;List为空时返回null
removeFirst() 移除并返回列表的头;List为空时抛出NoSuchElementException
remove() 移除并返回列表的头;List为空时抛出NoSuchElementException
removeLast() 移除并返回列表的最后一个元素
poll() 移除并返回列表的头;List为空时返回null
addFirst() 将某个元素插入到列表的头部
add() 将某个元素插入到列表的尾部
addLast() 将某个元素插入到列表的尾部

 

5、Stack

  “栈”通常是指“后进先出”(LIFO)的容器。


 1 public class Test {
 2  3     public static void main(String[] args) {
 4         Stack<String> stack = new Stack<String>();
 5         for(String s:"I am a clever boy".split(" ")){
 6             stack.push(s);
 7         }
 8         while(!stack.isEmpty()){
 9             System.out.print(stack.pop() + " ");
10         }
11     }
12 }

 

6、Set

  Set不保存重复的元素。Set具有与Collection完全一样的接口。

  (1)HashSet。使用了散列,输出的顺序没有任何规律可循。

  (2)TreeSet。将元素存储在红-黑树数据结构中,按照比较结构的升序保存对象。

  (3)LinkedHashSet。它按照添加的顺序保存对象。

 

7、Map

  Map保存了一组键值对,例如<key,value>。

方法名作用
put(key,value) 保存键值对
get(key) 获取对应的value
containsKey() 是否有相应的key
containsValue() 是否有相应的value
keySet() 获取所有的key

 

8、Queue

  队列是一个典型的先进先出(FIFO)的容器。即从容器的一端放入事物,从另一端取出,并且事物放入容器的顺序与取出的顺序是相同的。通过offer()方法将一个元素插入到队尾,通过remove()方法读取队头元素。

  PriorityQueue。优先级队列声明下一个弹出元素是最需要的元素(具有最高的优先级)。

 

9、迭代器

  迭代器是一个对象,它的工作是遍历并选择序列中的对象。迭代器通常被称为轻量级对象:创建它的代价小。

  (1)Iterator。只能单向移动。使用iterator()方法要求容器返回一个Iterator。使用next()获取下一个元素。使用hasNext()检查序列中是否还有元素。使用remove()将迭代器近返回的元素删除。


 1 public class Test {
 2     public static ArrayList<Integer> listofRandInteger(int length, int n)
 3     {
 4         Random rand = new Random();
 5         ArrayList<Integer> li = new ArrayList<Integer>();
 6         for(int i = 0; i < length; i++)
 7             li.add(rand.nextInt(n));
 8         return li;
 9     }
10 11     public static void display(Iterator<Integer> it){
12         System.out.print("[");
13         while (it.hasNext()){
14             Integer i = it.next();
15             System.out.print(i + " ");
16         }
17         System.out.println("]");
18     }
19 20     public static void main(String[] args) {
21         ArrayList<Integer> al = (ArrayList<Integer>) listofRandInteger(6,10);
22         LinkedList<Integer> ll = new LinkedList<Integer>(al);
23         HashSet<Integer> hs = new HashSet<Integer>(al);
24         TreeSet<Integer> ts = new TreeSet<Integer>(al);
25         display(al.iterator());
26         display(ll.iterator());
27         display(hs.iterator());
28         display(ts.iterator());
29     }
30 }

 

  (2)ListIterator。一个更加强大的Iterator的子类型,它只能用于各种List类型的访问,可以双向移动。使用listIterator()方法返回一个指向List开始处的ListIterator。使用listIterator(n)方法返回一个一开始就指向列表索引为n的元素处的ListIterator。


 1 public class Test {
 2     public static ArrayList<Integer> listofRandInteger(int length, int n)
 3     {
 4         Random rand = new Random();
 5         ArrayList<Integer> li = new ArrayList<Integer>();
 6         for(int i = 0; i < length; i++)
 7             li.add(rand.nextInt(n));
 8         return li;
 9     }
10 11 12     public static void main(String[] args) {
13         List<Integer> al = listofRandInteger(6,10);
14         ListIterator it = al.listIterator();
15         while (it.hasNext()){
16             System.out.print(it.next() + ",next:" + it.nextIndex() + ",previous:" + it.previousIndex() + "; ");
17         }
18         System.out.println("");
19         while (it.hasPrevious()){
20             System.out.print(it.previous() + " ");//将序列倒序输出
21         }
22         System.out.println("");
23         System.out.println(al);
24         it = al.listIterator(3);
25         int i = 0;
26         while (it.hasNext()){
27             it.next();
28             it.set(i++);
29         }
30         System.out.println(al);
31     }
32 }

 

 10、简单的容器分类

 

 

 

参考于《Java编程思想》,第216~247页

 

posted @ 2021-06-03 18:21  sumAll  阅读(70)  评论(0编辑  收藏  举报