collections集合操作排序,Java8 stream集合操作

/**
 * 
 */
package com.wen;

import java.math.BigDecimal;

/**
 * @author wencun
 * @date 2018年2月22日 上午9:49:37
 * @version 1.0
 * @since JDK 1.8
 */
public class Staff {
    private String name;
    private int age;
    private BigDecimal salary;
  
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * @return the age
     */
    public int getAge() {
        return age;
    }
    /**
     * @param age the age to set
     */
    public void setAge(int age) {
        this.age = age;
    }
    /**
     * @return the salary
     */
    public BigDecimal getSalary() {
        return salary;
    }
    /**
     * @param salary the salary to set
     */
    public void setSalary(BigDecimal salary) {
        this.salary = salary;
    }
    /**
     * @param name
     * @param age
     * @param salary
     */
    public Staff(String name, int age, BigDecimal salary) {
        super();
        this.name = name;
        this.age = age;
        this.salary = salary;
    }
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Staff [name=" + name + ", age=" + age + ", salary=" + salary + "]";
    }
    
    
    
    
    
}
package com.wen;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
/**
 * @author wencun
 * @date 2018年2月22日 上午9:48:01
 * @version 1.0
 * @since JDK 1.8
 */

import org.junit.Test;
public class TestJava8 {
          
    @Test
    public void java8() throws Exception {
        List<Staff> asList = Arrays.asList(new Staff("小明", 18, new BigDecimal(2000)),
                new Staff("小明1", 33, new BigDecimal(22020)),
                new Staff("小明2", 44, new BigDecimal(2800)),
                new Staff("小明3", 23, new BigDecimal(2700)),
                new Staff("小明4", 21, new BigDecimal(2600)),
                new Staff("小明5", 10, new BigDecimal(2000)),
                new Staff("小明6", 56, new BigDecimal(21000)),
                new Staff("小明7", 49, new BigDecimal(12000)));
        
        List<Integer> list=Arrays.asList(3,41,23,65,12,77,33, 1,4);
        Collections.sort(list);
        System.out.println("顺序"+list);                     
        Collections.reverse(list);
        System.out.println("倒序"+list);
        Collections.shuffle(list);
        System.out.println("混序"+list); 
        
        List<Integer> collect = asList.stream().map(e->e.getAge()).collect(Collectors.toList());
        System.out.println("年龄集合"+collect);
        
        List<BigDecimal> collect2 = asList.stream().map(Staff::getSalary).collect(Collectors.toList());
        System.err.println("工资集合"+collect2);
        
        
        asList.sort(new Comparator<Staff>() {

            @Override
            public int compare(Staff o1, Staff o2) {
               
                return o1.getAge()-o2.getAge();
            }
        });
        System.out.println("对象集合按年龄排序"+asList);
        Collections.sort(collect);
        System.out.println("年龄排序"+collect);        
        
        
    }

}
结果:
顺序[1, 3, 4, 12, 23, 33, 41, 65, 77]
倒序[77, 65, 41, 33, 23, 12, 4, 3, 1]
混序[65, 77, 1, 3, 33, 23, 12, 4, 41]
年龄集合[18, 33, 44, 23, 21, 10, 56, 49]
工资集合[2000, 22020, 2800, 2700, 2600, 2000, 21000, 12000]
对象集合按年龄排序[Staff [name=小明5, age=10, salary=2000], Staff [name=小明, age=18, salary=2000], Staff [name=小明4, age=21, salary=2600], Staff [name=小明3, age=23, salary=2700], Staff [name=小明1, age=33, salary=22020], Staff [name=小明2, age=44, salary=2800], Staff [name=小明7, age=49, salary=12000], Staff [name=小明6, age=56, salary=21000]]
年龄排序[10, 18, 21, 23, 33, 44, 49, 56]


 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值