java Collecions_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java Collecions

java Collecions

 2013/12/17 19:09:13  zcxwh353631531  程序员俱乐部  我要评论(0)
  • 摘要:packagetest;importjava.util.ArrayList;importjava.util.Collections;importjava.util.Comparator;importjava.util.List;publicclassTest{publicstaticvoidmain(String[]args){List<People>list=newArrayList<People>();Peoplepeople1=newPeople()
  • 标签:Java
package test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Test {
   public static void main(String[] args) {
   List<People> list = new ArrayList<People>();
   People people1 = new People();
   people1.setName("cep_20121212080912");
   people1.setAge(12);
  
   People people2 = new People();
   people2.setName("cep_20131212081912");
   people2.setAge(8);
  
   People people3 = new People();
   people3.setName("cep_20121212081112");
   people3.setAge(45);
   list.add(people1);
   list.add(people2);
   list.add(people3);
   Collections.sort(list, new Comparator<People>() {

@Override
public int compare(People o1, People o2) {
// TODO Auto-generated method stub
return o1.getName().compareTo(o2.getName());
}
});
   for(People p : list){
   System.out.println(p.getName() +" " +p.getAge());
   }
   }
}

class People{
public String name;
public 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;
}
}
发表评论
用户名: 匿名