集合
一:集合概念
对象的容器,实现了对对象常用的操作,类似数组的功能
和数组的区别:
- 数组长度固定,集合长度不固定
- 数组可以存储基本类型和引用类型,集合只能存储引用类型
位置:java.util.*;
Collection体系集合
Collection父接口
-
特点:代表一组任意类型的对象,无序、无下标、不能重复
-
方法:
- boolean add(Object obj) //添加一个对象
- boolean addAll(Collection c) //将一个集合中的所有对象添加到刺集合中
- void clear() //清空此集合中的所有对象
- boolean contains(Object o) //检查此集合中是否包含o对象
- boolean equals(Object o) //比较此集合是否与指定对象相等
- boolean isEmpty() //判断此集合是否为空
- boolean remove(Object o) //在此集合中移除o对象
- int size() //返回此集合中的元素个数
- Object [] toArray() //将此集合转换成数组
package Util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
* Collection接口的使用
* (1)添加元素
* (2)删除元素
* (3)遍历元素
* (4)判断
* @author wgl
*
*/
public class C1 {
public static void main(String[] args) {
//创建集合
Collection collection = new ArrayList();
//添加元素
collection.add("苹果");
collection.add("西瓜");
collection.add("榴莲");
System.out.println("元素个数:"+collection.size());
System.out.println(collection);
//删除元素
//collection.remove("榴莲");
//System.out.println("元素个数:"+collection.size());
//清空
//collection.clear();
//System.out.println("元素个数:"+collection.size());
//3-1遍历元素*不能使用for,可使用增强for
System.out.println("-----------------------------");
for (Object object:collection){
System.out.println(object);
}
/*3-2使用迭代器(迭代器专门是用来遍历集合的一种方式)
hasNext(); 有没有下个元素
next(); 获取下个元素
remove(); 删除当前元素
*/
Iterator it = collection.iterator();
while (it.hasNext()){
String s = ((String)it.next());
System.out.println(s);
//it.remove();
}
System.out.println("元素个数:"+collection.size());
//4.判断
System.out.println(collection.contains("西瓜"));//有西瓜返回true
System.out.println(collection.isEmpty());//为空返回true
}
}
package Util;
import LastCLass.Class7_Student;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class C2 {
public static void main(String[] args) {
//新建集合
Collection collection = new ArrayList();
C2_student s1 = new C2_student("张三", 22);
C2_student s2 = new C2_student("王五", 18);
C2_student s3 = new C2_student("李四", 8);
//添加元素
collection.add(s1);
collection.add(s2);
collection.add(s3);
collection.add(s3);//List可重复添加
System.out.println("元素个数:"+collection.size());
System.out.println(collection.toString());
//删除
//collection.remove(s1);
//System.out.println("删除之后元素个数:"+collection.size());
//3。遍历
//3.1增强for
System.out.println("--------------增强for-------------");
for (Object object:collection){
C2_student s = (C2_student)object;
System.out.println(s.toString());
}
//3.2迭代器:hasNext() next(); remove(); 迭代过程中不能使用collection的删除方法
System.out.println("----------迭代器------------");
Iterator it = collection.iterator();
while(it.hasNext()){
C2_student s = (C2_student) it.next();
System.out.println(s.toString());
}
//判断
System.out.println(collection.contains(s1));
}
}
package Util;
public class C2_student {
private String name;
private int age;
public C2_student() {
}
public C2_student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "C2_student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
List子接口
- 特点:有序、有下标、元素可重复
- 方法
- void add(int index,Object o) //在index位置插入对象o
- boolean addAll(int index,Collectionc) //将一个集合中的元素添加到此集合中的index位置
- Object get(int index) //返回集合中指定位置的元素
- List subList (int fromIndex,int to Index) //返回fromIndex和toIndex之间的集合元素。
package Util;
import com.sun.org.apache.bcel.internal.generic.FSUB;
import java.util.ArrayList;
import java.util.List;
/**
* List的使用
* @author wgl
*/
public class L2 {
public static void main(String[] args) {
//创建集合
List list = new ArrayList();
//添加数字数据(自动装箱)
list.add(20);//不是基本类型的20,而是一个类
list.add(30);
list.add(40);
list.add(50);
list.add(60);
System.out.println("元素个数:"+list.size());
System.out.println(list.toString());
//2删除
list.remove(2);
System.out.println("删除元素后:"+list.size());
System.out.println(list.toString());
list.remove((Object)20);//转成Object类型或者new Integer(20)
System.out.println(list.toString());
//补充方法subList;返回子集合,含头不含尾
List sublist = list.subList(1,3);
System.out.println(sublist.toString());
}
}
ArrayList
-
数组结构实现,查询快、增删慢;
-
JDK1.2版本,运行效率快、线程不安全。
-
源码分析:DEFAULT_CAPACITY = 10;默认容量
elementData存放元素的数组
size 实际元素个数
如果没有向集合中添加任何数据是容量为0
每次扩容原来的1.5倍
Vector
- 数组结构实现,查询快、增删慢;
- JDK1.0版本,运行效率慢、线程安全。
LinkedList
- 链表结构实现,增删快,查询慢。
ArrayList代码:
package Util;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
/**
* ArrayList的使用
* 存储结构:数组,查找遍历速度快,增删慢
* @author
*/
public class AL1 {
public static void main(String[] args) {
//创建集合
ArrayList arrayList = new ArrayList();
//1添加元素
C2_student s1 = new C2_student("刘德华",20);
C2_student s2 = new C2_student("郭富城",22);
C2_student s3 = new C2_student("梁朝伟",24);
arrayList.add(s1);
arrayList.add(s2);
arrayList.add(s3);
System.out.println("元素个数:"+arrayList.size());
System.out.println(arrayList.toString());
//2删除元素
//arrayList.remove(s1);
//System.out.println("删除后元素个数:"+arrayList.size());
//3使用迭代器
Iterator it = arrayList.iterator();
while(it.hasNext()){
C2_student s = (C2_student)it.next();
System.out.println(s.toString());
}
//3.1列表迭代器
ListIterator lit = arrayList.listIterator();
System.out.println("-----3.1列表迭代器-----");
while(lit.hasNext()){
C2_student s = (C2_student)lit.next();
System.out.println(s.toString());
}
System.out.println("--------3.2使用列表迭代器逆序---------");
while(lit.hasPrevious()){
System.out.println(s1.toString());
}
//4判断
System.out.println(arrayList.contains(new C2_student("梁朝伟",24)));
System.out.println(arrayList.isEmpty());
//5查找
System.out.println(arrayList.indexOf(new C2_student("刘德华",20)));
}
}
Vector代码:
package Util;
import javax.swing.*;
import java.util.Enumeration;
import java.util.Vector;
/**
* 演示Vector集合的使用
* 存储结构:数组
* @author wgl
*/
public class V {
public static void main(String[] args) {
//创建集合
Vector vector = new Vector<>();
//1添加元素
vector.add("草莓");
vector.add("芒果");
vector.add("西瓜");
System.out.println("元素个数:"+vector.size());
//2删除
//vector.remove(0);
//vector.remove("西瓜");
//vector.clear();//清空
//3遍历
//3.1使用枚举器
Enumeration en = vector.elements();
while(en.hasMoreElements()){
String o = (String)en.nextElement();
System.out.println(o);
}
//4判断
System.out.println(vector.contains("西瓜"));
System.out.println(vector.isEmpty());
//5.vector其他方法
//firstElement、lastElement、elementAT();
}
}
LinkedList代码:
package Util;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
/**
* LinkedList的使用
* 存储结构:双向链表
* @author wgl
*/
public class V2 {
public static void main(String[] args) {
//创建集合
LinkedList linkedList = new LinkedList<>();
//1添加元素
C2_student s1 = new C2_student("王五",20);
C2_student s2 = new C2_student("赵四",22);
C2_student s3 = new C2_student("张三",24);
linkedList.add(s1);
linkedList.add(s2);
linkedList.add(s3);
linkedList.add(s3);//可重复
System.out.println("元素个数:"+linkedList.size());
System.out.println(linkedList.toString());
//2删除
linkedList.remove(s1);
System.out.println(linkedList.toString());
//3.1for遍历
System.out.println("for");
for(int i=0;i<linkedList.size();i++){
System.out.println(linkedList.get(i));
}
//3.2增强for
System.out.println("增强for");
for (Object object:linkedList) {
C2_student s = (C2_student) object;
System.out.println(s.toString());
}
//3.3使用迭代器
System.out.println("迭代器");
Iterator it = linkedList.iterator();
while(it.hasNext()){
C2_student z = (C2_student)it.next();
System.out.println(z.toString());
}
//3.4列表迭代器
System.out.println("列表迭代器");
ListIterator lit = linkedList.listIterator();
while(lit.hasNext()){
C2_student z = (C2_student)lit.next();
System.out.println(z.toString());
}
}
}
ArrayList和LinkedList的区别
泛型
- Java泛型是JDK1.5中引入的一个新特性,其本质是参数化类型,把类型作为参数传递。
- 常见的形式有泛型类、泛型接口、泛型方法。
- 语法:
- <T…>T称为类型占位符,表示一种引用类型。
- 好处
- 1.提高代码的重用性
- 2.防止类型转换异常,提高代码的安全性
泛型集合
- 概念:参数化类型、类型安全的集合,强制集合元素的类型必须一致。
- 特点:
- 编译时即可检查,而非运行时抛出异常
- 访问时,不必类型转换(拆箱)
- 不同泛型之间引用不能相互赋值,泛型不存在多态
Set集合
Set子接口
-
特点:无序、无下标、元素不可重复
-
方法:全部继承自Collection方法
-
package StudySet; import java.util.HashSet; import java.util.Iterator; import java.util.Set; /** * 测试Set接口的使用 * 特点:1.无序 2.无下标 3.不能重复 * @author wgl */ public class Demo1 { public static void main(String[] args) { //创建集合 Set<String> set = new HashSet<>(); //1添加数据 set.add("苹果"); set.add("华为"); set.add("小米"); System.out.println("数据个数:"+set.size()); System.out.println(set.toString()); //2删除数据 set.remove("小米"); System.out.println(set.toString()); //3遍历* //3.1增强for,不能用for,因为没有角标 System.out.println("--------增强for----------"); for (String string:set){ System.out.println(string); } //3.2使用迭代器 System.out.println("-------迭代器---------"); Iterator<String> it = set.iterator(); while (it.hasNext()){ System.out.println(it.next()); } //判断 System.out.println("--------判断---------"); System.out.println(set.contains("华为")); System.out.println(set.isEmpty()); } }
Set实现类
- HashSet*
- 基于HashCode实现元素不重复
- 当存入元素的哈希吗相同时,会调用equals进行确认,如结果为true,则拒绝后者存入。
- TreeSet
- 基于排列顺序实现元素不重复。
- 实现了SortedSet接口, 对集合元素自动排序。
- 元素对象的类型必须实现Comparable接口,指定排序规则。●通过CompareTo方法确定是否为重复元素。
- 输出时自动排序
使用TreeSet集合实现字符串按长度进行排序
package StudySet;
import java.util.Comparator;
import java.util.TreeSet;
/**
* 要求:使用TreeSet集合实现字符串按长度进行排序
* @author wgl
*/
public class Demo6 {
public static void main(String[] args) {
//创建集合,并指定比较规则
TreeSet<String> treeSet = new TreeSet<>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
int n1 = o1.length()-o2.length();
int n2 = o1.compareTo(o2);
return n1==0?n2:n1;
}
});
//添加数据
treeSet.add("hello");
treeSet.add("xian");
treeSet.add("nanjing");
treeSet.add("beiji");
treeSet.add("the");
treeSet.add("changsha");
treeSet.add("lisi");
System.out.println(treeSet.toString());
}
}
or() {
@Override
public int compare(String o1, String o2) {
int n1 = o1.length()-o2.length();
int n2 = o1.compareTo(o2);
return n1==0?n2:n1;
}
});
//添加数据
treeSet.add(“hello”);
treeSet.add(“xian”);
treeSet.add(“nanjing”);
treeSet.add(“beiji”);
treeSet.add(“the”);
treeSet.add(“changsha”);
treeSet.add(“lisi”);
System.out.println(treeSet.toString());
}
}