关键词:
发现泛型用起来好方便!!!!!!!😐 😐
集合中使用泛型总结:
① 集合接口或集合类在jdk5.0时都修改为带泛型的结构。
② 在实例化集合类时,可以指明具体的泛型类型
③ 指明完以后,在集合类或接口中凡是定义类或接口时,内部结构(比如:方法、构造器、属性等)使用到类的泛型的位置,都指定为实例化的泛型类型。
比如:add(E e) —>实例化以后:add(Integer e)
④ 注意点:泛型的类型必须是类,不能是基本数据类型。需要用到基本数据类型的位置,拿包装类替换
⑤ 如果实例化时,没指明泛型的类型。默认类型为java.lang.Object类型。
对这个博客:【Java自用】集合练习题:TreeSet的自然排序与定制排序 里面的练习题进行泛型操作。
题目描述:涉及自然排序和定制排序
定义一个 Employee 类。
该类包含:private 成员变量 name,age,birthday,其中 birthday 为MyDate 类的对象;并为每一个属性定义 getter, setter 方法;并重写 toString 方法输出 name, age, birthday。
MyDate 类包含: private 成员变量 year,month,day;并为每一个属性定义 getter, setter 方法;
创建该类的 5 个对象,并把这些对象放入 TreeSet 集合中
分别按以下两种方式对集合中的元素进行排序,并遍历输出:
1). 使 Employee 实现 Comparable 接口,并按 name 排序
2). 创建 TreeSet 时传入 Comparator 对象,按生日日期的先后排序。
代码如下:
Employee.java
public class Employee implements Comparable<Employee>
private String name;
private int age;
private MyDate birthday;
public Employee(String name, int age, MyDate birthday)
this.name = name;
this.age = age;
this.birthday = birthday;
public Employee()
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;
public MyDate getBirthday()
return birthday;
public void setBirthday(MyDate birthday)
this.birthday = birthday;
@Override
public String toString()
return "Employee" +
"name='" + name + '\\'' +
", age=" + age +
", birthday=" + birthday +
'';
// 按 name 排序 指明泛型时的写法
@Override
public int compareTo(Employee o)
//不用判断o是否为Employee了也不用强转了
return this.name.compareTo(o.name);
//没有指明泛型时的写法
//按 name 排序
// @Override
// public int compareTo(Object o)
// if(o instanceof Employee)
// Employee e = (Employee)o;
// return this.name.compareTo(e.name);
//
return 0;
// throw new RuntimeException("传入的数据类型不一致!");
//
MyDate.java
public class MyDate implements Comparable<MyDate>
private int year;
private int month;
private int day;
public MyDate(int year, int month, int day)
this.year = year;
this.month = month;
this.day = day;
public MyDate()
public int getYear()
return year;
public void setYear(int year)
this.year = year;
public int getMonth()
return month;
public void setMonth(int month)
this.month = month;
public int getDay()
return day;
public void setDay(int day)
this.day = day;
@Override
public String toString()
return "MyDate" +
"year=" + year +
", month=" + month +
", day=" + day +
'';
//使用泛型之前=====================
// @Override
// public int compareTo(Object o)
// if(o instanceof MyDate)
// MyDate m = (MyDate)o;
//
// //比较年
// int minusYear = this.getYear() - m.getYear();
// if(minusYear != 0)
// return minusYear;
//
// //比较月
// int minusMonth = this.getMonth() - m.getMonth();
// if(minusMonth != 0)
// return minusMonth;
//
// //比较日
// return this.getDay() - m.getDay();
//
//
// throw new RuntimeException("传入的数据类型不一致!");
//
//
//使用泛型之后============================
@Override
public int compareTo(MyDate m)
//比较年
int minusYear = this.getYear() - m.getYear();
if(minusYear != 0)
return minusYear;
//比较月
int minusMonth = this.getMonth() - m.getMonth();
if(minusMonth != 0)
return minusMonth;
//比较日
return this.getDay() - m.getDay();
EmployeeTest.java (测试)
public class EmployeeTest
//问题二:按生日日期的先后排序-------定制排序
@Test
public void test2()
//jdk7新特性:类型推断,后面指定类可以省略, 原来:TreeSet<Employee> set = new TreeSet<Employee>();
TreeSet<Employee> set = new TreeSet<>(new Comparator<Employee>()
//使用泛型以后的写法 o1,o2为Employee 不用进行instanceOf判断和强转了!!!
@Override
public int compare(Employee o1, Employee o2)
MyDate b1 = o1.getBirthday();
MyDate b2 = o2.getBirthday();
return b1.compareTo(b2);
//使用泛型之前的写法=======================================
//@Override
// public int compare(Object o1, Object o2)
// if(o1 instanceof Employee && o2 instanceof Employee)
// Employee e1 = (Employee)o1;
// Employee e2 = (Employee)o2;
//
// MyDate b1 = e1.getBirthday();
// MyDate b2 = e2.getBirthday();
// //方式一:
//比较年
int minusYear = b1.getYear() - b2.getYear();
if(minusYear != 0)
return minusYear;
//比较月
int minusMonth = b1.getMonth() - b2.getMonth();
if(minusMonth != 0)
return minusMonth;
//比较日
return b1.getDay() - b2.getDay();
//
// //方式二:
// return b1.compareTo(b2);
//
return 0;
// throw new RuntimeException("传入的数据类型不一致!");
//
);
Employee e1 = new Employee("liudehua",55,new MyDate(1965,5,4));
Employee e2 = new Employee("zhangxueyou",43,new MyDate(1987,5,4));
Employee e3 = new Employee("guofucheng",44,new MyDate(1987,5,9));
Employee e4 = new Employee("liming",51,new MyDate(1954,8,12));
Employee e5 = new Employee("liangzhaowei",21,new MyDate(1978,12,4));
set.add(e1);
set.add(e2);
set.add(e3);
set.add(e4);
set.add(e5);
Iterator<Employee> iterator = set.iterator();
while (iterator.hasNext())
Employee employee = iterator.next();
System.out.println(employee);
//问题一:使用自然排序
@Test
public void test1()
//指明泛型-----------
TreeSet<Employee> set = new TreeSet<Employee>();
Employee e1 = new Employee("liudehua",55,new MyDate(1965,5,4));
Employee e2 = new Employee("zhangxueyou",43,new MyDate(1987,5,4));
Employee e3 = new Employee("guofucheng",44,new MyDate(1987,5,9));
Employee e4 = new Employee("liming",51,new MyDate(1954,8,12));
Employee e5 = new Employee("liangzhaowei",21,new MyDate(1978,12,4));
set.add(e1);
set.add(e2);
set.add(e3);
set.add(e4);
set.add(e5);
Iterator<Employee> iterator = set.iterator();
while (iterator.hasNext())
Employee employee = iterator.next();
System.out.println(employee);
如何遍历Map的key集,value集,key-value集,使用上泛型
Map<String,Integer> map = new HashMap<String,Integer>();
map.put();....
//遍历key
Set<String> keySet = map.keySet();
for(String key : keySet)
System.out.println(key);
//遍历value
Collection<Integer> values = map.values();
Iterator<Integer> iterator = values.iterator();
while(iterator.hasNext())
System.out.println(iterator.next());
//遍历key-value
Set<Map.Entry<String,Integer>> entrySet = map.entrySet();
Iterator<Map.Entry<String,Integer>> iterator = entrySet.iterator();
while(iterator.hasNext())
Map.Entry<String,Integer> entry = iterator.next();
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key + "--->" + value);
提供一个方法,用于遍历获取HashMap<String,String>中的所有value,并存放在List中返回。考虑上集合中泛型的使用。
public List<String> getValueList(HashMap<String,String> map)
ArrayList<String> valueList = new ArrayList<>():
Collection<String> values = map.values();
for(String value : values) //增强for循环
valueList.add(value);
return valueList;
java自用练习题-自定义泛型类的使用(代码片段)
题目描述:1.定义个泛型类DAO<T>,在其中定义一个Map成员变量,Map的键为String类型,值为T类型。分别创建以下方法:publicvoidsave(Stringid,Tentity):保存T类型的对象到Map成员变量中publicTget(Stringid):从ma... 查看详情
初识java集合及包装类和泛型的基本使用(代码片段)
文章目录一、介绍1.java集合框架是什么?2.学习java集合的意义3.java集合关系图二、接口使用实例1.Collection接口说明2.Collection示例3.Map接口说明4.Map实例三、泛型1.泛型的分类2.泛型的定义3.泛型背后作用时期和背后的简单原理4.... 查看详情
泛型的引入(代码片段)
JDK5.0新特性——泛型集合可以存储任何类型的对象,但是当把一个对象存入集合后,集合会"忘记"这个对象的类型,将该对象从集合中取出,这个对象的编译类型就变成了Object类型。为了解决这个问题,在Java中引入了"参数... 查看详情
java泛型的读写规则:pecs(代码片段)
...的重要用法。PECS就是当你需要遍历某一个类型和子类的集合数据时,集合相当于生产者,此时泛型使用<?extendsT>。当需要往某个类型的集合添加类和子类实例时,集合相当于消费者,此时泛型使用<?superT>... 查看详情
java泛型的读写规则:pecs(代码片段)
...的重要用法。PECS就是当你需要遍历某一个类型和子类的集合数据时,集合相当于生产者,此时泛型使用<?extendsT>。当需要往某个类型的集合添加类和子类实例时,集合相当于消费者,此时泛型使用<?superT>... 查看详情
java泛型的读写规则:pecs(代码片段)
...的重要用法。PECS就是当你需要遍历某一个类型和子类的集合数据时,集合相当于生产者,此时泛型使用<?extendsT>。当需要往某个类型的集合添加类和子类实例时,集合相当于消费者,此时泛型使用<?superT>... 查看详情
java-基础(集合泛型)(代码片段)
JAVA-基础(集合泛型)1.什么是泛型?可以在类或方法中预支地使用未知的类型。2.泛型的好处?将运行时期的ClassCastException,转移到了编译时期变成了编译失败。避免了类型强转的麻烦。(集合中是可以存放任意对象的,只要把... 查看详情
java泛型和内部类(代码片段)
...内部类4.匿名内部类一、泛型的概述1.概念我们都用过Java集合类,比如Arra 查看详情
java泛型(代码片段)
1.了解未使用泛型的集合→集合可以装任意类型的数据,但是接收数据的时候很麻烦,因为你不知道你会收到什么类型的数据,时刻需要准备强转。使用了泛型的集合→编译器会检测插入的数据类型,集合只能装... 查看详情
java——泛型(代码片段)
文章目录Java中泛型的概述Java中泛型的语法规则泛型集合泛型方法泛型方法示例(代码演示)小结泛型类泛型类示例(代码演示)小结泛型接口泛型接口示例(代码演示)高级泛型总结Java中泛型的概述泛型... 查看详情
java自用集合练习题:treeset的自然排序与定制排序(代码片段)
定义一个Employee类。该类包含:private成员变量name,age,birthday,其中birthday为MyDate类的对象;并为每一个属性定义getter,setter方法;并重写toString方法输出name,age,birthday。MyDate类包含:private成员变量year,month,day;并 查看详情
java_泛型笔记(代码片段)
...法Java_泛型在继承上的体现Java_通配符的使用Java_泛型简介集合容器类在设计阶段/声明阶段不能确定这个容器到底实际存的是什么类型的对象在JDK1.5之前只能把元素类型设计为Object,JDK1.5之后使用泛 查看详情
java泛型简介定义和使用含有泛型的类定义和使用含有泛型的方法定义和使用含有泛型的接口泛型通配符?受限泛型(代码片段)
泛型简介:集合中存储数据的类型称之为泛型,泛型是一种未知的数据类型,当不知道使用什么数据类型的时候可以使用泛型;当创建对象时在指明数据类型并赋值给泛型即可;泛型也可以看做是一个变量ÿ... 查看详情
集合类和java多线程(代码片段)
#知识点总结第五篇 1.泛型类就是带有参数类型的类,其类中也有属性和方法 数据类型可以实已有的类型,也可以是"类型参数"的类型。 2.泛型的好处 1.强制类型检查,在编译的时候就能得到类型错误的... 查看详情
javase集合框架——泛型(代码片段)
目录一、为什么要有泛型二、在集合中使用泛型2.1集合使用泛型2.2Comparable类和Comparator比较器使用泛型 1)Comprable类使用泛型2)Comparator使用泛型三、自定义泛型(泛型类、泛型接口;泛型方法)3.1泛型类/泛型... 查看详情
浅显理解java泛型的super和extends(代码片段)
...方面返回值方面总结概念简单理解List<?extendsT>表示该集合中存在的都是类型T的子类,包括T自己List<?superT>表示该集合中存的都是类型T的父类,包括T自己代码样例解读父子类代码 查看详情
java中泛型的深入理解(代码片段)
...;<数据类型>;注意:泛型只能支持引用数据类型。集合体系的全部接口和实现类都是支持泛型的使用的。泛型的好处:统一数据类型。把运行时期的问题提前到了编译期间,避免了强制类型转换可能出现的异常,因... 查看详情
java重点--泛型(代码片段)
文章目录💖泛型的概念✨使用泛型的好处与弊端✨定义和使用含有泛型的类✨定义和使用含有泛型的方法✨定义和使用含有泛型的接口✨泛型的通配符✨通配符的高级使用--受限泛型✨斗地主小案例💖泛型的概念泛型是... 查看详情