集合体系结构C总览
Collection 集合学习
代码 实现 创建和使用
public class CollectionDemo {
public static void main(String[] args) {
Collection<String> strings = new ArrayList<>();
//添加元素
strings.add("hello");
strings.add("world");
System.out.println(strings);//[hello, world]
}
}
Collection的常用方法
代码实现
public class CollectionDemo02 {
public static void main(String[] args) {
Collection<String> strings = new ArrayList<>();
//添加方法 返回的是boolean类型 添加成功或者失败
strings.add("hello");
strings.add("world");
strings.add("lala");
System.out.println("集合中全部的数据"+strings);
//从集合中移除方法
strings.remove("lala");
System.out.println("集合中移除后的数据"+strings);
//判断指定元素是否存在
strings.contains("lala");//返回的是boolean类型
System.out.println("集合中判断后的数据"+strings);
//判断集合是否为空
strings.isEmpty();//返回的是boolean类型
System.out.println("集合中判断后的数据"+strings);
//集合的长度
System.out.println("集合的长度"+strings.size());
/* 结果
集合中全部的数据[hello, world, lala]
集合中移除后的数据[hello, world]
集合中判断后的数据[hello, world]
集合中判断后的数据[hello, world]
集合的长度2
*/
}
}
Collection集合的遍历
代码
/*
集合的遍历
*/
public class CollectionTraversal {
public static void main(String[] args) {
Collection<String> ss = new ArrayList<>();
//添加方法 返回的是boolean类型 添加成功或者失败
ss.add("hello");
ss.add("world");
ss.add("java");
//使用迭代器遍历集合
/*
hello
world
java
*/
Iterator<String> iterator = ss.iterator();
System.out.println(iterator.next());
System.out.println(iterator.next());
System.out.println(iterator.next());
/*
System.out.println(iterator.next());//不小心多点了一个 出现异常 NoSuchElementException 没有元素了
*/
//使用if判断
// if (iterator.hasNext()){//iterator.hasNext()返回是一个booolean类型的 所以利用这个来循环
// System.out.println(iterator.next());
// }
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
案例 存储学生类并遍历出来
首先创建学生类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private String name;
private int age;
}
创建集合类并添加学生类并测试
public class ConllectionDemo {
public static void main(String[] args) {
Collection<Student> studentArrayList = new ArrayList<>();//创建集合
Student student1 = new Student("周冬雨",18);//创建学生类对象
Student student2 = new Student("黄四郎",19);//创建学生类对象
//添加到集合中
studentArrayList.add(student1);
studentArrayList.add(student2);
//遍历输出
Iterator<Student> iterator = studentArrayList.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
/*结果
Student(name=周冬雨, age=18)
Student(name=黄四郎, age=19)
*/
}
list集合学习
代码
/*
List集合的特点
有序
可重复
*/
public class ListDemo {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
//添加
list.add("hello");
list.add("world");
list.add("java");
list.add("java");
list.add("java");
// System.out.println(list);
/*
[hello, world, java, java, java]
*/
//集合遍历 既然集成于Collection集合 就能够用迭代器
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
/*
hello
world
java
java
java
*/
}
}
}
list集合的特有方法
代码
/*
List集合的特点
有序 通过有序 就可以实现很多操作
可重复
*/
public class ListDemo01 {
public static void main(String[] args) {
List<String> list1 = new ArrayList<>();
//添加
list1.add("hello");
list1.add("world");
list1.add("java");
list1.add("java");
list1.add("java");
//返回指定索引处的元素
String s = list1.get(0);
System.out.println(s);//hello
//修改指定索引的元素 并且 返回修改的元素
String lala = list1.set(3, "lala");
//删除指定索引的元素 冰返回删除的元素
String remove = list1.remove(4);
System.out.println("删除的元素"+remove);
//在指定位置插入指定的元素
list1.add(0,"ouou");
//遍历集合 这次不用迭代器
for (String s1 : list1) {
System.out.println(s1);
}
}
}
list集合遍历
方式一 迭代器
public class ListDemo02 {
public static void main(String[] args) {
List<String> list1 = new ArrayList<>();
//添加
list1.add("hello");
list1.add("world");
list1.add("java");
list1.add("java");
list1.add("java");
//遍历方式一迭代器
Iterator<String> iterator = list1.iterator();
while (iterator.hasNext()){
String next = iterator.next();
System.out.println(next);
/*
hello
world
java
java
java
*/
}
}
}
方式二 for循环
public class ListDemo02 {
public static void main(String[] args) {
List<String> list1 = new ArrayList<>();
//添加
list1.add("hello");
list1.add("world");
list1.add("java");
list1.add("java");
list1.add("java");
for (int i = 0; i < list1.size(); i++) {
System.out.println(list1.get(i));
/*
hello
world
java
java
java
*/
}
}
}
方式三 增强for循环
public class ListDemo02 {
public static void main(String[] args) {
List<String> list1 = new ArrayList<>();
//添加
list1.add("hello");
list1.add("world");
list1.add("java");
list1.add("java");
list1.add("java");
for (String s : list1) {
System.out.println(s);
/*
hello
world
java
java
java
*/
}
}
}
ArrayList和LikedList
ArrayList底层实现是数组 特点查询快 增删慢
LikedList底层实现是链表 特点是查询慢 增删快
创建学生对象存储在两个集合中 并遍历
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private String name;
private Integer age;
}
ArrayList实现
/*
Student(name=ll, age=15)
Student(name=lppl, age=15)
Student(name=lllkk, age=15)
*/
public class ArraysListAndStudent {
public static void main(String[] args) {
Student student = new Student("ll",15);
Student student1 = new Student("lppl",15);
Student student2= new Student("lllkk",15);
ArrayList<Student> students = new ArrayList<>();
students.add(student);
students.add(student1);
students.add(student2);
for (Student student3 : students) {
System.out.println(student3);
}
}
}
LikedList实现
/*结果
Student(name=ll, age=15)
Student(name=lppl, age=15)
Student(name=lllkk, age=15)
*/
public class LinkedListStudent {
public static void main(String[] args) {
Student s1 = new Student("ll",15);
Student s2 = new Student("lppl",15);
Student s3= new Student("lllkk",15);
LinkedList<Student> studentLinkedList = new LinkedList<>();
studentLinkedList.add(s1);
studentLinkedList.add(s2);
studentLinkedList.add(s3);
for (Student student : studentLinkedList) {
System.out.println(student);
}
}
}
LikedList特有的功能
Set集合 学习
Set集合的特点 不包含重复的元素 没有带索引的方法 所以不能用普通的for循环 HashSet对迭代的顺序不做任何保证
/*
Set集合的特点
不包含重复的元素
没有带索引的方法 所以不能用普通的for循环
HashSet对迭代的顺序不做任何保证
*/
public class SetDemo01 {
/*
结果 : 无序的 切 不重复
world
java
hello
*/
public static void main(String[] args) {
Set<String> hashSet = new HashSet<>();
hashSet.add("hello");
hashSet.add("world");
hashSet.add("java");//测试不包含重复元素
hashSet.add("java");
//增强for迭代
for (String s : hashSet) {
System.out.println(s);
}
}
}
HashSet
特点:
-
底层结构是hash表 不包含重复的元素 没有带索引的方法 所以不能用普通的for循环 HashSet对迭代的顺序不做任何保证
/*
Set集合的特点
底层结构是hash表
不包含重复的元素
没有带索引的方法 所以不能用普通的for循环
HashSet对迭代的顺序不做任何保证
*/
public class HashSetDemo {
/*
结果:
hellowordjava
helloword
hello
*/
public static void main(String[] args) {
HashSet<String> strings = new HashSet<>();
strings.add("hello");
strings.add("helloword");
strings.add("hellowordjava");
for (String string : strings) {
System.out.println(string);
}
}
}
这里要重写两个方法 来保证对象的唯一性
LinkedHashSet
特点
/* LinkedHashSet 既有linked属性 链表有序 也有set属性 唯一 */
TreeSet集合
特点
代码
/*
TreeSet集合特点
由于是set集合 所以是元素不重复的
元素是有序的 默认为自然排序
没有索引 所以不能用了普通for循环
*/
public class TreeSetDemo {
/*结果
abc
hello
java
world
*/
public static void main(String[] args) {
TreeSet<String> treeSet = new TreeSet<>();
treeSet.add("hello");
treeSet.add("world");
treeSet.add("java");
treeSet.add("abc");
for (String s : treeSet) {
System.out.println(s);
}
}
}
案例
代码
创建学生类 并实现Comparable接口
public class Student implements Comparable<Student>{
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Student() {
}
public Student(String name, Integer age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public int compareTo(Student s) {
// return 0;//添加一个元素
// return 1;//按照升序存储元素
// return 0;//按照降序存储元素
int i = this.age - s.age;//按照年龄排序
int sum = i == 0 ? this.name.compareTo(s.name):i;//如果年龄一样就按照姓名排序
return sum;
}
}
创建TreeSet集合类
public class TreeSetDemoStudent {
/*
Student{name='周冬雨', age=18}
Student{name='马天宇', age=18}
Student{name='黄四郎', age=20}
Student{name='黄四郎替身', age=21}
*/
public static void main(String[] args) {
//创建学生类对象
Student student1 = new Student("周冬雨",18);
Student student2 = new Student("黄四郎",20);
Student student3 = new Student("黄四郎替身",21);
Student student4 = new Student("马天宇",18);
//创建集合对象
TreeSet<Student> treeSet = new TreeSet<>();
treeSet.add(student1);
treeSet.add(student2);
treeSet.add(student3);
treeSet.add(student4);
for (Student student : treeSet) {
System.out.println(student);
}
}
}
1到20之间不重复的随机数
/*
1到20之间不重复的随机数
*/
public class Demo {
public static void main(String[] args) {
HashSet<Integer> hashSet = new HashSet<>();
Random random = new Random();
while (hashSet.size()<10){
int i = random.nextInt(20)+1;
hashSet.add(i);
}
for (Integer integer : hashSet) {
System.out.println(integer);
}
}
}
泛型类
创建泛型类i
public class GenericsDemo<T> {
private T t;
public GenericsDemo() {
}
public GenericsDemo(T t) {
this.t = t;
}
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
}
创建实现类
public class Test {
/*结果
hello
120
*/
public static void main(String[] args) {
GenericsDemo<String> demo = new GenericsDemo<>();
demo.setT("hello");
System.out.println(demo.getT());
GenericsDemo<Integer> integerGenericsDemo = new GenericsDemo<>();
integerGenericsDemo.setT(120);
System.out.println(integerGenericsDemo.getT());
}
}
Map集合
Map集合的特点
代码实现
/*
Map集合特点
是接口 Map<K,V> K:键的类型 V:值得类型
将建映射到值 不能包含重复的键 每个键只能映射到一个值
创建方式
多态的方式 具体实现对象 是HashMap
*/
public class MapDemo {
/*结果
{001=张飞, 002=关羽, 003=曹操01}
*/
public static void main(String[] args) {
Map<String, String> hashMap = new HashMap<>();
//添加方法 是put
hashMap.put("001","张飞");
hashMap.put("002","关羽");
hashMap.put("003","曹操");
hashMap.put("003","曹操01");//键一样,会替代掉值
System.out.println(hashMap);
}
}
Map集合的基本功能
代码实现
/*
Map集合的基本功能
*/
public class MapDemo01 {
/*结果
删除了键 张飞
是否包含001键 false
是否包含曹操 true
集合是否为空 false
集合的长度 2
最后输出集合 {002=关羽, 003=曹操}
*/
public static void main(String[] args) {
Map<String, String> hashMap = new HashMap<>();
//添加方法 是put
hashMap.put("001","张飞");
hashMap.put("002","关羽");
hashMap.put("003","曹操");
//移除功能
String remove = hashMap.remove("001");
System.out.println("删除了键 "+remove);
//判断是否包含指定的键
boolean b = hashMap.containsKey("001");
System.out.println("是否包含001键 "+b);
//判断是否包含指定的值
boolean b1 = hashMap.containsValue("曹操");
System.out.println("是否包含曹操 "+b1);
//判断是否为空
boolean empty = hashMap.isEmpty();
System.out.println("集合是否为空 "+empty);
//集合的长度
int size = hashMap.size();
System.out.println("集合的长度 "+size);
System.out.println("最后输出集合 "+hashMap);
}
}
Map集合的获取功能
代码实现
/*
Map集合的获取方式
*/
public class MapDemo03 {
/*结果
通过001键获取值 张飞
获取键的集合 [001, 002, 003]
获取值的集合 [张飞, 关羽, 曹操]
*/
public static void main(String[] args) {
Map<String, String> hashMap = new HashMap<>();
//添加方法 是put
hashMap.put("001","张飞");
hashMap.put("002","关羽");
hashMap.put("003","曹操");
//获取方式一
String s = hashMap.get("001");
System.out.println("通过001键获取值 "+s);
//获取方式二
Set<String> strings = hashMap.keySet();
System.out.println("获取键的集合 "+strings);
//获取方式三
Collection<String> values = hashMap.values();
System.out.println("获取值的集合 "+values);
}
}
Map集合遍历方式一
获取所有的键
然后通过增强foe循环得到每一个键 再根据键找出所有的值
代码
/*
Map集合的遍历方式
*/
public class MapDemo04 {
/*结果
001,张飞
002,关羽
003,曹操
*/
public static void main(String[] args) {
Map<String, String> hashMap = new HashMap<>();
//添加方法 是put
hashMap.put("001","张飞");
hashMap.put("002","关羽");
hashMap.put("003","曹操");
Set<String> strings = hashMap.keySet();
for (String string : strings) {
String s = hashMap.get(string);
System.out.println(string+","+s);
}
}
}
遍历方式二
代码实现
/*
Map集合遍历方式二
*/
public class MapDemo05 {
/*结果
001,张飞
002,关羽
003,曹操
*/
public static void main(String[] args) {
Map<String, String> hashMap = new HashMap<>();
//添加方法 是put
hashMap.put("001","张飞");
hashMap.put("002","关羽");
hashMap.put("003","曹操");
Set<Map.Entry<String, String>> entries = hashMap.entrySet();
for (Map.Entry<String, String> entry : entries) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key+","+value);
}
}
}
练习
创建集合对象 添加学生对象 并且遍历出来
创建学生对象
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private String name;
private Integer age;
}
创建集合对象 并添加学生对象 并遍历
public class HashMapDemo {
/*
001号学生 Student(name=张飞, age=25)
002号学生 Student(name=黄忠, age=40)
*/
public static void main(String[] args) {
Student student = new Student("张飞",25);
Student student2 = new Student("黄忠",40);
//创建集合对象
HashMap<String, Student> hashMap = new HashMap<>();
hashMap.put("001",student);
hashMap.put("002",student2);
//遍历集合
Set<Map.Entry<String, Student>> entries = hashMap.entrySet();
for (Map.Entry<String, Student> entry : entries) {
Student value = entry.getValue();
String key = entry.getKey();
System.out.println(key+"号"+"学生 "+value);
}
}
}
练习二 ArrayList存储HashMap
创建HashMap集合 添加到ArrayList集合中
public class ArrayListAndHashMapDemo {
/*
{张飞=15, 赵云=16}
{小桥=16, 周瑜=15}
{司马懿=16, 曹操=15}
*/
public static void main(String[] args) {
//创建ArrayList集合对象
ArrayList<HashMap<String,String>> hashMaps = new ArrayList<>();
HashMap<String, String> stringStringHashMap = new HashMap<>();
stringStringHashMap.put("张飞","15");
stringStringHashMap.put("赵云","16");
HashMap<String, String> stringStringHashMap1 = new HashMap<>();
stringStringHashMap1.put("周瑜","15");
stringStringHashMap1.put("小桥","16");
HashMap<String, String> stringStringHashMap2 = new HashMap<>();
stringStringHashMap2.put("曹操","15");
stringStringHashMap2.put("司马懿","16");
hashMaps.add(stringStringHashMap);
hashMaps.add(stringStringHashMap1);
hashMaps.add(stringStringHashMap2);
//遍历
for (HashMap<String, String> hashMap : hashMaps) {
System.out.println(hashMap);
}
}
}
练习三 HashMap集合存储ArrayList集合并遍历
public class HashMapAndArrayListDemo {
/*
1号学生[Student(name=张飞, age=25)]
2号学生[Student(name=黄忠, age=40)]
*/
public static void main(String[] args) {
//创建HashMap集合对象
HashMap<Integer, ArrayList<Student>> integerArrayListHashMap = new HashMap<>();
//创建ArrayList集合对象
ArrayList<Student> students = new ArrayList<>();
ArrayList<Student> students2 = new ArrayList<>();
//创建学生对象
Student student = new Student("张飞",25);
Student student2 = new Student("黄忠",40);
//添加到ArrayList集合中
students.add(student);
students2.add(student2);
//把ArrayList集合对象添加到HashMap中
integerArrayListHashMap.put(01,students);
integerArrayListHashMap.put(02,students2);
//遍历HashMap
Set<Map.Entry<Integer, ArrayList<Student>>> entries =
integerArrayListHashMap.entrySet();
for (Map.Entry<Integer, ArrayList<Student>> entry : entries) {
ArrayList<Student> value = entry.getValue();
Integer key = entry.getKey();
System.out.println(key+"号学生"+value);
}
}
}
Collections类的使用方法