java8的stream如何取max
java8的stream取max
?public static void main(String[] args) { ? ? ? ? List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6); ? ? ? ? Integer max = list.stream().max((a, b) -> { ? ? ? ? ? ? if (a > b) { ? ? ? ? ? ? ? ? return 1; ? ? ? ? ? ? } else return -1; ? ? ? ? }).get(); ? ? ? ? System.out.println(max); ? ? }
注意點(diǎn):這里判斷大小是通過正負(fù)數(shù)和0值。 而不是直接寫成
if (a > b) { return a; } else return b;
可以簡化寫法
int max = list.stream().max((a, b) -> a > b ? 1 : -1).get();
java8 stream詳解~聚合(max/min/count)
max
、min
、count
這些字眼你一定不陌生,沒錯(cuò),在mysql中我們常用它們進(jìn)行數(shù)據(jù)統(tǒng)計(jì)。
Java stream中也引入了這些概念和用法,極大地方便了我們對集合、數(shù)組的數(shù)據(jù)統(tǒng)計(jì)工作。
「案例一:獲取String集合中最長的元素?!?/h3>
public class StreamTest {
public static void main(String[] args) {
List<String> list = Arrays.asList("adnm", "admmt", "pot", "xbangd", "weoujgsd");
Optional<String> max = list.stream().max(Comparator.comparing(String::length));
System.out.println("最長的字符串:" + max.get());
}
}
public class StreamTest { public static void main(String[] args) { List<String> list = Arrays.asList("adnm", "admmt", "pot", "xbangd", "weoujgsd"); Optional<String> max = list.stream().max(Comparator.comparing(String::length)); System.out.println("最長的字符串:" + max.get()); } }
「案例二:獲取Integer集合中的最大值?!?/h3>
public class StreamTest {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(7, 6, 9, 4, 11, 6);
// 自然排序
Optional<Integer> max = list.stream().max(Integer::compareTo);
// 自定義排序
Optional<Integer> max2 = list.stream().max(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1.compareTo(o2);
}
});
System.out.println("自然排序的最大值:" + max.get());
System.out.println("自定義排序的最大值:" + max2.get());
}
}
public class StreamTest { public static void main(String[] args) { List<Integer> list = Arrays.asList(7, 6, 9, 4, 11, 6); // 自然排序 Optional<Integer> max = list.stream().max(Integer::compareTo); // 自定義排序 Optional<Integer> max2 = list.stream().max(new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o1.compareTo(o2); } }); System.out.println("自然排序的最大值:" + max.get()); System.out.println("自定義排序的最大值:" + max2.get()); } }
「案例三:獲取員工工資最高的人?!?/h3>
public class StreamTest {
public static void main(String[] args) {
List<Person> personList = new ArrayList<Person>();
personList.add(new Person("Tom", 8900, 23, "male", "New York"));
personList.add(new Person("Jack", 7000, 25, "male", "Washington"));
personList.add(new Person("Lily", 7800, 21, "female", "Washington"));
personList.add(new Person("Anni", 8200, 24, "female", "New York"));
personList.add(new Person("Owen", 9500, 25, "male", "New York"));
personList.add(new Person("Alisa", 7900, 26, "female", "New York"));
Optional<Person> max = personList.stream().max(Comparator.comparingInt(Person::getSalary));
System.out.println("員工工資最大值:" + max.get().getSalary());
}
}
public class StreamTest { public static void main(String[] args) { List<Person> personList = new ArrayList<Person>(); personList.add(new Person("Tom", 8900, 23, "male", "New York")); personList.add(new Person("Jack", 7000, 25, "male", "Washington")); personList.add(new Person("Lily", 7800, 21, "female", "Washington")); personList.add(new Person("Anni", 8200, 24, "female", "New York")); personList.add(new Person("Owen", 9500, 25, "male", "New York")); personList.add(new Person("Alisa", 7900, 26, "female", "New York")); Optional<Person> max = personList.stream().max(Comparator.comparingInt(Person::getSalary)); System.out.println("員工工資最大值:" + max.get().getSalary()); } }
「案例四:計(jì)算Integer集合中大于6的元素的個(gè)數(shù)?!?/h3>
import java.util.Arrays;
import java.util.List;
public class StreamTest {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(7, 6, 4, 8, 2, 11, 9);
long count = list.stream().filter(x -> x > 6).count();
System.out.println("list中大于6的元素個(gè)數(shù):" + count);
}
}
import java.util.Arrays; import java.util.List; public class StreamTest { public static void main(String[] args) { List<Integer> list = Arrays.asList(7, 6, 4, 8, 2, 11, 9); long count = list.stream().filter(x -> x > 6).count(); System.out.println("list中大于6的元素個(gè)數(shù):" + count); } }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot非分布式定時(shí)任務(wù)實(shí)現(xiàn)代碼
這篇文章主要介紹了Springboot非分布式定時(shí)任務(wù)實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11Java實(shí)現(xiàn)冒泡排序與雙向冒泡排序算法的代碼示例
這篇文章主要介紹了Java實(shí)現(xiàn)冒泡排序與雙向冒泡排序算法的代碼示例,值得一提的是所謂的雙向冒泡排序并不比普通的冒泡排序效率來得高,注意相應(yīng)的時(shí)間復(fù)雜度,需要的朋友可以參考下2016-04-04JAVA基于SnakeYAML實(shí)現(xiàn)解析與序列化YAML
這篇文章主要介紹了JAVA基于SnakeYAML實(shí)現(xiàn)解析與序列化YAML,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-124位吸血鬼數(shù)字的java實(shí)現(xiàn)思路與實(shí)例講解
今天小編就為大家分享一篇關(guān)于4位吸血鬼數(shù)字的java實(shí)現(xiàn)思路與實(shí)例講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03java 用泛型參數(shù)類型構(gòu)造數(shù)組詳解及實(shí)例
這篇文章主要介紹了java 用泛型參數(shù)類型構(gòu)造數(shù)組詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-02-02JavaWeb中Session對象的學(xué)習(xí)筆記
在WEB開發(fā)中,服務(wù)器可以為每個(gè)用戶瀏覽器創(chuàng)建一個(gè)會話對象,即session對象,這篇文章就為大家詳細(xì)介紹Session對象的定義、實(shí)現(xiàn)原理等基礎(chǔ)知識點(diǎn),感興趣的小伙伴們可以參考一下2016-05-05mybatis注解如何實(shí)現(xiàn)對象批量更改
這篇文章主要介紹了mybatis注解實(shí)現(xiàn)對象批量更改的方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07