发现泛型用起来好方便!!!!!!!😐 😐
集合中使用泛型总结:
① 集合接口或集合类在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 +
'}';
}
@Override
public int compareTo(Employee o) {
return this.name.compareTo(o.name);
}
}
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(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(){
TreeSet<Employee> set = new TreeSet<>(new Comparator<Employee>() {
@Override
public int compare(Employee o1, Employee o2) {
MyDate b1 = o1.getBirthday();
MyDate b2 = o2.getBirthday();
return b1.compareTo(b2);
}
});
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();....
Set<String> keySet = map.keySet();
for(String key : keySet){
System.out.println(key);
}
Collection<Integer> values = map.values();
Iterator<Integer> iterator = values.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
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){
valueList.add(value);
}
return valueList;
}
|