Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use stream to realize multi-field sorting in java8

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you about how to use stream to achieve multi-field sorting in java8, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Using the new features of java8, let's start with the basics

one

two

three

four

five

six

seven

eight

nine

ten

eleven

twelve

thirteen

fourteen

fifteen

sixteen

seventeen

eighteen

nineteen

twenty

twenty-one

twenty-two

twenty-three

twenty-four

twenty-five

twenty-six

twenty-seven

twenty-eight

twenty-nine

thirty

thirty-one

thirty-two

thirty-three

List list; stands for a collection

/ / returns the collection of objects sorted by class attributes in ascending order

List.stream () .sorted (Comparator.comparing (class:: attribute 1))

/ / return a collection of objects to sort by class attributes in descending order. Note two ways of writing.

List.stream (). Sorted (Comparator.comparing (class: attribute 1). Reversed (); / / first, the attribute is in ascending order, and as a result, the attribute is in descending order

List.stream () .sorted (Comparator.comparing (class: attribute 1, Comparator.reverseOrder (); / / descend with attribute

/ / returns the collection of objects with one ascending order of class attributes and two ascending order of attributes

List.stream (). Sorted (Comparator.comparing (class:: attribute 1). ThenComparing (class:: attribute 2))

/ / return the collection of objects with class attributes, one descending attribute, two ascending order, two writing methods.

List.stream (). Sorted (Comparator.comparing (class: attribute 1). Reversed (). ThenComparing (class: attribute 2)); / / first use the attribute one ascending order, the ascending order result of the attribute one descending order, and then the attribute second ascending order

List.stream () .sorted (Comparator.comparing (class: attribute one, Comparator.reverseOrder ()) .thencomparing (class: attribute two)); / / first descend the order of attribute one, then ascend the second order of attribute

/ / return the collection of objects with class attributes, one descending attribute and two descending order.

List.stream (). Sorted (Comparator.comparing (class: attribute 1). Reversed (). ThenComparing (class: attribute 2, Comparator.reverseOrder ()); / / first, attribute one ascending order, ascending order result, attribute one descending order, and then attribute second descending order

List.stream () .sorted (Comparator.comparing (class: attribute 1, Comparator.reverseOrder ()) .thencomparing (class: attribute 2, Comparator.reverseOrder ()); / / descend the attribute first, and then descend the attribute second

/ / return the collection of objects with class attributes, one ascending order, two descending order, two writing methods.

List.stream (). Sorted (Comparator.comparing (class: attribute 1). Reversed (). ThenComparing (class: attribute 2). Reversed (); / / first use the attribute one ascending order, the ascending order result carries on the attribute one descending order, then the attribute second ascending order, the result carries on the attribute one descending attribute two descending order

List.stream (). Sorted (Comparator.comparing (class: attribute 1). ThenComparing (class: attribute 2, Comparator.reverseOrder ()); / / first ascend the order of attributes, and then descend the order of attributes

Through the above examples, we can find that

1. Comparator.comparing (class:: attribute 1) .reversed ()

2. Comparator.comparing (class: attribute 1, Comparator.reverseOrder ())

The two kinds of sorting are completely different, be sure to distinguish 1 is to get the sort results and then sort, 2 is to sort directly, many people will be confused to make mistakes in understanding, 2 is better to understand, it is recommended to use 2

Practical examples:

An existing class test has two attributes: state state time time, which requires state order and reverse time order

one

two

three

four

five

six

seven

eight

nine

ten

eleven

twelve

thirteen

fourteen

fifteen

sixteen

seventeen

eighteen

nineteen

twenty

twenty-one

twenty-two

twenty-three

twenty-four

twenty-five

twenty-six

twenty-seven

twenty-eight

twenty-nine

thirty

thirty-one

thirty-two

thirty-three

thirty-four

thirty-five

Class test {

/ / status

Private int state

/ / time

Private Date time

Public test (int state, Date time) {

This.state = state

This.time = time

}

Public int getState () {

Return state

}

Public void setState (int state) {

This.state = state

}

Public Date getTime () {

Return time

}

Public void setTime (Date time) {

This.time = time

}

@ Override

Public String toString () {

Return "test {" +

"state=" + state +

", time=" + DateUtils.formatDateYMD (time) +

'}'

}

}

one

two

three

four

five

six

seven

eight

nine

ten

eleven

twelve

thirteen

fourteen

fifteen

sixteen

seventeen

eighteen

nineteen

twenty

twenty-one

twenty-two

twenty-three

twenty-four

twenty-five

Class testRun {

Public static void main (String [] args) {

List testList = new ArrayList ()

Date d = DateUtils.now ()

For (int I = 1; I {

System.out.println (o.toString ())

});

}

}

Running result:

one

two

three

four

five

six

seven

eight

nine

ten

eleven

twelve

thirteen

fourteen

fifteen

sixteen

seventeen

Before sorting:

Test {state=1, time=2019-07-24}

Test {state=2, time=2019-07-25}

Test {state=3, time=2019-07-26}

Test {state=1, time=2019-08-23}

Test {state=2, time=2019-09-23}

Test {state=3, time=2019-10-23}

-

After sorting:

Test {state=1, time=2019-08-23}

Test {state=1, time=2019-07-24}

Test {state=2, time=2019-09-23}

Test {state=2, time=2019-07-25}

Test {state=3, time=2019-10-23}

Test {state=3, time=2019-07-26}

Process finished with exit code 0

The above is how to use stream to achieve multi-field sorting in java8. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report