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,他们中都带了 ...
随机推荐
- thinkphp5的auth权限认证(转自thinkphp官方文档+自己总结)
thinkphp5的auth权限认证(转自thinkphp官方文档+自己总结) 一.总结 一句话总结:相当于就是用其它thinkphp的扩展一样,都是引入扩展,配置扩展,然后使用 引入 配置 使用 基 ...
- Oracle 创建 Schema
-- 查看当前已有的用户 SELECT Username FROM dba_users; -- 创建临时 CREATE USER gzmpc IDENTIFIED BY PASSWORD; -- 授权 ...
- MyBatis Generator配置文件context元素的defaultModelType属性
MyBatis Generator配置文件context元素的defaultModelType属性 MyBatis Generator配置文件context元素有一个defaultModelType属 ...
- GVIM设置背景颜色
首先找到GVim的安装目录,在安装目录下你可以发现一个_vimrc文件,使用文本编辑器打开后在里面添加两行代码即可:代码如下set gfn=Courier_New:h14colorscheme tor ...
- 005——php字符串中的处理函数(四)
<?php /** * 字符串处理函数: * parse_url 解析URL.返回其组成部分 */ /* $url="http://www.lantianwang.com/admin/ ...
- LINUX创建管道文件
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- Jacoco的原理
覆盖率计数器 Jacoco使用一系列的不同的计数器来做覆盖率的度量计算.所有这些计数器都是从java的class文件中获取信息,这些class文件可以(可选)包含调试的信息在里面.即使在没有源码的情况 ...
- 2018.11.29 Wireless technology roadmap(1)
1物联网还是卖开发板阶段. BT NBIoT 窄带物联网(Narrow Band Internet of Things) Sigfox Zigbee Wifi:大数据量传输 LoRA :远距离无线电( ...
- JSP Unable to compile class for JSP
今天刚弄好MyEclipse环境,试了一下jsp的创建,然后就出现了一个很令人纠结的问题. 文档目录如下: Jsp代码如下: <%@page import="com.pd.Person ...
- PHPExcel解决内存占用过大问题-设置单元格对象缓存
PHPExcel解决内存占用过大问题-设置单元格对象缓存 PHPExcel是一个很强大的处理Excel的PHP开源类,但是很大的一个问题就是它占用内存太大,从1.7.3开始,它支持设置cell的缓存方 ...