李林超博客
首页
归档
留言
友链
动态
关于
归档
留言
友链
动态
关于
首页
Java
正文
持有对象--总结
Leefs
2019-12-14 PM
1374℃
0条
# 持有对象--总结 ### 前言 经过不断的努力,持有对象已经接近尾声,本小节将对之前的持有对象做一个系统性总结,另为下周小编要开始下一个篇章:**字符串** ### 一、知识梳理 **java提供了大量的持有对象的方式:** > 1. 1.数组将数字和对象联系起来,它保存类型明确的对象,查询对象时,不需要对结果做类型转换,它可以是多维的,可以保存基本数据类型的数据,但是,数组一旦生成,其容量就不能改变 > 2. 2.Collection保存单一的元素,而Map保存相关联的键值对.有了java泛型,你就可以指定容器中存放的对象类型,因此你就不会将错误类型的对象放置到容器中,并且从容器中取出元素时,不必进行类型转换,各种Collection和各种Map都可以在你向其中添加更多的元素时,自动调整其尺寸.容器不能持有基本数据类型,但是自动包装机制会仔细地执行基本数据类型到容器中所持有的包装器类型之间的双向切换. > 3. 3.像数组一样,List也建立数字索引与对象的关联,因此,数组和List都是排好序的容器,List能够自动扩充容量 > 4. 4.如果要进行大量的随机访问就用ArrayList,如果经常从表中插入或删除元素,则应该使用LinkedList > 5. 5.各种Queue以及栈的行为,由LinkedLIst提供支持 > 6. 6.Map是一种将对象(而非数字)与对象相关联的设计,HashMap设计用来快速访问,而TreeMap保持"键"始终处于排序状态,所以没有HashMap快,LinkedHashMap保持元素插入的顺序,但是也通过散列提供了快速访问能力 > 7. 7.Set不接受重复元素,HashSet提供最快的查询速度,而TreeSet保持元素处于排序状态,LinkedHashSet以插入顺序保存元素 > 8. 8.新程序中不应该用过时的Vector,Hashtable和Stack ### 二、Java容器简图 ![Java容器简图01.png][1] Java容器简图 上面的Java容器的简图,包含了一般情况下会碰到的接口和类,可以看到,其实只有四种容器:**Map, List, Set和Queue**,它们各有两到三个实现版本(Queue的java.util.concurrent实现没有包含在上面这两个图中). **常用**的容器用**黑色粗线框**表示, **点线框表示接口**, **实线框**表示**普通的(具体的)类**,带有空心箭头的点线表示一个特定的类实现了一个接口,**实心箭头**表示**某个类可以生成箭头所指向类的对象** 例如,任意的Collection可以生成Iterator,而list可以生成ListIterator(也可以生成普通的Iterator,因为List继承自Collection) ### 三、各种不同的类在方法上的差异 > 示例 ```java package ThinkInJava.container; import net.mindview.util.*; public class ContainerMethods { public static void main(String[] args) { ContainerMethodDifferences.main(args); } } ``` > 运行结果 ```java 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] Process finished with exit code 0 ``` *注意:上方代码需要引入`net.mindview.util.jar`文件,如果需要的在下方给小编留言。* 上方的示例展示了各种不同的类在方法上的差异,可以看到**除了TreeSet之外的所有Set都拥有与Collection完全一样的接口**,List和Colletion存在着明显的不同,尽管所要求的方法都在Colletion中,另一方面,在Queue接口中的方法都是独立的,在创建具有Queue功能的实现时,不需要使用Colletion方法,最后Map和Colletion之间唯一的重叠就是Map可以使用entrySet()和values()方法来产生Colletion 注意,标记接口java.uitl.RandomAccess附着在ArrayList上,而没有附着在LinkedList上,这为想要根据所使用的特定的List而动态修改其行为的算法提供了信息 [1]: https://lilinchao.com/usr/uploads/2019/12/41722072.png
标签:
Java
,
Java编程思想
,
JavaSE
,
持有对象
非特殊说明,本博所有文章均为博主原创。
如若转载,请注明出处:
https://www.lilinchao.com/archives/283.html
上一篇
持有对象--适配器方法惯用法
下一篇
字符串--String不可变
取消回复
评论啦~
提交评论
栏目分类
随笔
2
Java
326
大数据
229
工具
31
其它
25
GO
47
标签云
HDFS
前端
VUE
随笔
Zookeeper
Spark Core
JavaWeb
设计模式
哈希表
ajax
Redis
Typora
Jenkins
微服务
Golang基础
BurpSuite
NIO
SpringBoot
正则表达式
递归
Scala
MyBatisX
排序
序列化和反序列化
SQL练习题
Spark
Flink
Hive
JavaScript
工具
友情链接
申请
范明明
庄严博客
Mx
陶小桃Blog
虫洞