1.泛型
泛型是jdk1.5版本后的新特性,泛型的本质是参数化类型,将对象的类型作为参数,指定到其他类或者方法上,从而保证类型转换的安全性和稳定性。
泛型的定义语法格式:
类1或者接口<类型实参> 对象 = new 类2<类型实参>();
说明:类2可以是类1的本身,也可以是类1的子类,还可以是接口的实现类。
类2的类型实参必须和类1的类型实参相同。
2.泛型集合
泛型集合可以约束集合内的元素类型
典型泛型集合ArrayList<E>、HashMap<K,V>
<E>,<K,V>表示该泛型集合中的元素类型
泛型集合中的数据不再转换为Object
除了指定了集合中的元素类型外,泛型集合和集合的用法完全一样。
举例说明:ArrayList集合使用泛型
public static void main(String[] args) {
//1.准备数据:创建NewsTitle对象
NewsTitle nt1=new NewsTitle(100, "俄乌战争走向", "国际社");
NewsTitle nt2=new NewsTitle(200, "国庆放假通知", "学校通知");
NewsTitle nt3=new NewsTitle(300, "金坷垃之谜", "圣地亚哥");
//创建ArrayList对象,这个类在java.util包中,需要导包
ArrayList<NewsTitle> alt=new ArrayList<NewsTitle>();
//通过add()方法向集合添加元素
alt.add(nt1);
alt.add(nt2);
alt.add(nt3);
alt.add(nt2);
//add(int index,Object obj)方法:将元素存储到集合指定位置
alt.add(2, nt1);
//对集合中元素的操作,都是通过集合对象名alt调用方法来实现
//获取集合元素个数,即集合长度:size()方法
int size=alt.size();
System.out.println("alt集合中的元素个数:"+size);
//获取集合中的指定元素:get(int index)方法
NewsTitle newsTitle=alt.get(3);//返回值是什么类型的就用什么类型的变量去接收
System.out.println(newsTitle);
System.out.println("-----------");
//遍历集合元素有三种方式:普通for()循环,增强for()循环,迭代器
//普通for()循环
for (int i = 0; i < alt.size(); i++) {
NewsTitle newsTitle1=alt.get(i);
System.out.println(newsTitle1);
}
//删除集合中的元素:remove(元素名/元素下标)方法
boolean boo=alt.remove(nt1);
System.out.println("删除了nt1:"+boo);
//使用增强for循环遍历删除后的集合
for(NewsTitle ntl:alt){
System.out.println(ntl);
}
System.out.println("----使用迭代器-----");
//使用迭代器来遍历集合
Iterator<NewsTitle> it=alt.iterator();
while(it.hasNext()){
NewsTitle newst=it.next();
System.out.println(newst);
}
//判断集合中是否包含某个元素:contains()方法
boolean result=alt.contains(nt3);
System.out.println("集合中包含nt3这个元素:"+result);
//将集合中的元素转换为数组:toArray()方法
Object[] objs=alt.toArray();
//遍历数组
for (int i = 0; i < objs.length; i++) {
System.out.println(objs[i]);
}
//清空集合:clear()方法
alt.clear();
System.out.println(alt.size());
//isEmpty()方法
boolean boo1=alt.isEmpty();
System.out.println("集合元素清空成功:"+boo1);
}
HashMap集合使用泛型
public static void main(String[] args) {
//创建学生对象
Student stu1=new Student("张三", 21);
Student stu2=new Student("李四", 23);
Student stu3=new Student("李华", 32);
//创建HashMap对象
HashMap<Integer,Student> hm=new HashMap<Integer, Student>();//也可以用多态:Map map=new HashMap();
hm.put(1, stu1);
hm.put(2, stu2);
hm.put(3, stu3);
//遍历map集合
//法一:先获取所有的键keySet()方法,再通过get()方法获取对应的值
Set<Integer> keys=hm.keySet();
for(int key1:keys){
Student student=hm.get(key1);
System.out.println(key1+"对应的值是:"+student);
}
System.out.println("-----------------");
//法二:通过迭代器遍历:iterator()方法
Iterator<Integer> it=keys.iterator();//把所有的键转移到Iterator中
while(it.hasNext()){
int key2=it.next();
Student student2=hm.get(key2);
System.out.println(key2+"对应的值是:"+student2);
}
System.out.println("--------------------");
//法三:通过获取键值对来获取键和值
//entrySet()方法:获取键值对
Set<Map.Entry<Integer, Student>> keysValues=hm.entrySet();//键值对的类型是Map.Entry,键的类型是Integer,值的类型是Student
for (Map.Entry<Integer, Student> me: keysValues) {
//获取键
int key3=me.getKey();
//获取值
Student student3=me.getValue();
System.out.println(key3+"对应的值是:"+student3);
}
}
3.Collections算法类
Collections类是Java提供的一个集合操作工具类,定义了一系列用于操作集合的静态方法,用于实现对集合元素的排序、查找和替换等操作。
Collections和Collection是不同的,Collections是集合的操作类,Collection是集合接口。
Collections类提供的常用的静态方法:sort()排序方法,binarySearch()查找方法,max()/min()查找最大值/最小值方法。
//Collections类:提供操作集合的方法,这些方法都是静态方法
public static void main(String[] args) {
//创建ArrayList对象
ArrayList<String> al=new ArrayList<String>();
//向集合al中存储元素
al.add("ikun");
al.add("abc");
al.add("fed");
al.add("fgr");
al.add("vbh");
al.add("ert");
//遍历集合:for循环,增强for循环和迭代器都可
Iterator<String> it=al.iterator();
while(it.hasNext()){
String String=it.next();
System.out.print(String+" ");
}
System.out.println();
//对集合排序:sort()方法
Collections.sort(al);
System.out.println("----排序后-----");
for (String string : al) {
System.out.print(string+" ");
}
System.out.println();
//查找集合元素:binarySearch()方法
//和数组一样,查找前需要先对集合进行排序
int index=Collections.binarySearch(al, "ikun");
System.out.println(index);
System.out.println(Collections.binarySearch(al, "aaa"));//找不到就会返回一个负值
//求集合中的最大值和最小值:max()方法和min()方法
String strMax=Collections.max(al);
System.out.println(strMax);
String strMin=Collections.min(al);
System.out.println(strMin);
}
3.1 对集合元素排序和查找
1.排序是针对一个集合的常见操作。排序的前提是要知道两个元素哪个大哪个小。在Java中,如果要实现一个类的对象之间比较大小,那么这个类就要实现Comparable接口。
2.Comparable接口强行对实现它的每个类的对象进行整体排序。这种排序被称为类的自然排序,类的compareTo()方法被称为它的自然比较方法。
3.compareTo()方法用于比较此对象与指定对象的顺序,如果该对象小于、等于或大于指定对象,则分别返回负整数、零或正整数。
4.元素之间可以比较大小之后,就可以使用Collections类的sort()方法对元素进行排序了。Set接口本身是无序的,故不能对Set接口做排序操作;List接口是有序的,可以对List接口进行排序。
5.使用binarySearch()方法可以查找集合中的元素,但是在使用binarySearch()方法之前需要使用sort()方法对集合进行排序,否则不能保证查找结果的正确性。
举例说明:按照年龄比较学生对象大小。
编写学生类Student
public class Student implements Comparable<Student> {
private String name;
private int age;
public Student() {
super();
}
public Student(String name, int age) {
super();
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;
}
//重写compareTo()方法,给sort()方法规定一个规则去排序,如:按照年龄排序
public int compareTo(Student stu) {
//此对象大于、等于、小于指定对象,分别返回正整数、零、负整数;
if(this.getAge()>stu.getAge()){
return 1;
}else if(this.getAge()==stu.getAge()){
return 0;
}else {
return -1;
}
}
}
编写测试类
public static void main(String[] args) {
//创建学生对象
Student stu1=new Student("赵四", 22);
Student stu2=new Student("王二", 24);
Student stu3=new Student("张三",19);
Student stu4=new Student("李华",20);
Student stu5=new Student("吴勇",27);
//创建LinkedList对象
LinkedList<Student> link=new LinkedList<Student>();
link.add(stu1);
link.add(stu2);
link.add(stu3);
link.add(stu4);
link.add(stu5);
//遍历
for (Student student : link) {
System.out.println(student.getName()+" "+student.getAge());
}
//排序:不能直接用sort()方法对对象排序,因为不清楚排序规则,需要在实现类Student类中实现Comparable接口,重写compareTo()方法
Collections.sort(link);
System.out.println("---排序后----");
for (Student student : link) {
System.out.println(student.getName()+" "+student.getAge());
}
}