• 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));
    	
    	
    	
    }
    }
    

      

  • 相关阅读:
    在国内时,更新ADT时需要配置的
    mantis增加密码修改
    http://182.92.241.20/mypro/login 偶的点金项目细化分包管理平台即将上线!!
    bootstrap菜单完美解决---原创
    PB常用日期
    ctrl+shift+del 清理火狐缓存,解决页面显示错乱问题
    Kylin上chromium不能用flash的解决命令
    正确的SVN导入代码命令
    GNU :6.47 Function Names as Strings
    std::advance 给迭代器增加指定偏移量
  • 原文地址:https://www.cnblogs.com/ipetergo/p/7425214.html
Copyright © 2020-2023  润新知