public int compareTo(Object o) {
//将此对象与指定的对象进行比较以进行排序。
//返回一个负整数,零或正整数,因为该对象小于,等于或大于指定对象。
Student stu=(Student)o;
int i=0;
if(this.age==stu.age)
{i=0;}
if(this.age>stu.age)
{i=1;}
if(this.age
set可以放一个null,但是不能够放入重复的元素
Address.java
/**
*
* @author FDL
*
*/
public class Address {
private String name;
private String street;
private String city;
private String province;
private String country;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Override
public String toString() {
return "[\n姓名:" + name + "\n街道:" + street + "\n市(县):" + city
+ "\n省(自治区):" + province + "\n国家(地区):" + country + "\n]";
}
public Address(String name, String street, String city, String province,
String country) {
super();
this.name = name;
this.street = street;
this.city = city;
this.province = province;
this.country = country;
}
public Address() {
super();
}
}
TestAddress.java
import java.util.ArrayList;
import java.util.List;
public class TestAddress {
public static void main(String[] args) {
List addressList =new ArrayList();
Address a1=new Address("张三", "王府井", "北京", "北京", "中国");
Address a2=new Address("李四", "张扬路", "上海", "上海", "中国");
Address a3=new Address("John lenon", "12 Park Avenue", "California", "CaliforniaState", "USA");
addressList.add(a1);
addressList.add(a2);
addressList.add(a3);
for (Address address : addressList) {
System.out.println(address);
}
}
}
运行结果:
2.构建描述学生的类Student, 属性:姓名和年龄
构建集合来存放学生对象信息,但要保证学生的年龄按大小顺序进行排序
最后遍历输出格式如下图所示
提示:
Student2.java
public class Student2 implements java.lang.Comparable{
private String name;
private int age;
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 Student2(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Student2() {
super();
}
@Override
public String toString() {
return " " + name + " " + age + " ";
}
@Override
public int compareTo(Object o) {
//将此对象与指定的对象进行比较以进行排序。
//返回一个负整数,零或正整数,因为该对象小于,等于或大于指定对象。
//基础是二叉树
Student2 stu=(Student2)o;
//由大到小排列
int i=0;
if(this.age==stu.age) //不处理
{
i=0;// 相等 不处理
}
if(this.agestu.age)
{
i=-1;//大于 往左放
}
return i;
}
}
TestStudent2.java
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
public class TestStudent2 {
public static void main(String[] args) {
TreeSet tree = new TreeSet();
tree.add(new Student2("Linda", 21));
tree.add(new Student2("Mary", 28));
tree.add(new Student2("John", 26));
tree.add(new Student2("Tom", 25));
tree.add(new Student2("Jack", 24));
tree.add(new Student2("Lisa", 22));
show(tree);
}
public static void show(Set set) {
System.out.println("---------------------------");
System.out.println(" 姓名年龄");
for (Student2 s : set) {
System.out.println("---------------------------");
System.out.println(s);
}
System.out.println("---------------------------");
}
}