学习java第22天
2020-12-18 16:33
标签:sep 学习java optional turn int max system rri int() 1.流 得到流 Stream 对于数组 : Arrays.sstream(ary) 对于collection : 用list.stream() *数组进行流化 Arrays.stream(a) .filter(i -> i
.map(i -> i*i) .max(); 学习java第22天 标签:sep 学习java optional turn int max system rri int() 原文地址:https://www.cnblogs.com/SirNie/p/13386377.html
class streamList {
public static void main(String... args) {
Collection
new Person("Ted", 18, 88),
new Person("Charlotte", 18, 88),
new Person("Michael", 18, 99),
new Person("Matthew", 19, 84),
new Person("Matthew", 21, 84)
);
people.parallelStream()
.filter( p -> p.age .sorted( (p1,p2)->p1.age-p2.age)
.sorted( Person::better)
.sorted( Comparator.comparing( Person::getName ) )
.limit(5)
.mapToDouble( p -> p.score )
.average();
System.out.println(result);
}
}
{
public String name;
public int age;
public double score;
name = n; age = a; score = s;
}
public String getName(){ return name;}
@Override
public String toString() {
return String.format("%s[%d](%f)", name,age,score);
}
public static int better(Person p1, Person p2) {
return (int)(p2.score - p1.score);
}
}
class streamArray
{
public static void main(String[] args)
{
int [] a = new int[100];
for(int i=0; i
OptionalInt result=
Arrays.stream(a).parallel()
.filter( i -> i>20 )
.map( i -> i*i )
.sorted()
.distinct()
.limit(10)
.max();
result.isPresent()? "最大值为"+result.getAsInt(): "无值");
}
}
class UseParallelStream
{
public static void main(String[] args)
{
List
System.out.println(
a.parallelStream()
.mapToInt(i->(int)i)
.filter( i -> i>2 )
.map( i -> i*i )
.sorted()
.distinct()
.limit(10)
.max()
);
}
}
上一篇:线程相关工具类