java Collections对数组排序
2016-08-03 13:14
337 查看
package org.geekworld.webTest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
/**
* Hello world!
*
*/
public class App
{
@SuppressWarnings("unchecked")
public static void main( String[] args )
{
ArrayList<Person> arrayList = new ArrayList<Person>();
for (int i = 0; i < 10; i++) {
arrayList.add(new Person("name"+i, 100-i));
}
Comparator<Person> arg1=new Comparator<Person>() {
@Override
public int compare(Person arg0, Person arg1) {
if(arg0!=null && arg1!=null){
if(arg0.getAge()>arg1.getAge()){
System.out.println("大......");
return 1;//TODO 从大到小排序的话返回-1
}else if(arg0.getAge()<arg1.getAge()){
System.out.println("小......");
return -1;//TODO 从大到小排序的话返回1
}
}
//TODO 维持原样不变
return 0;
}
};
Collections.sort(arrayList, arg1);
for (Person person : arrayList) {
System.out.println(person.getName()+"/"+person.getAge());
}
//Decending sorted iterator
String[] strs1 = new String[]{"3","2","5","6","2"};
//倒序
// Collections.sort(Arrays.asList(strs1),Collections.reverseOrder());
//顺序
Collections.sort(Arrays.asList(strs1));
for (int i = 0; i < strs1.length; i++) {
System.out.println(strs1[i]);
}
}
private static class Person{
private String name;
private int age;
public Person() {
super();
}
public Person(String name, int age) {
super();
this.name = name;
this.age = 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;
}
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
/**
* Hello world!
*
*/
public class App
{
@SuppressWarnings("unchecked")
public static void main( String[] args )
{
ArrayList<Person> arrayList = new ArrayList<Person>();
for (int i = 0; i < 10; i++) {
arrayList.add(new Person("name"+i, 100-i));
}
Comparator<Person> arg1=new Comparator<Person>() {
@Override
public int compare(Person arg0, Person arg1) {
if(arg0!=null && arg1!=null){
if(arg0.getAge()>arg1.getAge()){
System.out.println("大......");
return 1;//TODO 从大到小排序的话返回-1
}else if(arg0.getAge()<arg1.getAge()){
System.out.println("小......");
return -1;//TODO 从大到小排序的话返回1
}
}
//TODO 维持原样不变
return 0;
}
};
Collections.sort(arrayList, arg1);
for (Person person : arrayList) {
System.out.println(person.getName()+"/"+person.getAge());
}
//Decending sorted iterator
String[] strs1 = new String[]{"3","2","5","6","2"};
//倒序
// Collections.sort(Arrays.asList(strs1),Collections.reverseOrder());
//顺序
Collections.sort(Arrays.asList(strs1));
for (int i = 0; i < strs1.length; i++) {
System.out.println(strs1[i]);
}
}
private static class Person{
private String name;
private int age;
public Person() {
super();
}
public Person(String name, int age) {
super();
this.name = name;
this.age = 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;
}
}
}
相关文章推荐
- 根据所选的year 和 month 获得所有的周开始与结束
- springmvc 文件上传到服务器的固定目录中
- Springmvc 页面重定向
- spring-junit中使用 log4j或者logback 打印spring日志
- 数组a,数组b,复制到数组c
- Java数据类型及对应取值范围【速查】
- spring 中使用 logback打印日志,替换其他日志如log4j,commons-logging
- java的变量
- eclipse运行速度优化(解决狂读盘、发布慢、CPU100%等问题)
- jdk内置工具列表
- Java语法的细节
- JDK8的安装及win10配置JDK8的环境变量
- 如何在 Eclipse 中使用命令行
- 78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
- Eclipse输入命令行参数
- Java接口详解
- 解释:i=i++
- Java获取请求客户端的真实IP地址
- java 获取IP地址的方法
- Java修饰符