如何使用java 8流对对象进行排序

时间:2018-02-02 15:27:45

标签: java java-stream

我在java 8中使用流API来处理我的集合。但是,我想知道使用此API以给定顺序对对象进行排序的优雅方法。

import UIKit

class CalculatorViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

    @IBOutlet weak var firstButton: UIButton!
    @IBOutlet weak var secondButton: UIButton!
    @IBOutlet weak var thirdButton: UIButton!
    @IBOutlet weak var fourthButton: UIButton!
    @IBOutlet weak var fifthButton: UIButton!

    @IBOutlet weak var firstTextField: UITextField!
    @IBOutlet weak var secondTextField: UITextField!
    @IBOutlet weak var thirdTextField: UITextField!
    @IBOutlet weak var fourthTextField: UITextField!
    @IBOutlet weak var fifthTextField: UITextField!

    @IBOutlet weak var pickerView: UIPickerView!

    var lastPressedButton: UIButton

    override func viewDidLoad() {
        super.viewDidLoad()

        firstButton.addTarget(self, action:#selector(self.buttonClicked(:)), forControlEvents: .touchUpInside)
        secondButton.addTarget(self, action:#selector(self.buttonClicked(:)), forControlEvents: .touchUpInside)
        thirdButton.addTarget(self, action:#selector(self.buttonClicked(:)), forControlEvents: .touchUpInside)
        fourthButton.addTarget(self, action:#selector(self.buttonClicked(:)), forControlEvents: .touchUpInside)
        fifthButton.addTarget(self, action:#selector(self.buttonClicked(:)), forControlEvents: .touchUpInside)

        self.pickerView.dataSource = self;
        self.pickerView.delegate = self;
    }

    func buttonAction(sender:UIButton!) {

        lastPressedButton = sender

        if lastPressedButton == firstButton {
            firstTextField.inputView = pickerView
        } else if lastPressedButton == secondButton {
            secondTextField.inputView = pickerView
        }
    }

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

        if lastPressedButton == firstButton {
            return firstButtonDataSource.count
        } else if lastPressedButton == secondButton {
            return secondButtonDataSource.count
        }
    }

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {

        if lastPressedButton == firstButton {
            return firstButtonDataSource[row]
        } else if lastPressedButton == secondButton {
            return secondButtonDataSource[row]
        }
    }

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

        if lastPressedButton == first {
            self.firstTextField.text = firstButtonDataSource[row]
        } else if lastPressedButton == secondButton {
            self.secondTextField.text = firstButtonDataSource[row]
        }
    }
}

这里我想使用id对已映射的元素进行排序 属性。

有什么想法吗?谢谢!

3 个答案:

答案 0 :(得分:10)

只需使用Stream#sorted,如下所示,使用Element ID的getter方法名称。

inputCollection.stream()
               .map(e -> new Element(e.id, e.color))
               .sorted(Comparator.comparing(Element::getId))
               .collect(Collectors.toList());

答案 1 :(得分:0)

也许试试这个

library(latex2exp)
plot(x, type = "l", ylab = TeX("$x_{i+1}=x_i+\\epsilon_{i+1}$"), 
     xlab = "step")

答案 2 :(得分:0)

详细示例:对于Stream类的入门者可能会有所帮助。

  public class Employee {
        String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getSalary() {
            return salary;
        }

        public void setSalary(int salary) {
            this.salary = salary;
        }

        int salary;

        public Employee(String n, int s) {
            this.name = n;
            this.salary = s;
        }

        @Override
        public String toString() {
            return "[" + name + ", " + salary + "]";
        }

        public static void main(String[] args) {
            List<Employee> list = new ArrayList<Employee>() {
                {
                    add(new Employee("Joe", 50000));
                    add(new Employee("Jim", 75000));
                    add(new Employee("Tom", 80000));
                    add(new Employee("Jim", 70000));
                    add(new Employee("Steve", 55000));
                    add(new Employee("Jim", 100000));
                    add(new Employee("Joe", 59000));
                    add(new Employee("Rich", 88000));
                }
            };
            List<Employee> listSorted = new ArrayList<Employee>();
            Comparator<Employee>  NameCommparator = Comparator.comparing(Employee::getName);
            listSorted = list.stream().sorted(NameCommparator).collect(Collectors.toList());

            System.out.println(listSorted);

//sorting on descending order
            Comparator<Employee>  SalaryCommparator = Comparator.comparing(Employee::getSalary);
            listSorted = list.stream().sorted(SalaryCommparator.reversed()).collect(Collectors.toList());
            System.out.println(listSorted);


        }
    }