Java 8 : Stream API 练习
//店铺属性类
public class Property {
String name;
// 距离,单位:米
Integer distance;
// 销量,月售
Integer sales;
// 价格,这里简单起见就写一个级别代表价格段
Integer priceLevel;
public Property(String name, int distance, int sales, int priceLevel) {
this.name = name;
this.distance = distance;
this.sales = sales;
this.priceLevel = priceLevel;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getDistance() {
return distance;
}
public void setDistance(Integer distance) {
this.distance = distance;
}
public Integer getSales() {
return sales;
}
public void setSales(Integer sales) {
this.sales = sales;
}
public Integer getPriceLevel() {
return priceLevel;
}
public void setPriceLevel(Integer priceLevel) {
this.priceLevel = priceLevel;
}
@Override
public String toString() {
return "Property [name=" + name + ", distance=" + distance + ", sales=" + sales + ", priceLevel=" + priceLevel
+ "]";
} }
//JAVA8Stream它提供了对集合操作的增强 与I/O不同
public class TestJAVA8Stream {
public static void main(String[] args) {
/*// Stream操作 List排序
System.out.println("=====JAVA8Stream操作============方法1找到距离我最近的店铺");
Property p1 = new Property("木桶饭", 1000, 500, 2);
Property p2 = new Property("黄焖鸡", 2300, 1500, 3);
Property p3 = new Property("麦当劳", 580, 3000, 1);
Property p4 = new Property("肯德基", 6000, 200, 4);
List<Property> properties = Arrays.asList(p1, p2, p3, p4);
String name2 = properties.stream()
.sorted(Comparator.comparingInt(x -> x.distance))
.findFirst()
.get().name;
System.out.println("距离我最近的店铺是:" + name2);*/
/*System.out.println("======传统操作List排序===========方法二找到距离我最近的店铺");
Property p1 = new Property("木桶饭", 1000, 500, 2);
Property p2 = new Property("黄焖鸡", 2300, 1500, 3);
Property p3 = new Property("麦当劳", 580, 3000, 1);
Property p4 = new Property("肯德基", 6000, 200, 4);
List<Property> properties = Arrays.asList(p1, p2, p3, p4);
Collections.sort(properties, (x, y) -> x.distance.compareTo(y.distance));
String name = properties.get(0).name;
System.out.println("距离我最近的店铺是:" + name);*/
System.out.println("=====迭代=======");
Property p1 = new Property("木桶饭", 1000, 500, 2);
Property p2 = new Property("黄焖鸡", 2300, 1500, 3);
Property p3 = new Property("woi麦当劳", 580, 3000, 1);
Property p4 = new Property("肯德基", 6000, 200, 4);
List<Property> properties = Arrays.asList(p1, p2, p3, p4);
int count = 0;//月销量大于1000的店铺个数
for (Property property : properties) {
if(property.sales > 1000){
count++;
}
}
System.out.println("月销量大于1000的店铺个数:"+count);
//使用内部迭代进行计算
long count2 = properties.stream().filter(p -> p.sales > 1000).count();
System.out.println("月销量大于1000的店铺个数:"+count2);
long count3 =properties.stream().filter(p -> p.distance < 1000).count();//筛选出距离我在1000米内的店铺
long count4 =properties.stream().filter(p -> p.name.length() > 5).count();//筛选出名称大于5个字的店铺
Property property = properties.stream().max(Comparator.comparingInt(p -> p.priceLevel)).get();//筛选出价格最低的店铺
//获取距离我最近的2个店铺
List<Property> properties_s = properties.stream()
.sorted(Comparator.comparingInt(x -> x.distance))
.limit(2)
.collect(Collectors.toList());
//获取所有店铺的名称
List<String> names = properties.stream()
.map(p -> p.name)
.collect(Collectors.toList());
//获取每个店铺的价格等级
Map<String, Integer> map = properties.stream()
.collect(Collectors.toMap(Property::getName, Property::getPriceLevel));
//所有价格等级的店铺列表
Map<Integer, List<Property>> priceMap = properties.stream()
.collect(Collectors.groupingBy(Property::getPriceLevel)); }
}
Java 8 : Stream API 练习的更多相关文章
- Java 8 Stream API详解--转
原文地址:http://blog.csdn.net/chszs/article/details/47038607 Java 8 Stream API详解 一.Stream API介绍 Java8引入了 ...
- Java 8 Stream API Example Tutorial
Stream API Overview Before we look into Java 8 Stream API Examples, let’s see why it was required. S ...
- Java 8 Stream API
Java 8 Stream API JDK8 中有两大最为重要的改变.第一个是 Lambda 式:另外 Stream API(java.util.stream.*) Stream 是 JDK8 中处理 ...
- Java 8 Stream API具体解释
Java 8 Stream API具体解释 一.Stream API介绍 Java 8引入了全新的Stream API,此Stream与Java I/O包里的InputStream和OutputStr ...
- Java 8 Stream Api 中的 peek 操作
1. 前言 我在Java8 Stream API 详细使用指南[1] 中讲述了 [Java 8 Stream API]( "Java 8 Stream API") 中 map 操作 ...
- Java 使用 Stream API 筛选 List
前言 上课的时候看到老师用迭代器来遍历 List 中的元素的时候,我的内心是极其嫌弃的,这种迭代方法不能直接访问当前的元素,而且写起来也麻烦.于是上网查了查 Java 有没有类似于 Linq 的东西, ...
- Java 8 Stream API的使用示例
前言 Java Stream API借助于Lambda表达式,为Collection操作提供了一个新的选择.如果使用得当,可以极大地提高编程效率和代码可读性. 本文将介绍Stream API包含的方法 ...
- Spring WebFlux 学习笔记 - (一) 前传:学习Java 8 Stream Api (3) - Stream的终端操作
Stream API Java8中有两大最为重要的改变:第一个是 Lambda 表达式:另外一个则是 Stream API(java.util.stream.*). Stream 是 Java8 中处 ...
- Spring WebFlux 学习笔记 - (一) 前传:学习Java 8 Stream Api (2) - Stream的中间操作
Stream API Java8中有两大最为重要的改变:第一个是 Lambda 表达式:另外一个则是 Stream API(java.util.stream.*). Stream 是 Java8 中处 ...
- Spring WebFlux 学习笔记 - (一) 前传:学习Java 8 Stream Api (1) - 创建 Stream
影子 在学习Spring WebFlux之前,我们先来了解JDK的Stream,虽然他们之间没有直接的关系,有趣的是 Spring Web Flux 基于 Reactive Stream,他们中都带了 ...
随机推荐
- codeforces 256 div2 C. Painting Fence 分治
C. Painting Fence time limit per test 1 second memory limit per test 512 megabytes input standard in ...
- python 字典输出键值对
d = {, , } for dict_key, dict_value in d.items(): print(dict_key,'->',dict_value)
- [转]Cryengine渲染引擎剖析
转篇Napoleon314 大牛的分析,排版好乱,见谅,耐心读,这是个好东西,注意看他自己的实现,是个技术狂人啊,Ogre焕发次时代的光芒啊~~~努力 ------------------------ ...
- org.springframework.beans.factory.BeanCreationException: sqlSessionFactory
sqlSessionFactory实例化错误 pom默认导入的jar包中存在低版本,导致实例化sqlSessionFactory错误,删除此jar包即可
- Linux CentOS 6.5 使用自带jdk修改环境变量
首先声明,默认jdk指我们安装完CentOS后系统自带jdk,自己下载安装的jdk只需要下载,解压即可,之后步骤与此文一致 1.查看我们默认jdk的位置 指令: which java 我们去看一下发现 ...
- [转载]SQL语句练习
.查询“生物”课程比“物理”课程成绩高的所有学生的学号: 思路: 获取所有有生物课程的人(学号,成绩) - 临时表 获取所有有物理课程的人(学号,成绩) - 临时表 根据[学号]连接两个临时表: 学号 ...
- 控制反转(IOC)模式
控制反转(Inversion of Control):提倡实现松耦合层.组件和类的设计原则,颠倒程序的控制流程.IoC使用分离执行特定问题处理代码的概念: IoC意味着将你设计好的对象交给容器控制,而 ...
- Linux安装MySQL遇到的问题
安装: https://www.cnblogs.com/fnlingnzb-learner/p/5830622.html https://www.cnblogs.com/xinjing-jingxin ...
- 『转』Kaspersky Internet Security for Android &KMS – 免费6个月
卡巴越南的活动,需要注册账户,完成小调查,24小时内发送激活码,激活码3个月内有效.建议用谷歌翻译下网站.KIS for Android 的激活码也通用于 Kaspersky Mobile Secur ...
- L203 词汇题
Conditions for the growth of this plant are optimum in early summer.we will live as free people, not ...