Java 8 : Stream API 练习
2017-08-24 20:50
387 查看
//店铺属性类 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)); } }
相关文章推荐
- Java8 Lambda表达式与Stream API (二): Stream API的使用
- JAVA学习第三十课(常用对象API)- String类:类方法练习
- JAVA 8 StreamAPI 和 lambda表达式 总结(三)--Optional类型
- Java Stream API性能测试
- Java8学习笔记之Stream API
- Atitit 实现java的linq 以及与stream api的比较
- Java 8新特性:全新的Stream API (三) 转载整理
- java 9 中对 Stream API 的增强
- JAVA学习第四十课(常用对象API)- Map集合练习
- Poechant 练习 Java API - Y/N 输入验证
- Java 8 Stream API features --- map/reduce methods
- Upgrading to Java 8——第四章 The Stream API
- 编码练习——Java-String-API-练习
- 详解Java8特性之Stream API并行流
- Atitit 实现java的linq 以及与stream api的比较
- Java 8学习之Stream API
- JAVA 8 StreamAPI 和 lambda表达式 总结(四)--stream的一些聚合操作
- Java 8 Stream API 实用指南
- Java的Reflect反射基础API练习
- java8 Stream API之reduce