第十七章 集合Collection 1
来源:互联网 发布:js 当前页面url 编辑:程序博客网 时间:2024/06/07 10:59
17.1 概述
存储对象的容器。
集合的长度是可变的(数组不可变)。
集合中不可以存储基本数据类型,只能存对象。(数组可以)。
17.2 体系&共性功能
Collection是集合的根接口,包含了集合的共性功能。
添加 add(); addAll(Collection);
删除 remove(); removeAll(Collection); clear();
查询 size(); iterator();
判断 contains(); containsAll(); isEmpty();
其他 toArray(); equals(); hasCode(); retainAll();
17.3 迭代器使用
while(iterator.hasNext()){iterator.next();}//orfor (iterator; iterator.hasNext; ){iterator.next();}
17.4 迭代器的原理
通过内部类实现,iterator是集合的内部类。
17.5 List和Set的特点
List有序(存储顺序);Set无序。
List有索引,科技精确控制;Set没有索引。
List可存储重复的元素;Set中元素不能重复。(*)
因为具有索引,List较于父类Collection具有更多操作索引的方法。
17.6 ListIterator 接口
迭代器和集合不能同时操作集合内元素。
当需要在迭代器中操作元素(添加、删除等),使用Iterator子接口ListIterator(仅限List集合)
ListIterator it = list.ListIterator();while(it.hasNext()){it.add();//此时使用迭代器操作,而不是集合}it.hasPrevious();it.previous();//可用于逆向遍历
List的常用子类
Vector:数据结构为数组,同步。
ArrayList:数据结构为数组,不同步。
LinkedList:数据结构为链表,不同步。
17.7 Vector(逐渐被ArrayList取代)
addElement(); -->add();elementAt();-->ger();hasMoreElements()();-->hasNext();capacity();
17.8 LinkedList
add(index,element);addFirst();addLast();pollFirst();(获取但不移除,列表为空时返回null)pollLast();removeFirst();(获取且移除,列表为空时抛出NoSuchElementException)removelast();getFirst();(列表为空时抛出NoSuchElementException)getLast()peekFirst;(列表为空时返回null)peekLast;
17.9 LinkedList练习
使用上述几个方法,可以模拟实现堆栈、队列数据结构。
17.10 HashSet
Set不重复,无序
内部数据结构是哈希表、
HashSet不同步
17.11 哈希表
按元素的哈希值进行存储
Object方法 hashCode();
哈希表判断元素是否相同:
1)判断哈希值是否相同(hashCode())
2)若哈希值相同再判断内容是否相同(equals())
哈希冲突(哈希值相同但内容不同,很少发生)
哈希表存储自定义对象时,需要重写equals()方法,或者也可重写hashCode()方法
LinkedHashSet 双重链表+哈希表,唯一且有序
17.12 TreeSet
可以对集合内元素进行指定顺序的排序
存储自定义对象时,TreeSet存储的元素应该实现Comparable接口
重写compareTo()方法(return正、负、0)
除了使元素可比较,也可以使集合自身实现Comparator接口,重写compare()方法
0 0
- 第十七章 集合Collection 1
- 集合1 collection
- Collection集合1
- javaAPI(Collection单列集合1)
- 集合collection 2016 1 22
- 集合之Collection(1)
- 集合1——Collection集合
- 集合Collection
- 集合collection
- Collection集合
- Collection集合
- Collection集合
- 集合 Collection
- Collection集合
- Collection集合
- 集合Collection
- 集合collection
- Collection集合
- ProgressBar的圆形进度和水平进度的使用
- Codeforces 768A Oath of the Night's Watch
- D3D11地形渲染教程十九之Foliage(植被的渲染)
- HTML标签----图文详解
- 在github上fork的相关操作
- 第十七章 集合Collection 1
- Whoere即时聊天App(三)客户端实现+服务部署
- 设计模式-装饰者设计模式
- linux驱动的异步同步通知机制
- TreeSet部分源码解析
- 【蓝桥杯】算法提高 7-2求arccos值
- 面向对象的原则
- ACM 海贼王之伟大航路(深搜剪枝)
- Android之DOM解析XML