• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

集合的运用,TreeSet排序方法, 泛型的运用

开发技术 开发技术 4小时前 2次浏览

import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

public class Employee implements Comparable<Employee>{

//私有属性

private String name;
private int age;
private MyDate birthday;
//get  set 方法
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;
}

//有参构造器
public Employee(String name, int age, MyDate birthday) {
super();
this.name = name;
this.age = age;
this.birthday = birthday;
}

//无参构造器
public Employee() {
super();
}

//toString方法
@Override
public String toString() {
return “Employee [name=” + name + “, age=” + age + “, birthday=” + birthday + “]”;
}
/**
* 泛型
*/
@Override
public int compareTo(Employee o) {

return this.name.compareTo(o.name);
}

public static void main(String[] args) {
//规定当前集合中只能使用一种数据类型
TreeSet<Employee> list =new TreeSet<>();
list.add(new Employee(“a”,20,new MyDate(1995,10,10)));
list.add(new Employee(“d”,20,new MyDate(1990,9,10)));
list.add(new Employee(“c”,20,new MyDate(1998,7,10)));
list.add(new Employee(“b”,20,new MyDate(1996,12,10)));
list.add(new Employee(“e”,20,new MyDate(1992,2,10)));
System.out.println(list);
}
}

//输出[Employee [name=a, age=20, birthday=MyDate [year=1995, month=10, day=10]], Employee [name=b, age=20, birthday=MyDate [year=1996, month=12, day=10]], Employee [name=c, age=20, birthday=MyDate [year=1998, month=7, day=10]], Employee [name=d, age=20, birthday=MyDate [year=1990, month=9, day=10]], Employee [name=e, age=20, birthday=MyDate [year=1992, month=2, day=10]]]

 

public class MyDate {
private int year;
private int month;
private int day;
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;
}
public MyDate(int year, int month, int day) {
super();
this.year = year;
this.month = month;
this.day = day;
}
public MyDate() {
super();
}
@Override
public String toString() {
return “MyDate [year=” + year + “, month=” + month + “, day=” + day + “]”;
}

}

 

import java.util.Comparator;
import java.util.Stack;
import java.util.TreeSet;

public class Test {
public static void main(String[] args) {
//泛型只能必须是引用数据类型
Comparator<Employee> c=new Comparator<Employee>() {
/*
* 按生日日期的先后排序
*/
@Override
public int compare(Employee o1, Employee o2) {
MyDate m1=o1.getBirthday();
MyDate m2=o2.getBirthday();
int a=m1.getYear()-m2.getYear();
if(a==0) {//判断如果a==0,那么年相等,接着对比月份
int b=m1.getMonth()-m2.getMonth();
if(b==0) {
return m1.getDay()-m2.getDay();
}
else {
return b;
}
}
else {
return a;
}

}
};

TreeSet<Employee> list =new TreeSet<>(c);
list.add(new Employee(“a”,20,new MyDate(1995,10,10)));
list.add(new Employee(“d”,20,new MyDate(1990,9,10)));
list.add(new Employee(“c”,20,new MyDate(1998,7,10)));
list.add(new Employee(“b”,20,new MyDate(1996,12,10)));
list.add(new Employee(“e”,20,new MyDate(1992,2,10)));
System.out.println(list);

//输出

[Employee [name=d, age=20, birthday=MyDate [year=1990, month=9, day=10]], Employee [name=e, age=20, birthday=MyDate [year=1992, month=2, day=10]], Employee [name=a, age=20, birthday=MyDate [year=1995, month=10, day=10]], Employee [name=b, age=20, birthday=MyDate [year=1996, month=12, day=10]], Employee [name=c, age=20, birthday=MyDate [year=1998, month=7, day=10]]]

//Stack的特点是什么?pop和peek的区别?
Stack<Integer> s=new Stack<Integer>();
s.push(1);//添加数值,先存1,1在下,从下往上装
s.push(2);
s.push(3);
System.out.println(s);
System.out.println(s.peek());//复制并取最上面的值
System.out.println(s.pop());//不复制直接取值
System.out.println(s);

//输出

[1, 2, 3]
3
3
[1, 2]


程序员灯塔
转载请注明原文链接:集合的运用,TreeSet排序方法, 泛型的运用
喜欢 (0)