集合的概念
集合的框架结构
Java的集合框架 从整体上可以分为两种:
1、Collection接口:该接口下的所有子孙均存储的是单一对象
2、Map接口 :该接口下的所有子孙均存储的是key-value(键值对)形式的数据
集合与数组的对比
Collection接口
Conllection接口是一中允许重复的对象,蒂尼了存取对象的方法。
比较常用的两个子接口:
- List接口:存放元素有序且有重复的集合接口。
- Set接口: 存放元素无序且不允许有重复的集合接口,所有的重复内容是靠hashCode()和euqals()两个方法区分
集合对象的创建
接口不能被实例化,所以创建集合对象,只能创建其接口的实现类。如:
Collection c1 = new ArrayList(); Collection c2 = new LinkedList(); Collection c3 = new HashSet();
Collection中常用的方法:
| 方法名 | 描述 |
|---|---|
| int size(); | 返回此collection中的元素个数。 |
| boolean isEmpty(); | 判断此collection中是否包含元素。 |
| boolean contains(Object obj); | 判断此collection是否包含指定(equals为true)的元素。 |
| boolean containsAll(Collection c); | 判断此collection是否包含指定 Collection c 中的所有元素。比较方式通过 equals比较 |
| boolean add(Object element); | 向此collection中添加元素。 |
| boolean addAll(Collection c); | 将指定collection中的所有元素添加到此collection中 |
| boolean remove(Object element); | 从此collection中移除指定的元素,如果有多个对象重复,则只会删除第一个。 |
| boolean removeAll(Collection c); | 移除此collection中那些也包含在指定 Collection c 中的所有元素。比较方式通过 equals比较 |
| void clear(); | 移除些collection中所有的元素。 |
| boolean retainAll(Collection c); | 仅保留此collection中那些也包含在指定 Collection c 的元素。比较方式通过 equals比较 |
| Iterator iterator(); | 返回在此collection的元素上进行迭代的迭代器。 |
| Object[] toArray(); | 把此collection转成数组。 |
Iterator 迭代器
由于集合中存有很多元素,很多时候需要遍历集合中的所有元素,java专门为集合提供了遍历集合的API:迭代器接口Iterator对象称作迭代器,用以方便的实现对集合内元素的遍历操作。
迭代器的工作原理
迭代器的使用
调用集合对象的iterator()方法,可以获得一个与该集合对象关联的迭代器对象。
List<String> list = new ArrayList<>(); list.add("java基础"); list.add("java高编"); //获得Iterator对象 Iterator<String> iterator = list.iterator();
Iterator定义如下三个方法:
public class Test {
public static void main(String[] args) { List
list =
new ArrayList<>();
list.add(
"java基础");
list.add(
"java高编");
list.add(
"Android基础");
list.add(
"Android进阶"); ListIterator
it =
list.listIterator();
while (it.hasNext()) { String str = it.next();
if (str.equals(
"java高编")) { it.add(
"MySql"); } System.out.println(str); } System.out.println(
list); } }

List接口的特点
public E get(int index);返回列表中的指定位置的的元素 public void add(int index, E element); 在列表的指定位置插入指定元素。将当前处于该位置的元素(如果有的话)和所有后续元素向右移动。 public E set(int index, E element) ; 用指定元素替换列表中指定位置的元素 public E remove(int index) 移除列表中指定位置的元素 public ListIterator listIterator() 返回此列表元素的列表迭代器(List专有) 注意:list集合中的元素的索引与数组中元素的索引一样,均是从0开始。
ArrayList实现类,实现原理
LinkedList实现类,实现原理
public void addFirst(E e);向集合头部添加数据 public void addLast(E e);向集合尾部添加数据 public E removeFirst();删除链表第一个数据,并返回被删除的数据 public E removeLast();删除链表的最后一个数据,并返回被删除的数据
对比ArrayList 和LinkedList的效率:
public class Test02 { public static void main(String[] args) { long a =System.currentTimeMillis(); LinkedList
linkedList =
new LinkedList<>();
for (
int i =
0 ; i <
99999 ; i++){ linkedList.add(
"张三:" + i); }
long b = System.currentTimeMillis(); System.
out.println(
"LinkedList 添加时间:" + (b - a)); ArrayList
arrayList =
new ArrayList
();
for (
int i =
0; i <
99999; i++) { arrayList.add(
"张三" + i); }
long c = System.currentTimeMillis(); System.
out.println(
"ArrayList 添加时间:" + (c - b));
for (
int i =
0; i < arrayList.size(); i++) { arrayList.
get(i); }
long d= System.currentTimeMillis(); System.
out.println(
"ArrayList 遍历时间:" + (d - c));
for (
int i =
0; i < linkedList.size(); i++) { linkedList.
get(i); }
long e = System.currentTimeMillis(); System.
out.println(
"linkedList 遍历时间:" + (e - d));
while (linkedList.size() >
0) { linkedList.removeFirst(); }
long f = System.currentTimeMillis(); System.
out.println(
"linkedList 删除时间:" + (f - e));
while (arrayList.size() >
0){ arrayList.remove(
0); }
long g = System.currentTimeMillis(); System.
out.println(
"arrayList 删除时间:" + (g - f)); } }
当然,List接口还有几种,这里就不一一介绍了,因为并不常用,作为了解
还有:Vector、Stack 有兴趣可以去了解一下
Map接口概述
Map接口的常用方法:这个就比较重要了
| 方法名 | 描述 |
|---|---|
| Object put(Object key, Object value); | 将指定的“键-值”对存入Map中,返回已经存在的key的Values。如果不存在则返回null。 |
| Object get(Object key); | 返回指定键所映射的值 |
| Object remove(Object key); | 根据指定的键把此“键-值”对从Map中移除。 |
| boolean containsKey(Object key); | 判断此Map是否包含指定键的“键-值”对。 |
| boolean containsValue(Object value); | 判断此Map是否包含指定值的“键-值”对。 |
| boolean isEmpty(); | 判断此Map中是否有元素。 |
| int size(); | 获得些Map中“键-值”对的数量。 |
| void clear(); | 清空Map中的所有“键-值”对。 |
| Set keySet(); | 返回此Map中包含的键的Set集。 |
| Collection values(); | 返回此Map中包含的值的Collection集。 |
Map接口的常用实现类
HashMap
Map.Entry
Map<String, Integer> map = new HashMap<String,Integer>(); Set<Entry<String, Integer>> entrySet = map.entrySet(); Iterator<Entry<String, Integer>> iterator = entrySet.iterator(); Entry<String, Integer> entry = iterator.next(); Integer value = entry.getValue();//获取值 String key = entry.getKey();//获取键

Collections:工具类
- 排序操作(主要针对List接口相关)
- reverse(List list):反转指定List集合中元素的顺序
- shuffle(List list):对List中的元素进行随机排序(洗牌)
- sort(List list):对List里的元素根据自然升序排序
- swap(List list, int i, int j):将指定List集合中i处元素和j出元素进行交换
- sort(List list, Comparator c):自定义比较器进行排序
- rotate(List list, int distance):将所有元素向右移位指定长度,如果distance等于size那么结果不变
- 查找和替换(主要针对Collection接口相关)
- binarySearch(List list, Object key):使用二分搜索法,以获得指定对象在List中的索引(下标)。
- max(Collection coll):返回最大元素
- max(Collection coll, Comparator comp):根据自定义比较器,返回最大元素
- min(Collection coll):返回最小元素
- min(Collection coll, Comparator comp):根据自定义比较器,返回最小元素
- fill(List list, Object obj):使用指定对象填充
- frequency(Collection c, Object o):返回指定集合中指定对象出现的次数
- replaceAll(List list, Object old, Object new):替换
- 其他用法比较不常用,详情请参照 JAVA API 说明。
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/222076.html原文链接:https://javaforall.net
