java集合中对象排序
概述:本示例实现对象按年龄升序 人气升序排序功能 姓名升序 降序排序功能
package ch02;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* @author YaoShiyou 实现对象排序
*
*/
public class Person {
// 姓名
private String name;
// 年龄
private int age;
// 人气
private int hobby;
public Person(String name) {
this.name = name;
}
public Person(String name, int age) {
this(name);
this.age = age;
}
public Person(String name, int age, int hobby) {
this(name, age);
this.hobby = hobby;
}
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 int getHobby() {
return hobby;
}
public void setHobby(int hobby) {
this.hobby = hobby;
}
public static void main(String[] args) {
List<Person> list = new ArrayList<Person>();
list.add(new Person("g1", 18, 122));
list.add(new Person("g2", 17, 244));
list.add(new Person("g3", 45, 243));
list.add(new Person("g4", 9, 67));
list.add(new Person("g5", 98, 2));
System.out.println("排序前---");
for (Person person : list) {
System.out.println(person.getName() + "\t" + person.getAge() + "\t"
+ person.getHobby());
}
// Collections调用sort 方法 参数为
Collections.sort(list, new AgeComparatorAsc());
System.out.println("年龄排序后----");
for (Person person : list) {
相关文档:
List的用法
List包括List接口以及List接口的所有实现类。因为List接口实现了Collection接口,所以List接口拥有Collection接口提供的所有常用方法,又因为List是列表类型,所以List接口还提供了一些适合于自身的常用方法,如表1所示。
表1 List接口定义的常用方法及功能
从表1可以看出,List接口提供的适合于自身的 ......
1,OCP(Open-Close Principle)开闭原则
Software entities should be open for extension,but closed for modification,(在设计一个模块的时候,应当使这个模块可以在不被修改的前提下扩展)。
&nbs ......
关于绝对路径和相对路径:
绝对路径就是你的主页上的文件或目录在硬盘上真正的路径,(URL和物理路径)例如:C:xyz est.txt 代表了test.txt文件的绝对路径。http://www.sun.com/index.htm也代表了一个URL绝对路径。相对路径:相对与某个基准目录的路径。包含Web的相对路径(HTML中的相对目录),例如:在Servlet中,"/"代表W ......
1.[http://www.javaalmanac.com] Java开发者年鉴一书的在线版本. 要想快速查到某种Java技巧的用法及示例代码, 这是一个不错的去处.
2.[http://www.onjava.com] OReilly的Java网站. 每周都有新文章.
3.[http://java.sun.com] 官方的Java开发者网站 每周都有新文章发表.
4.[http://www.developer.com/java] ......