jdk8-stream的使用

本文通过实例演示了如何使用 Java 的 Stream API 来简化集合操作,对比传统集合处理方式,展示了 Stream API 在过滤、排序和转换操作上的优势。
/**
 * 
 */
package com.gewb.stream;

/**
 * @author Bingo.Ge
 * @date 2020年6月15日
 */
public class Dish {
	private final String name;
	private final boolean vegetarian;
	private final int calories;
	private final Type type;
	
	
	/**
	 * @param name
	 * @param vegetarian
	 * @param calories
	 * @param type
	 */
	public Dish(String name, boolean vegetarian, int calories, Type type) {
		this.name = name;
		this.vegetarian = vegetarian;
		this.calories = calories;
		this.type = type;
	}
	
	public String getName() {
		return name;
	}
	
	public boolean isVegetarian() {
		return vegetarian;
	}
	
	public int getCalories() {
		return calories;
	}
	
	public Type getType() {
		return type;
	}
	
	public enum Type {MEAT, FISH, OTHER};
	
	
	
}
/**
 * 
 */
package com.gewb.stream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Bingo.Ge
 * @date 2020年6月15日
 */
public class SimpleStream {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		List<Dish> menu = Arrays.asList(new Dish("pork", false, 800, Dish.Type.MEAT),
										new Dish("beef", false, 700, Dish.Type.MEAT),
										new Dish("chicken", false, 400, Dish.Type.MEAT),
										new Dish("french fries", true, 530, Dish.Type.OTHER),
										new Dish("rice", true, 350, Dish.Type.OTHER),
										new Dish("season fruit", true, 120, Dish.Type.OTHER),
										new Dish("pizza", true, 550, Dish.Type.FISH),
										new Dish("prawns", false, 300, Dish.Type.FISH),
										new Dish("salmon", false, 450, Dish.Type.FISH));
		
		List<String> dishNamesByCollections = getDishNameByCollection(menu);
		System.out.println(dishNamesByCollections);
		
		List<String> dishNamesByStream = getDishNameByStream(menu);
		System.out.println(dishNamesByStream);

	}

	/**
	 * @param menu
	 * @return
	 */
	private static List<String> getDishNameByStream(List<Dish> menu) {
		List<String> collect = menu.stream().filter(o -> o.getCalories() <= 400)
						.sorted(Comparator.comparing(Dish::getCalories))
						.map(Dish :: getName)
						.collect(Collectors.toList());
		return collect;
	}

	/**
	 * @param menu
	 * @return
	 */
	private static List<String> getDishNameByCollection(List<Dish> menu) {
		Collections.sort(menu, new Comparator<Dish>() {
			@Override
			public int compare(Dish o1, Dish o2) {
				return Integer.compare(o1.getCalories(), o2.getCalories());
			}
		});
		
		List<String> dishNameList = new ArrayList<>();
		for (Dish dish : menu) {
			if(dish.getCalories() <= 400) {
				dishNameList.add(dish.getName());
			}
		}
		
		return dishNameList;
	}

}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值