java中排序使用方法
来源:互联网 发布:打的费用计算软件 编辑:程序博客网 时间:2024/05/20 07:49
package model;import java.text.SimpleDateFormat;import java.util.Date;import org.apache.commons.lang.StringUtils;import org.apache.commons.lang.builder.ToStringBuilder;public class Person { private String name; private int age; private double salary; private Date birthday; public Person() { } /** * @param name * @param age * @param salary * @param birthday */ public Person(String name, int age, double salary, Date birthday) { this.name = name; this.age = age; this.salary = salary; this.birthday = birthday; } /** * @param name * @param age * @param salary */ public Person(String name, int age, double salary) { this.name = name; this.age = age; this.salary = salary; } /** * @param name * @param age */ public Person(String name, int age) { this.name = name; this.age = age; } /** * Getter method for property <tt>name</tt>. * * @return property value of name */ public String getName() { return name; } /** * Setter method for property <tt>name</tt>. * * @param name value to be assigned to property name */ public void setName(String name) { this.name = name; } /** * Getter method for property <tt>age</tt>. * * @return property value of age */ public int getAge() { return age; } /** * Setter method for property <tt>age</tt>. * * @param age value to be assigned to property age */ public void setAge(int age) { this.age = age; } /** * Getter method for property <tt>salary</tt>. * * @return property value of salary */ public double getSalary() { return salary; } /** * Setter method for property <tt>salary</tt>. * * @param salary value to be assigned to property salary */ public void setSalary(double salary) { this.salary = salary; } /** * Getter method for property <tt>birthday</tt>. * * @return property value of birthday */ public Date getBirthday() { return birthday; } /** * Setter method for property <tt>birthday</tt>. * * @param birthday value to be assigned to property birthday */ public void setBirthday(Date birthday) { this.birthday = birthday; } /** * @see java.lang.Object#toString() */ @Override public String toString() { SimpleDateFormat format=new SimpleDateFormat("YYYY-MM-dd"); String finalBirthday=""; if(this.getBirthday()!=null){ finalBirthday=format.format(birthday); } return "Person [name=" + name + ", age=" + age + ", salary=" + salary + ", birthday=" + finalBirthday + "]"; } }
package sort;import java.util.Comparator;import java.util.Date;import java.util.List;import java.util.stream.Collectors;import com.google.common.collect.Lists;import model.Person;public class PersonSort { private void initPerson(List<Person> persons) { Person p = new Person("whangsan", 12, 12.5, new Date(2017, 11, 12)); Person p1 = new Person("zhangsan", 13, 12.5, new Date(2017, 11, 13)); Person p2 = new Person("zhangsan", 13, 13.5, new Date(2017, 10, 12)); Person p3 = new Person("zhangsan", 13, 13.5, new Date(2017, 10, 9)); Person p4 = new Person("zhangsan", 11, 10.5, new Date(2017, 12, 11)); Person p5 = new Person("zhangsan", 11, 10.5); persons.add(p); persons.add(p1); persons.add(p2); persons.add(p3); persons.add(p4); persons.add(p5); } private void print(List<Person> persons) { for (Person p : persons) { System.out.println(p); } } public static Comparator<Person> sort() { Comparator<Person> sortByName = ((o1, o2) -> o1.getName().compareTo(o2.getName())); Comparator<Person> sortByAge = ((o1, o2) -> Integer.compare(o1.getAge(), o2.getAge())); Comparator<Person> sortBySalary = ((o1, o2) -> Double.compare(o1.getSalary(), o2.getSalary())); Comparator<Person> sortByDate = ((o1, o2) -> o1.getBirthday().compareTo(o2.getBirthday())); Comparator<Person> sortByBirthday = ((o1, o2) -> Comparator.nullsFirst( Comparator.<Date> reverseOrder()).compare(o1.getBirthday(), o2.getBirthday())); return sortByName.thenComparing(sortByAge.reversed()).thenComparing(sortBySalary) .thenComparing(sortByBirthday); } /** * * @param args */ public static void main(String[] args) { PersonSort personSort = new PersonSort(); List<Person> data = Lists.newArrayList(); List<Person> newdata = Lists.newArrayList(); personSort.initPerson(data); personSort.print(data); System.out.println("------"); newdata = data.stream().sorted(sort()).collect(Collectors.toList()); personSort.print(newdata); System.out.println("------"); personSort.print(data); }}
阅读全文
0 0
- java中排序使用方法
- Java集合类中Sort排序的使用方法
- java中Volatile使用方法
- Java中finalize使用方法
- JAVA中enum使用方法
- Java 中 this 的使用方法
- java中cookie的使用方法
- java中static的使用方法
- java中memcached基本使用方法
- java中Scanner的使用方法
- Java中枚举的使用方法
- java中sort的使用方法
- JAVA中trim()函数使用方法
- Java中this的使用方法
- java 中数组的使用方法
- java中static的使用方法。
- java中final的使用方法
- Java中JNI的使用方法
- 【Scikit-Learn 中文文档 】安装 scikit-learn | ApacheCN
- 欢迎使用CSDN-markdown编辑器
- WebView或浏览器通过Scheme调起App
- View编程指南2—Views
- centos7通过yum安装JDK1.8
- java中排序使用方法
- Cassandra NoSQL数据模型设计指南
- Hive日期时间函数学习
- scrollTop:在JavaScript中获取当前页面的滚动位置
- linux中的vi设置
- SpringMVC前台传递数组,后台使用list接收
- 【示例教程】如何通过leadtools读取二维码中的中文
- View编程指南1—Windows 和 Views 简介
- 77秒小程序系统开发详解