Javaheima07(Javaheima07)-java
Javaheima07(Javaheima07)
Java
集合
Collection集合
集合的代表:Collection 接口
体系
- Collection
List
ArrayList
LinkedListSet
HashSet
LinkedHashSet
TreeSet
- List
ArrayList
LinkedList - ArrayList
- LinkedList
- Set
HashSet
LinkedHashSet
TreeSet
- HashSet
LinkedHashSet
- LinkedHashSet
- TreeSet
最常用
特点
- List系列集合 : 添加的元素是有序的,可重复,有索引
ArrayList,LinkedList:有序,可重复,可索引
- ArrayList,LinkedList:有序,可重复,可索引
- Set系列集合:添加的元素是无序,不重复,无索引.
HashSet:无序,不重复,无索引
LinkedHashSet: 有序,不重复,无索引
TreeSet:按照大小默认升序排序,不重复,无索引
- HashSet:无序,不重复,无索引
LinkedHashSet: 有序,不重复,无索引
- LinkedHashSet: 有序,不重复,无索引
- TreeSet:按照大小默认升序排序,不重复,无索引
集合对泛型的支持
- 集合都是支持泛型的,可以在编译阶段约束集合只能操作某种数据类型
-
Collection
lists = new ArrayList ();
Collectionlists = new ArrayList<>();
//JDK 1.7开始毫秒的泛型类型申明可以省略不写
注意:集合和泛型都只能支持引用数据类型,不支持基本数据类型,所以集合中存储的元素都认为是对象
错误实例
Collection<int> lists = new ArrayList<>();
//不能这样使用
如果集合中要存储基本类型数据要是有基本数据类型包装类
//存储基本类型使用包装类
Collection<Integer> lists = new ArrayList<>();
//最后的<>可以省略,最好加上
Collection<Double> lists = new ArrayList<>();
Collection集合常用API
- Collection是单列集合的祖宗接口,它的功能是全部单列集合都可以继承使用的
-
方法名称
说明public boolean add(E e)
把给定的对象添加到当前集合中public void clear()
清空集合中所有的元素public boolean remove(E e)
把给定给的对象在当前集合中删除public boolean contains(Object boj)
判断当前集合中是否包含给定的对象public boolean IsEmpty()
判断当前集合是否为空public int size()
返回集合元素的个数public Object[] toArray()
把集合中的元素,存储到数组中
方法名称 | 说明 |
---|---|
public boolean add(E e) | 把给定的对象添加到当前集合中 |
public void clear() | 清空集合中所有的元素 |
public boolean remove(E e) | 把给定给的对象在当前集合中删除 |
public boolean contains(Object boj) | 判断当前集合中是否包含给定的对象 |
public boolean IsEmpty() | 判断当前集合是否为空 |
public int size() | 返回集合元素的个数 |
public Object[] toArray() | 把集合中的元素,存储到数组中 |
遍历方式
方式一 : 迭代器
方式二 : foreach/增强for循环
方式三 : Lambda表达式
迭代器遍历概述
- 遍历就是一个一个包容器中的元素访问一遍
- 迭代器在Java中的代表是Iterator,迭代器是集合的专用遍历方式
Collection集合获取迭代器
方法名称 | 说明 |
---|---|
Iterator |
返回集合中的迭代器对象,该迭代器对象默认指向当前集合的0索引 |
Iterator中的常用方法
方法名称 | 说明 |
---|---|
boolean hasNext() | 访问当前位置是否有元素存在,存在返回true,不存在返回false |
E next() | 获取当前位置的元素,并同时将迭代器对象一项下一个位置,注意防止取出越界 |
迭代器越界出现的问题:NoSuchElementException异常
增强for循环
- 增强for循环:既可以遍历集合也可以遍历数组
- 它是JDK5之后出现的,其内部原理是一个Iterator迭代器,遍历集合相当于是迭代器的简化写法
- 实现Iterable接口的类才可以使用迭代器和增强for,Collection接口已经实现Iterable接口
格式
for(元素数据类型 变量名:数组或者Collection集合){
//在此处使用变量即可,该变量就是元素
}
//实例
Collection<String> list = new ArrayList<>();
...
for(String ele:list){
System.out.println(ele);
//改变ele的值不会对集合/数组内容进行改变
}
Lambda表达式
- 得益于JDK 8 开始的新技术Lanbda表达式,提供了一种更简单,更直接的遍历集合的方式.
Collection结合Lambda遍历的API
方法名称 | 说明 |
---|---|
default void forEach(Consumer<?super T> action): | 结合lambda遍历集合 |
package com.yu.SingleInstance;
import java.util.ArrayList;
import java.util.Collection;
import java.util.function.Consumer;
public class CollectionTest {
public static void main(String[] args) {
Collection<String> lists = new ArrayList<>();
lists.add("1");
lists.add("2");
lists.add("3");
lists.add("4");
lists.add("5");
System.out.println(lists);
System.out.println("------------------------------------");
lists.forEach(new Consumer<String>() {
@Override
public void accept(String s) {
System.out.println(s);
}
});
System.out.println("------------------------------------");
lists.forEach((String s) -> {
System.out.println(s);
}
);
System.out.println("------------------------------------");
lists.forEach(s -> {
System.out.println(s);
}
);
System.out.println("------------------------------------");
lists.forEach(s -> System.out.println(s));
System.out.println("------------------------------------");
lists.forEach(System.out::println);
}
}
答案相同但是书写格式越来越简单
[1, 2, 3, 4, 5]
------------------------------------
1
2
3
4
5
------------------------------------
1
2
3
4
5
------------------------------------
1
2
3
4
5
------------------------------------
1
2
3
4
5
------------------------------------
1
2
3
4
5
又是懒惰的一天
2022.05.16
Java
aggregate
Collection集合
Representative of collection: collection interface
system
- Collection
List
ArrayList
LinkedListSet
HashSet
LinkedHashSet
TreeSet
- List
ArrayList
LinkedList - ArrayList
- LinkedList
- Set
HashSet
LinkedHashSet
TreeSet
- HashSet
LinkedHashSet
- LinkedHashSet
- TreeSet
Most commonly used
characteristic
- List Series Collection: the added elements are ordered, repeatable and indexed
ArrayList, LinkedList: ordered, repeatable and indexable - ArrayList,LinkedList:有序,可重复,可索引
- Set series collection: the added elements are unordered, non repeated and non indexed
HashSet: unordered, non duplicate, no index
Linkedhashset: ordered, non duplicate, no index
TreeSet: sort by size in ascending order by default. No duplicate and no index - HashSet: unordered, non duplicate, no index
Linkedhashset: ordered, non duplicate, no index - LinkedHashSet: 有序,不重复,无索引
- TreeSet: sort by size in ascending order by default. No duplicate and no index
Collection support for generics
- A collection of data types can only be supported in the compilation stage
-
Collection
lists = new ArrayList ();
Collectionlists = new ArrayList<>();
//JDK 1.7开始毫秒的泛型类型申明可以省略不写
< strong > note < / strong >: both sets and generic types can only support reference data types, not basic data types, so the elements stored in the set are considered objects
< strong > error instance < / strong >
Collection<int> lists = new ArrayList<>();
//不能这样使用
< strong > if basic type data is to be stored in the collection, if there is a basic data type packing class < / strong >
//存储基本类型使用包装类
Collection<Integer> lists = new ArrayList<>();
//最后的<>可以省略,最好加上
Collection<Double> lists = new ArrayList<>();
Collection集合常用API
- Collection is the ancestor interface of single column collection. Its function is that all single column collections can be inherited and used
-
方法名称
说明public boolean add(E e)
把给定的对象添加到当前集合中public void clear()
清空集合中所有的元素public boolean remove(E e)
把给定给的对象在当前集合中删除public boolean contains(Object boj)
判断当前集合中是否包含给定的对象public boolean IsEmpty()
判断当前集合是否为空public int size()
返回集合元素的个数public Object[] toArray()
把集合中的元素,存储到数组中
方法名称 | 说明 |
---|---|
public boolean add(E e) | 把给定的对象添加到当前集合中 |
public void clear() | 清空集合中所有的元素 |
public boolean remove(E e) | 把给定给的对象在当前集合中删除 |
public boolean contains(Object boj) | 判断当前集合中是否包含给定的对象 |
public boolean IsEmpty() | 判断当前集合是否为空 |
public int size() | 返回集合元素的个数 |
public Object[] toArray() | 把集合中的元素,存储到数组中 |
Traversal mode
Method 1: iterator
Mode 2: foreach / enhanced for loop
Method 3: lambda expression
< strong > overview of iterator traversal < / strong >
- Traversal is to access the elements in the package container one by one
- Iterator is the representative of iterator in Java, and iterator is the special traversal mode of collection
< strong > collection get iterator < / strong >
方法名称 | 说明 |
---|---|
Iterator |
返回集合中的迭代器对象,该迭代器对象默认指向当前集合的0索引 |
< strong > common methods in iterator < / strong >
方法名称 | 说明 |
---|---|
boolean hasNext() | 访问当前位置是否有元素存在,存在返回true,不存在返回false |
E next() | 获取当前位置的元素,并同时将迭代器对象一项下一个位置,注意防止取出越界 |
< strong > problems with iterator out of bounds: NoSuchElementException exception < / strong >
< strong > enhanced for loop < / strong >
- Enhanced for loop: it can traverse both sets and arrays
- It appeared after jdk5. Its internal principle is an iterator iterator. Traversing the set is equivalent to the simplified writing of the iterator
- 实现Iterable接口的类才可以使用迭代器和增强for,Collection接口已经实现Iterable接口
格式
for(元素数据类型 变量名:数组或者Collection集合){
//在此处使用变量即可,该变量就是元素
}
//实例
Collection<String> list = new ArrayList<>();
...
for(String ele:list){
System.out.println(ele);
//改变ele的值不会对集合/数组内容进行改变
}
Lambda表达式
- Thanks to the new technology of JDK 8, lanbda expression provides a simpler and more direct way to traverse a collection
Collection结合Lambda遍历的API
方法名称 | 说明 |
---|---|
default void forEach(Consumer<?super T> action): | 结合lambda遍历集合 |
package com.yu.SingleInstance;
import java.util.ArrayList;
import java.util.Collection;
import java.util.function.Consumer;
public class CollectionTest {
public static void main(String[] args) {
Collection<String> lists = new ArrayList<>();
lists.add("1");
lists.add("2");
lists.add("3");
lists.add("4");
lists.add("5");
System.out.println(lists);
System.out.println("------------------------------------");
lists.forEach(new Consumer<String>() {
@Override
public void accept(String s) {
System.out.println(s);
}
});
System.out.println("------------------------------------");
lists.forEach((String s) -> {
System.out.println(s);
}
);
System.out.println("------------------------------------");
lists.forEach(s -> {
System.out.println(s);
}
);
System.out.println("------------------------------------");
lists.forEach(s -> System.out.println(s));
System.out.println("------------------------------------");
lists.forEach(System.out::println);
}
}
The answers are the same, but the writing format is becoming simpler and simpler
[1, 2, 3, 4, 5]
------------------------------------
1
2
3
4
5
------------------------------------
1
2
3
4
5
------------------------------------
1
2
3
4
5
------------------------------------
1
2
3
4
5
------------------------------------
1
2
3
4
5
Another lazy day
2022.05.16