玖叶教程网

前端编程开发入门

读书笔记之Java编程思想第10章-内部类

可以将一个类的定义放在另一个类的内部,这就是内部类,内部类自动拥有对其外围类所有成员的访问权。

容器分为Collection和Map,Collection分为List,Set,Queue,Stack

Stack,“栈”通常指后进先出的容器,有时栈也被称为叠加栈,因为最后压入栈的元素,第一个弹出栈。

Queue,队列是一个典型的先进先出的容器,,即从容器的一端放入事物,从另一端取出,并且事物放入容器的顺序与取出的顺序是相同的。

如果要进行大量的随机访问,就是用ArrayList;如果要经常从表中间插入或者删除元素,则应该使用LinkedList。Map是一种将对象(而非数字)与对象相关联的设计。HashMap设计用来快速访问;而TreeMap保持“键”始终处于排序状态,所以没有HashMap快。LinkedHashMap保持元素插入的顺序,但是也通过散列提供了快速访问能力。Set不接受重复元素。HashSet提供了最快的查询速度,而TreeSet保持元素处于排序状态。LinkedHashSet以插入顺序保存元素。新程序中不应该使用过时的Vector、Hashtable和Stack。

package com.exam.cn;

import net.mindview.util.ContainerMethodDifferences;

public class ContainerMethods {
public static void main(String[] args) {
	ContainerMethodDifferences.main(args);
}
}
输出结果:
Collection: [add, addAll, clear, contains, containsAll, equals, forEach, hashCode, isEmpty, iterator, parallelStream, remove, removeAll, removeIf, retainAll, size, spliterator, stream, toArray]
Interfaces in Collection: [Iterable]
Set extends Collection, adds: []
Interfaces in Set: [Collection]
HashSet extends Set, adds: []
Interfaces in HashSet: [Set, Cloneable, Serializable]
LinkedHashSet extends HashSet, adds: []
Interfaces in LinkedHashSet: [Set, Cloneable, Serializable]
TreeSet extends Set, adds: [headSet, descendingIterator, descendingSet, pollLast, subSet, floor, tailSet, ceiling, last, lower, comparator, pollFirst, first, higher]
Interfaces in TreeSet: [NavigableSet, Cloneable, Serializable]
List extends Collection, adds: [replaceAll, get, indexOf, subList, set, sort, lastIndexOf, listIterator]
Interfaces in List: [Collection]
ArrayList extends List, adds: [trimToSize, ensureCapacity]
Interfaces in ArrayList: [List, RandomAccess, Cloneable, Serializable]
LinkedList extends List, adds: [offerFirst, poll, getLast, offer, getFirst, removeFirst, element, removeLastOccurrence, peekFirst, peekLast, push, pollFirst, removeFirstOccurrence, descendingIterator, pollLast, removeLast, pop, addLast, peek, offerLast, addFirst]
Interfaces in LinkedList: [List, Deque, Cloneable, Serializable]
Queue extends Collection, adds: [poll, peek, offer, element]
Interfaces in Queue: [Collection]
PriorityQueue extends Queue, adds: [comparator]
Interfaces in PriorityQueue: [Serializable]
Map: [clear, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, equals, forEach, get, getOrDefault, hashCode, isEmpty, keySet, merge, put, putAll, putIfAbsent, remove, replace, replaceAll, size, values]
HashMap extends Map, adds: []
Interfaces in HashMap: [Map, Cloneable, Serializable]
LinkedHashMap extends HashMap, adds: []
Interfaces in LinkedHashMap: [Map]
SortedMap extends Map, adds: [lastKey, subMap, comparator, firstKey, headMap, tailMap]
Interfaces in SortedMap: [Map]
TreeMap extends Map, adds: [descendingKeySet, navigableKeySet, higherEntry, higherKey, floorKey, subMap, ceilingKey, pollLastEntry, firstKey, lowerKey, headMap, tailMap, lowerEntry, ceilingEntry, descendingMap, pollFirstEntry, lastKey, firstEntry, floorEntry, comparator, lastEntry]
Interfaces in TreeMap: [NavigableMap, Cloneable, Serializable]

jar包下载路径:net.mindview.util包: https://pan.baidu.com/s/1SAb4Z1wD21xJ-iCwGvd0aw 提取码: 5inz

上面的实例展示了各种不同的类在方法上的差异。实际的代码来自第15章,我在这里只是调用它以产生输出。程序的输出也展示了在每个类或接口中所实现的接口。在Queue接口中的方法都是独立的,在创建具有Queue功能的实现时,不需要使用Collection方法。

Java的基本理念是“结构不佳的代码不能运行”。

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言