向TreeMap集合和ArrayList集合当中进行自定义对象的添加并对其排序
public class Student implements Comparable<Student>{
public Student(String name,int age) {
// TODO Auto-generated constructor stub
this.name=name;
this.age=age;
}
private String name;
private int age;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
@Override
public boolean equals(Object obj) {
//对当前Student对象当中的方法进行重写,将要进行比较的object对象先强制转变为student对象
Student stu=(Student)obj;
// 另当前对象当中的指定属性和参数对象当中的指定 属性值进行比较
boolean flag=this.name.equals(stu.getName());
if(flag)
{
return true;
}
else
return false;
}
@Override
public int hashCode() {
if(this.name!=null)
{
return name.hashCode();
}
else
return super.hashCode();
}
/**
* 该方法进行比较之后存放到集合当中的元素是升序,要想倒序排序的话只要将+1和-1的位置进行互换即可
* public int compareTo(Student student)
{
//用于将当前的student对象和参数对象进行比较操作,定义两个学生对象的比较规则
//1.根据年龄来对两个学生对象的年龄大小进行比较
if(this.age==student.age)
{
return 0;
}
else
{
if(this.age>student.age)
return 1;
else
return -1;
}
}
*/
@Override
public int compareTo(Student student)
{
//用于将当前的student对象和参数对象进行比较操作,定义两个学生对象的比较规则
//1.根据年龄来对两个学生对象的年龄大小进行比较
if(this.age==student.age)
{
// 2.如果当前的两个学生对象的年龄相同的时候则对名字进行比较
if(this.name.equals(student.name))
{
return 0;
}
else
{
if(this.name.compareTo(student.name)>0)
{
// 表名当前对象当中的名字要比参数对象的名字值大
return 1;
}
else
return -1;
}
}
else
{
if(this.age>student.age)
return 1;
else
return -1;
}
}
@Override
public String toString() {
String str="studentName:"+name+" studentAge:"+age;
return str;
}
}
package collection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
public class TreeMapTest {
public static void main(String[] args) {
Student student1=new Student("bai",18);
Student student2=new Student("zhou",19);
Student student3=new Student("zhang",20);
Student student4=new Student("an",21);
Student student5=new Student("yang",22);
Student student6=new Student("qing",18);
/**
* TreeSet集合对象会对存放在集合对象当中的数据进行排序操作
* 在使用TreeSet集合对象来将student对象添加到集合对象当中的时候将会有异常抛出
* 因为TreeSet集合在将一个对象添加到集合当中的时候将会调用该对象当中comparableTo方法来对当前的对象进行比较和排序操作
* 但是在当前的student学生对象当中是没有comparable方法的,所以在将student对象添加到集合对象当中的时候将会有异常抛出
*/
Set<Student> students=new TreeSet<Student>();
students.add(student1);
students.add(student2);
students.add(student3);
students.add(student4);
students.add(student5);
students.add(student6);
students.add(student1);
/**
* 对TreeSet集合对象当中不能够进行对象存放的解决:
* 由于对象当中没有student对象,所以要另student对象实现comparable接口,然后对该接口当中的方法进行重写操作
* 此时TreeSet集合对象当中就可以添加student对象,但是由于默认的comparableTo方法的默认的返回值为0,所以所存入到TreeSet集合当中的对象是无序的
* 要想使得存入到集合当中的对象是有序的,就必须要对student对象当中comparableTo方法进行重写来定义排序规则:
* 另存入到集合当中的student对象按照年龄来进行排序,当年龄相同的时候来按照名字进行排序
*
* 在TreeSet集合对象当中对重复性的元素对象只存放一次
*/
Iterator<Student> iterator=students.iterator();
while(iterator.hasNext())
{
System.out.println(iterator.next());
}
System.out.println("使用List来对集合对象当中的元素进行遍历:");
List<Student> list=new ArrayList<Student>();
list.add(student1);
list.add(student2);
list.add(student3);
list.add(student4);
list.add(student5);
list.add(student6);
list.add(student1);
Iterator<Student> iterator2=list.iterator();
/**
* 通过遍历可以发现在list集合当中是可以存放无序的对象的
* 在list集合对象当中所存放的数据都是无序
*/
while(iterator2.hasNext())
{
System.out.println(iterator2.next());
}
/**
* 调用Collections.sort方法来对list集合当中的数据进行排序操作
* 该方法将会将list参数集合对象转变为一个数组对象
* 然后调用Arrays.sort(a);来对数组对象进行排序操作
* 将排序好之后的数据重新设置到集合对象当中
*/
Collections.sort(list);
// 将重新进行排序好之后的集合对象添加到迭代器对象当中然后进行遍历输出
Iterator<Student> iterator3=list.iterator();
System.out.println("对排序好之后的list集合对象当中的数据进行遍历:");
while(iterator3.hasNext())
{
System.out.println(iterator3.next());
}
}
}