利用stream sorted進(jìn)行降序排序
根據(jù)value值的大小進(jìn)行降序排序,并進(jìn)行截取。
public static void main(String[] args) { List<Map<String, Object>> list = Lists.newArrayList(); Map<String, Object> map = Maps.newHashMap(); map.put("id", 1); map.put("value", 20); list.add(map); map = Maps.newHashMap(); map.put("id", 2); map.put("value", 80); list.add(map); map = Maps.newHashMap(); map.put("id", 3); map.put("value", 21); list.add(map); map = Maps.newHashMap(); map.put("id", 4); map.put("value", 28); list.add(map); System.out.println("原始數(shù)據(jù):"+list); //根據(jù)value進(jìn)行排序 得到升序排列 list.sort(Comparator.comparing(h -> ConvertUtil.obj2Integer(h.get("value")))); //顛倒排序,變?yōu)榻敌蚺帕? Collections.reverse(list); //截取前3個(gè) System.out.println("結(jié)果"+list.subList(0, 3)); }
使用1.8 stream處理
List<Map<String, Object>>result = list.stream().sorted((h1, h2) -> //降序 ConvertUtil.obj2Integer(h2.get("value")).compareTo(ConvertUtil .obj2Integer(h1.get("value")))) //前3個(gè) .limit(3) //終止流 .collect(Collectors.toList()); System.out.println("流結(jié)果"+result );
運(yùn)行結(jié)果:
原始數(shù)據(jù):
[{id=1, value=20}, {id=2, value=80}, {id=3, value=21}, {id=4, value=28}]
結(jié)果
[{id=2, value=80}, {id=4, value=28}, {id=3, value=21}]
補(bǔ)充:Java8Stream流的sorted()排序===使用Comparator排序
sorted()方法排序,一個(gè)是Comparable(自然排序),一個(gè)是Comparator接口,像Integer、String等這些基本類型的包裝類已經(jīng)實(shí)現(xiàn)了Comparable接口,
Comparable接口======》java.lang包下的
方法CompareTo(Object o),除了基本類型包裝類外,一些常用的pojo類要使用CompareTo(Object o)排序,就要實(shí)現(xiàn)Comparable接口,在pojo類里重寫compareTo()方法
Comparator接口======》java.util包下的
方法compare(Object a,Object b)
關(guān)于Comparable和Comparator接口
Sorted排序一共有兩種,自然排序和Comparator接口的排序,
使用sorted()排序
1、簡(jiǎn)單List<Integer> 排序=====》
package com.it.test; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class StreamTest { public static void main(String[] args) { List<Integer> integers = new ArrayList<>(); integers.add(9); integers.add(2); integers.add(0); integers.add(4); integers.add(8); List<Integer> collect = integers.stream().sorted().collect(Collectors.toList()); //自然排序 } }
結(jié)果
2、簡(jiǎn)單List<String>排序
package com.it.test; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class StreamTest { public static void main(String[] args) { List<String> strings = new ArrayList<>(); strings.add("aa"); strings.add("b"); strings.add("aac"); strings.add("bb"); strings.add("abb"); List<String> collect1 = strings.stream().sorted().collect(Collectors.toList()); System.out.println(collect1.toString()); } }
結(jié)果
3、復(fù)雜實(shí)體對(duì)象pojo排序
使用Comparator排序
Person.java
package com.it.pojo; import java.util.Comparator; import java.util.Objects; @Data @NoArgsConstructor @ToString public class Person { private String name; private Integer age; }
test.java===>先只根據(jù)年齡排序,后先根據(jù)年齡排序,再根據(jù)姓名排序,都升序
package com.it.test; import java.util.ArrayList; import java.util.List; import com.it.pojo.Person; import java.util.Comparator; import java.util.stream.Collectors; public class StreamTest { public static void main(String[] args) { Person person1 = new Person(); person1.setAge(21); person1.setName("21"); Person person2 = new Person(); person2.setAge(19); person2.setName("19"); Person person3 = new Person(); person3.setAge(19); person3.setName("20"); Person person4 = new Person(); person4.setAge(20); person4.setName("20"); Person person5 = new Person(); person5.setAge(19); person5.setName("18"); List<Person> people = new ArrayList<>(); people.add(person1); people.add(person2); people.add(person3); people.add(person4); people.add(person5); List<Person> collect1 = people.stream().sorted((Comparator.comparing(Person::getAge))).collect(Collectors.toList());//只根據(jù)年齡排序,升序 System.out.println(collect2); List<Person> collect2 = people.stream().sorted((Comparator.comparing(Person::getAge)).reversed()).collect(Collectors.toList());//只根據(jù)年齡排序,降序 System.out.println(collect2); List<Person> collect3 = people.stream().sorted((Comparator.comparing(Person::getAge)).thenComparing(Comparator.comparing(Person::getName))).collect(Collectors.toList());//先根據(jù)年齡進(jìn)行排序,遇見相同的,然后根據(jù)姓名進(jìn)行排序,都升序 System.out.println(collect3); List<Person> collect4 = people.stream().sorted((Comparator.comparing(Person::getAge)).thenComparing(Comparator.comparing(Person::getName)).reversed()).collect(Collectors.toList());/先根據(jù)年齡進(jìn)行排序,遇見相同的,然后根據(jù)姓名進(jìn)行排序,降序【都降序】 System.out.println(collect4); List<Person> collect5 = people.stream().sorted(Comparator.comparing(Person::getAge).reversed().thenComparing(Comparator.comparing(Person::getName)).reversed()).collect(Collectors.toList());//年齡升序,姓名降序 System.out.println(collect5); List<Person> collect6 = people.stream().sorted(Comparator.comparing(Person::getAge).reversed().thenComparing(Comparator.comparing(Person::getName))).collect(Collectors.toList());//根據(jù)年齡降序,姓名升序 System.out.println(collect6); } }
結(jié)果
只根據(jù)年齡進(jìn)行排序的,默認(rèn)是升序
根據(jù)年齡降序
先根據(jù)年齡,后根據(jù)姓名排序的,默認(rèn)都是升序
根據(jù)年齡和性別,先根據(jù)年齡,年齡相同根據(jù)性別,只在最后一個(gè)comparing上寫reversed,兩個(gè)排序都是降序【根據(jù)年齡降序,姓名降序】
根據(jù)年齡升序,年齡相同,根據(jù)姓名降序
總結(jié)
先這么著吧
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
Java中@Autowired和@Resource區(qū)別
本文主要介紹了Java中@Autowired和@Resource區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06Spring中的ApplicationContext與BeanFactory詳解
這篇文章主要介紹了Spring中的ApplicationContext與BeanFactory詳解,Spring的IoC容器就是一個(gè)實(shí)現(xiàn)了BeanFactory接口的可實(shí)例化類,事實(shí)上, Spring提供了兩種不同的容器,一種是最基本的BeanFactory,另一種是擴(kuò)展的ApplicationContext,需要的朋友可以參考下2024-01-01解決eclipse啟動(dòng)tomcat時(shí)不能加載web項(xiàng)目的問題
這篇文章主要介紹了解決eclipse啟動(dòng)tomcat時(shí)不能加載web項(xiàng)目的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06