泛型(了解)
作用(重点)
将数据类型作为参数进行传递
定义
语法:
<自定义的字母1, 自定义的字母2, ...>
如:
<K,V>
理解:
可以理解为是数据类型的占位符,没有使用时无法确定其数据类型
使用
在方法上使用
语法:
访问权限修饰符 修饰符 <自定义泛型> 返回值类型 方法名(形参列表){
方法体
}
优点:
该方法任何一处使用不确定的数据类型的地方都可以使用该自定义的泛型代替
在类上使用
语法:
访问权限修饰符 class 类名<自定义泛型>{
}
优点:
该类任何一处使用不确定的数据类型的地方都可以使用该自定义的泛型代替
在接口上使用
语法:
访问权限修饰符 interface 接口名<自定义的泛型>{
}
优点:
可以在接口中使用不确定的数据类型的地方都可以使用该自定义的泛型代替
注意:
公共常量处不能使用
公共静态方法不能使用
default修饰的方法可以使用
抽象方法可以使用
注意(重点)
泛型只能传递引用数据类型
package demo01;
//在方法上使用
/**
* 需求:
* 定义一个方法
* 要求:
* 1.该方法可以传入任意一种数据类型的参数
* 2.该方法传入的参数数据类型就是返回值的数据类型
*/
public class Test01 {
public static void main(String[] args) {
String str = test("你好世界"); //可以传入String类型的
Integer i = test(10); //可以传入Integer类型的
Double d = test(1.1); //可以传入Double类型的
A a = test(new A()); //可以传入自定义的类型
}
//定义一个带有泛型的方法
public static <T> T test(T t) {
return t;
}
}
class A{
}
package demo01;
//在类上使用
public class Test02 {
public static void main(String[] args) {
//创建带有泛型的类的对象
AA<String> aa1 = new AA<String>("abc");
AA<Integer> aa2 = new AA<Integer>(0);
BB<String,Integer,Double> bb = new BB<String,Integer,Double>();
}
}
class AA<T>{
private T t;
public AA() {
}
public AA(T t) {
this.t = t;
}
public void test(T t) {
}
public T test0() {
return t;
}
}
//子类继承于有泛型的父类,那么子类的泛型要包含父类的泛型
class BB<T,K,V> extends AA<T>{
}
package demo01;
//在接口上使用
public class Test03 {
public static void main(String[] args) {
C<String> c1 = new C<String>();
C<Integer> c2 = new C<Integer>();
//证明泛型只能传递引用数据类型
//C<int> c3 = new C<int>();
}
}
interface CInterface<Q>{
default Q test01(Q q) {
return q;
}
public abstract Q test02(Q q);
}
//实现带有泛型的接口,实现类(子类)的泛型要包含父接口的所有泛型
class C<Q> implements CInterface<Q>{
@Override
public Q test02(Q q) {
return null;
}
}
//继承带有泛型的接口时,子接口要包含父接口的所有泛型
interface DInterface<Q> extends CInterface<Q>{
}
集合
概念
存储一组数据类型相同的数据的容器
特点
1.长度可变
2.只能存储引用数据类型
体系结构
Collection(接口): 所有集合类的顶级接口
List(接口): Collection的子接口
ArrayList: List的子类
LinkedList: List的子类
Vector: List的子类
Set(接口): Collection的子接口
HashSet: Set的子类
LinkedHashSet: Set的子类
TreeSet: Set的子类
Queue(接口): Collection的子接口
就是队列(队列: 先进先出 栈: 后进先出)
明白:
1.所有集合类的对象都可以转换为Collection的对象
2.Collection提供的方法是所有集合类对象拥有的方法
3.子类对象转换为父类对象后不能再使用子类特有的属性与方法
List与Set的区别:
List: 有序,有下标,数据重复
Set: 无序,无下标,数据不可重复
注意: 这里的有序指的是存入顺序与取出顺序一致
Collection
提供的方法:
增
boolean add(e)
作用: 一次添加一个元素
参数: 本次添加的元素
返回值: 是否添加成功
boolean addAll(c)
作用: 一次添加一个集合数据
参数: 本次添加的集合
返回值: 是否添加成功
删
boolean remove(o)
作用: 删除指定元素
参数: 被删除的元素
返回值: 是否删除成功
注意:
如果删除的元素,不在集合中,会删除失败
如果集合中存在多个该元素,只能删除第一个
boolean removeAll(c)
作用: 删除一组数据
参数: 被删除的数据
返回值: 是否删除成功
注意:
只能删除一个元素也算删除成功
删除集合中所有指定的该数据
clear()
作用: 清空(将集合中的数据全部删除)
查
size()
作用: 获取集合长度
boolean contains(o)
作用: 判断集合中是否存在指定的元素
参数: 指定的元素
返回值: 是否存在
boolean containsAll(c)
作用: 判断集合中是否存在指定的一组元素
参数: 指定的一组元素
返回值: 是否存在
注意: 只有全部包含才返回true
boolean isEmpty()
作用: 判断是否为空集合(空集合: 集合中没有存储元素)
返回值: 是否为空
注意: 空集合与空(null)不一样
其他
iterator()
作用: 获取迭代器
迭代器提供的方法:
boolean hasNext()
作用: 判断集合中是否还有下一个元素
返回值: 是否有下一个元素
E next()
作用: 移动游标到下一个元素,并获取该元素
返回值: 获取到的元素
练习
package demo02;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Test01 {
public static void main(String[] args) {
// 1.给集合中存储以下元素,并获取集合长度打印输出
// 张三,李四,王五,马六,候七,张三
Collection<String> c1 = new ArrayList<String>();
c1.add("张三");
c1.add("李四");
c1.add("王五");
c1.add("马六");
c1.add("侯七");
c1.add("张三");
System.out.println(c1.size());
// 2.删除集合所有张三的元素,并获取集合长度打印输出
Collection<String> c0 = new ArrayList<String>();
c0.add("张三");
c1.removeAll(c0);
System.out.println(c1.size());
// 3.创建一个新的集合,给新集合存储以下元素
// 老王,老李,老张,大黄
Collection<String> c2 = new ArrayList<String>();
c1.add("老王");
c1.add("老李");
c1.add("老张");
c1.add("大黄");
// 4.将3题中的集合的元素添加到题目一的集合中
c1.addAll(c2);
System.out.println(c1);
// 5.创建一个新的集合,给新集合存储以下元素
// 李四,王五,大黄
Collection<String> c3 = new ArrayList<String>();
c3.add("李四");
c3.add("王五");
c3.add("大黄");
// 6.从题目一的集合中删除题目五集合中数据
c1.removeAll(c3);
System.out.println(c1);
// 7.遍历题目一的集合(两种方式)
for (String s : c1) {
System.out.println(s);
}
System.out.println("---------------");
Iterator<String> iterator = c1.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
运行结果
package demo02;
//求两个集合的交集
import java.util.ArrayList;
import java.util.Collection;
public class Test02 {
public static void main(String[] args) {
Collection<String> c1 = new ArrayList<String>();
c1.add("张三");
c1.add("李四");
c1.add("王五");
c1.add("马六");
c1.add("侯七");
c1.add("张三");
Collection<String> c2 = new ArrayList<String>();
c2.add("张三");
c2.add("李四");
c2.add("王富贵");
c2.add("马乾坤");
c2.add("小倩");
c2.add("张三");
Collection<String> c0 = new ArrayList<String>();
for (String s : c1) {
if(c2.contains(s)) {
if(!c0.contains(s)) {
c0.add(s);
}
}
}
System.out.println("交集为: " + c0);
}
}
运行结果
package demo02;
//求两个集合的并集
import java.util.ArrayList;
import java.util.Collection;
public class Test03 {
public static void main(String[] args) {
Collection<String> c1 = new ArrayList<String>();
c1.add("张三");
c1.add("李四");
c1.add("王五");
c1.add("马六");
c1.add("侯七");
c1.add("张三");
Collection<String> c2 = new ArrayList<String>();
c2.add("张三");
c2.add("李四");
c2.add("王富贵");
c2.add("马乾坤");
c2.add("小倩");
c2.add("张三");
Collection<String> c0 = new ArrayList<String>();
for (String s : c1) {
if(!c0.contains(s)) {
c0.add(s);
}
}
for (String s : c2) {
if(!c0.contains(s)) {
c0.add(s);
}
}
System.out.println("并集为: " + c0);
}
}
运行结果