一、集合框架结构
二、集合
-
Collection
Collection是单列集合的顶层接口,JDK不提供此接口的直接实现,它提供更具体的子接口的实现。
创建Collection集合的对象:
1.多态的方式
2.具体的实现类ArrayList
常用方法
- boolean add(E e) 添加元素,永远返回true
- boolean remove(Object o) 从该集合中删除指定元素的单个实例
- void clear() 从此集合中删除所有元素(可选操作)
- boolean isEmpty() 如果此集合不包含元素,则返回 true
- int size() 返回此集合中的元素数。
- Iterator iterator() 返回此集合中元素的迭代器。
常用方法:E next()、boolean hasNest()
注意:存在并发异常的问题!(可用ListIterator迭代器解决)
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/*
创建一个存储学生的集合,存储三个学生对象,并遍历输出
思路:1.定义一个学生类
2.创建一个Collectin集合对象
3.创建学生对象
4.把学生对象添加到集合中
5.遍历集合
通过集合对象获取迭代器对象
通过迭代器对象的hasNext()方法判断是否还有元素
通过迭代器对象的next()方法获取下一个元素
*/
public class CollectionDemo01<s1> {
public static void main(String[] args) {
//1、创建Collection 集合对象---ArrayList
Collection<Student> c = new ArrayList<Student>();
//2、创建学生对象
Student s1 = new Student("张三",24);
Student s2 = new Student("李四",25);
Student s3 = new Student("张三",24);
//3、添加到集合中
c.add(s1);
c.add(s2);
c.add(s3);
//4、遍历集合(迭代器方式)
Iterator<Student> it = c.iterator();
while(it.hasNext()){
Student s = it.next();
System.out.println(s.getName() + ", " + s.getAge());
}
}
}
特有方法
- void add(int index, E item) 在指定的位置将指定的项目添加到滚动列表。
- E remove(int index) 删除指定索引的元素,返回被删除的元素
- E set(int index, E item) 修改指定索引的元素,返回被修改的元素
- E get(int index) 返回指定索引的元素
import java.util.ArrayList;
import java.util.List;
public class ListDemo01 {
public static void main(String[] args) {
//创建集合对象
List<String> list = new ArrayList<>();
//添加元素
list.add("hello");
list.add("java");
list.add("world");
System.out.println(list);
//获取指定索引元素
list.get(2);
System.out.println(list);
list.add(1,"java");
System.out.println(list);
//删除指定索引元素
list.remove(1);
System.out.println(list);
//修改指定索引元素
list.set(0,"zhang");
System.out.println(list);
}
}
输出结果:
- ListIterator listIterator() 返回列表中的列表迭代器
public class List {
public static void main(String[] args) {
//创建集合对象
List<String> list = new ArrayList<>();
//添加元素
list.add("hello");
list.add("java");
list.add("world");
System.out.println(list);
//4、遍历集合(迭代器方式)
ListIterator<String> lit = list.listIterator();
while(lit.hasNext()){
String s = lit.next();
if (s.equals("world")){
lit.add("javee");
}
}
System.out.println(list);
}
}
输出结果:
特有功能
- void addFirst(E e) 在该列表开头插入指定的元素。
- void addLast(E e) 将指定的元素追加到此列表的末尾。
- E getFirst() 返回此列表中的第一个元素。
- E getLast() 返回此列表中的最后一个元素。
- E removeFirst() 从此列表中删除并返回第一个元素。
- E removeLast() 从此列表中删除并返回最后一个元素。
案例:HashSet集合存储学生对象并遍历
import java.util.HashSet;
import java.util.Objects;
//学生类定义
public class Student {
private String name;
private int age;
//构造方法
Student(){
}
Student(String name, int age){
this.age = age;
this.name = name;
}
public String getName(){
return this.name;
}
public int getAge(){
return this.age;
}
public void setName(String name){
this.name = name;
}
public void setAge(int age){
this.age = age;
}
//方法自动重写
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age && Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
//测试
public class SetDemo {
//主函数
public static void main(String[] args) {
//1、创建集合对象
HashSet<Student> hs =new HashSet<>();
//2、创建学生对象
Student s1 = new Student("张三",27);
Student s2 = new Student("李四",25);
Student s3 = new Student("王五",24);
Student s4 = new Student("李四",25);
//3、添加到集合中
hs.add(s1);
hs.add(s2);
hs.add(s3);
hs.add(s4);
//4、遍历集合
for(Student s : hs){
System.out.println(s.getName() + ", " + s.getAge());
}
}
}
输出结果:
案例:利用TreeSet存储学生对象,并按照年龄进行排序。
public class Student implements Comparable<Student>{
//对Comparable接口中的compareTo()方法重写
private String name;
private int age;
//构造方法
Student(){
}
Student(String name, int age){
this.age = age;
this.name = name;
}
public String getName(){
return this.name;
}
public int getAge(){
return this.age;
}
public void setName(String name){
this.name = name;
}
public void setAge(int age){
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age && Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
@Override
public int compareTo(Student o) {
//按照年龄排序
int num = this.age - o.age;
//年龄相同时,按照姓名字母顺序排序
int num2 = num == 0 ? this.name.compareTo(o.name) : num;
return num2;
}
}
public class TreeSetDemo {
public static void main(String[] args) {
//1、创建集合对象
TreeSet<Student> ts =new TreeSet<Student>();
//2、创建学生对象
Student s1 = new Student("张三",27);
Student s2 = new Student("李四",25);
Student s3 = new Student("王五",24);
Student s4 = new Student("唐唐",25);
//3、添加到集合中
ts.add(s1);
ts.add(s2);
ts.add(s3);
ts.add(s4);
//4、遍历集合
for(Student s : ts){
System.out.println(s.getName() + ", " + s.getAge());
}
}
}
输出结果:
基本功能
- V get(Object key) 返回指定键映射到的值,如果此映射不包含该键的映射,则返回 null
- V put(K key, V value) 添加元素
- V remove(Object key) 根据键删除键值对
- void clear()
- boolean containsKey(Object key) 如果此映射包含指定键的映射,则返回 true 。
- boolean containsValue(Object value) 如果此映射将一个或多个键映射到指定的值,则返回 true 。
- int size() 集合的长度
- boolean isEmpty() 判断集合是否是空
- Set keySet() 获取所有键的集合
- Set<Map.Entry<K,V>> entrySet() 获取所有键值对对象的集合
- Collection values() 获取所有值的集合
-
HashMap
案例:HashMap存储学生对象并遍历,键是学生对象(Student),值是居住地(String),并遍历。
public class HashMapDemo {
public static void main(String[] args) {
//1、创建集合对象
HashMap<Student, String> hm = new HashMap<>();
//2、创建学生对象
Student s1 = new Student("张三",27);
Student s2 = new Student("李四",25);
Student s3 = new Student("王五",24);
Student s4 = new Student("李四",25);
//3、添加到集合中
hm.put(s1, "南京");
hm.put(s2, "西安");
hm.put(s3, "郑州");
hm.put(s4, "北京");
//4、遍历集合
// Set<Student> keySet = hm.keySet();
// for(Student s : keySet){
// String value = hm.get(s);
// System.out.println(s.getName() + ", " + s.getAge() + ", " + value);
// }
Set<Map.Entry<Student, String>> set = hm.entrySet();
for(Map.Entry<Student, String> s : set){
Student key = s.getKey();
String value = s.getValue(); //ctrl+alt+v
System.out.println(key.getName() + ", " + key.getAge() + ", " + value);
}
}
}
JavaSE入门: JavaSE基础语法01
JavaSE入门: JavaSE基础语法之面向对象和异常02
JavaSE入门: JavaSE基础语法之常见类03