学习任务:

视频学习

动手测试生成流

根据Stream API总图可知,对Collection、Map、数组三种数据结构生成流对象,编写测试代码。

公用类PbCourse(复制即可):

package com.pbteach.javase.oop.stream.test1;

/**
 * 	课程类
 * @author 攀博课堂(www.pbteach.com)
 *
 */
public class PbCourse implements Comparable<PbCourse> {
	
	//课程标识
	private long id;
	//课程名称
	private String courseName;
	//课程价格
	private int price;
	//课程分类
	private String category;
	
	@Override
	public int compareTo(PbCourse o) {
		//新数减旧数,大于0表示新数排在后边
		return this.price - o.getPrice();
	}
	
	public PbCourse() {
		
	}
	public PbCourse(long id, String courseName, int price) {
		this.id = id;
		this.courseName = courseName;
		this.price = price;
	}
	public PbCourse(long id, String courseName, int price,String category) {
		this.id = id;
		this.courseName = courseName;
		this.price = price;
		this.category=category;
	}
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getCourseName() {
		return courseName;
	}
	public void setCourseName(String courseName) {
		this.courseName = courseName;
	}
	public int getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}
	
	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	@Override
	public int hashCode() {
		return 1;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		PbCourse other = (PbCourse) obj;
		if (courseName == null) {
			if (other.courseName != null)
				return false;
		} else if (!courseName.equals(other.courseName))
			return false;
		if (id != other.id)
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "PbCourse [id=" + id + ", courseName=" + courseName + ", price=" + price + ", category=" + category
				+ "]";
	}

	

}

测试代码如下:

package com.pbteach.javase.oop.stream.test1;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;


/**
 * Stream api 测试
 * 
 * @author 攀博课堂(www.pbteach.com)
 *
 */
public class StreamApiTest1 {
	
	//Collection生成流
	public static void test1() {
		 //创建collection集合
        List<String> list = new ArrayList<String>() ;
        list.add("hello");
        list.add("www");
        list.add("pbteach") ;
        list.add("com") ;

        // 调用stream方法生成流对象
        Stream<String> stream = list.stream();

        // 对流进行终结操作
        stream.forEach(System.out::println);
	}
	
	//Map生成流
	public static void test2() {
		//定义一个map
		Map<String,PbCourse> map = new HashMap<>();

        // 添加课程
		map.put("100",new PbCourse(100L, "攀博课堂-Java面向对象教程", 33));
		map.put("101",new PbCourse(101L, "攀博课堂-Spring全家桶教程", 44));
		map.put("102",new PbCourse(102L, "攀博课堂-SpringCloud微服务教程", 55));
		map.put("103",new PbCourse(103L, "攀博课堂-在线教育分布式项目教程", 66));
		map.put("104",new PbCourse(104L, "攀博课堂-电子商务项目教程", 66));
		
		 // 获取该Map集合所有的键对应的Set集合
        Set<String> keySet = map.keySet();

        // 通过Set集合对象调用stream方法获取流对象
        Stream<String> streamByKey = keySet.stream();

        // 对流进行终结操作
        streamByKey.forEach( s -> {
        	PbCourse pbCourse = map.get(s) ;
            System.out.println(pbCourse);
        });
	}
	
	//数组生成流
	public static void test3() {
		String[] strs = new String[] {"hello","www","pbteach","com"};
		Stream<String> stream = Stream.of(strs);//创建Stream流对象
		stream.forEach(System.out::println);
	}

	public static void main(String[] args) {
		test1();
		test2();
		test3();
	}

}

提问-攀博课堂
我要提问 不会就问,有效沟通
关注公众号,加入微信群交流提问。 攀博课堂官方公众号
问答列表,查看本知识点所有问题