泛型:将对象的类型作为参数,指定到其他类或者方法上,从而保证类型转换的安全性和稳定性
语法结构如下:类或者接口<类型实参> 对象 = new 类<类型实参>( );
泛型集合
泛型集合可以约束集合内的元素类型
典型泛型集合ArrayList<E>、HashMap<K,V>
<E>、<K,V>表示该泛型集合中的元素类型
泛型集合中的数据不再转换为Object
- public class NewsTitle {
- private int id;
- private String name;
- private String author;
-
- public NewsTitle() {
- super();
- }
-
- public NewsTitle(int id, String name, String author) {
- super();
- this.id = id;
- this.name = name;
- this.author = author;
- }
-
- public int getId() {
- return id;
- }
-
- public void setId(int id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getAuthor() {
- return author;
- }
-
- public void setAuthor(String author) {
- this.author = author;
- }
-
- @Override
- public String toString() {
- return "NewsTitle [id=" + id + ", name=" + name + ", author=" + author+ "]";
- }
- }
- import java.util.ArrayList;
- import java.util.Iterator;
-
- public class Test {
-
- public static void main(String[] args) {
- NewsTitle nt1=new NewsTitle(001,"合肥又发现一名新冠肺炎感染者","合肥日报");
- NewsTitle nt2=new NewsTitle(001,"合肥近日多天有雨","合肥气象局");
- NewsTitle nt3=new NewsTitle(001,"震惊!有一程序员猝死","张三");
-
- //定义一个集合,调用ArrayList类的无参的构造方法,默认构造一个初始容量为 10 的空列表。
- ArrayList<NewsTitle> al=new ArrayList<NewsTitle>();
-
- //将新闻标题的对象存储到al集合中
- al.add(nt1);
- al.add(nt2);
- al.add(nt3);
-
- //获取新闻标题的总数,即获取集合中的元素个数
- int num=al.size();
- System.out.println("新闻的标题总数为"+num);
-
- //void add(int index,Object o):在指定的索引位置添加元素。索引位置必须介于0和列表中元素个数之
- al.add(0, nt3);
- NewsTitle news =al.get(1);
- System.out.println("获取元素:"+news);
-
- //boolean contains(Object o):判断列表中是否存在指定元素,如果集合中存在你要找的元素,返回true,否则返回false
- boolean result1=al.contains(nt3);
- System.out.println("集合中你需要找到的元素nt3:"+result1);
-
- //boolean remove(Object o):从列表中删除元素,删除成功返回true,删除失败返回false
- boolean result2=al.remove(nt2);
- System.out.println("删除是否成功:"+result2);
-
-
- //toArray():将集合变成数组
- Object[] obj3 =al.toArray();
- for (int i = 0; i < obj3.length; i++) {
- System.out.println("转换成数组:"+obj3[i]);
- }
- System.out.println("------------------");
- for(NewsTitle ob:al){
- System.out.println(ob);
- }
-
- //遍历打印出每个新闻的名称
- for(int i=0;i<al.size();i++){
- Object obj=al.get(i);
- NewsTitle newsTitle=(NewsTitle)obj;
- System.out.println(newsTitle.getName());
- }
-
- //清除集合所有的元素
- // al.clear();
-
- // al.clone();
-
- //判断是否为空
- boolean result=al.isEmpty();
- System.out.println(result);
-
- Iterator<NewsTitle> it =al.iterator();
- while(it.hasNext()){
- //取出元素
- NewsTitle nt =it.next();
- System.out.println(nt);
- }
- }
- }
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Set;
-
- public class Test {
-
- public static void main(String[] args) {
-
- //创建学生对象
- Student stu1 = new Student("张三", "男");
- Student stu2 = new Student("李四", "男");
- Student stu3 = new Student("如花", "女");
-
- //创建HashMap集合对象
- HashMap<String, Student> hm=new HashMap<String, Student>();
-
- //将学生对象添加到集合
- hm.put("jack", stu1);
- hm.put("tom", stu2);
- hm.put("rose", stu3);
-
- Object obj=hm.get("jack");
- Student stu =(Student)obj;
- System.out.println("jack对应的学员信息为:姓名:"+stu.getName()+",性别:"+stu.getGender());
-
- System.out.println("---------------");
- Set<Map.Entry<String, Student>> set=hm.entrySet();
- for (Map.Entry<String, Student> me : set) {
- //获取键值对应的键
- String key=me.getKey();
- //获取键值对应的值
- Student student=me.getValue();
- System.out.println(key+"对应的学员信息为:姓名:"+student.getName()+",性别:"+student.getGender());
- }
- }
- }
Collections算法类
Java集合框架将针对不同数据结构算法的实现都保存在工具类中
Collections类定义了一系列用于操作集合的静态方法
Collections类常用方法
Collections和Collection不同,前者是集合的操作类,后者是集合接口
Collections提供的常用静态方法
sort():排序
binarySearch():查找
max()\min():查找最大\最小值
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Iterator;
-
- public class Demo01 {
-
- public static void main(String[] args) {
- //创建一个ArrayList集合对象,存储String类型数据
- ArrayList<String> al=new ArrayList<String>();
-
- al.add("fadga");
- al.add("addfag");
- al.add("hmjej");
- al.add("fagevsf");
- al.add("jwryjwyr");
-
- System.out.println("集合排序之前:");
- for (String string : al) {
- System.out.println(string);
- }
-
- //排序
- Collections.sort(al);
-
- System.out.println("集合排序之后:");
- for (String string : al) {
- System.out.println(string);
- }
-
- //查找元素
- int index=Collections.binarySearch(al, "fagevsf");
- System.out.println(index);
-
- //求集合中的最大值最小值
- String max = Collections.max(al);
- System.out.println(max);
- String min = Collections.min(al);
- System.out.println(min);
-
- Collections.reverse(al);
- System.out.println("集合反转之后:");
- Iterator<String> it=al.iterator();
- while(it.hasNext()){
- String str =it.next();
- System.out.println(str);
- }
- }
- }
Collections排序
Collections类可以对集合进行排序、查找和替换操作
实现一个类的对象之间比较大小,该类要实现Comparable接口
重写compareTo()方法
- public class Student implements Comparable<Student> {
-
- private String name;
- private int stuId;
-
- public Student() {
- super();
- }
-
- public Student(String name, int stuId) {
- super();
- this.name = name;
- this.stuId = stuId;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public int getStuId() {
- return stuId;
- }
-
- public void setStuId(int stuId) {
- this.stuId = stuId;
- }
-
- @Override
- public String toString() {
- return "Student [name=" + name + ", stuId=" + stuId + "]";
- }
-
- @Override
- public int compareTo(Student student) {
- //比较此对象与指定对象的顺序。
- return this.stuId-student.stuId;
- }
-
- }
-
-
-
- import java.util.ArrayList;
- import java.util.Collections;
-
- public class StudentTest {
-
- public static void main(String[] args) {
- //创建4个Student类对象
- Student stu1 = new Student("张三", 1001);
- Student stu2 = new Student("李四", 1002);
- Student stu3 = new Student("王五", 1003);
- Student stu4 = new Student("赵六", 1004);
-
- //创建ArrayList集合对象
- ArrayList<Student> al = new ArrayList<Student>();
-
- al.add(stu3);
- al.add(stu2);
- al.add(stu4);
- al.add(stu1);
-
- System.out.println("集合排序前:");
- for (Student student : al) {
- System.out.println(student);
- }
-
- Collections.sort(al);
- System.out.println("集合排序后:");
- for (Student student : al) {
- System.out.println(student);
- }
- }
- }