public class Test06 {
/*
* StreamAPI:
* StreamAPI是用来处理数据,处理集合等容器中的数据,处理操作有:查询、筛选、删除、过滤、统计、映射等。
* 希望能够用类似于SQL语法的形式对Java内存中的数据进行处理。
*
* SQL是对数据库中的数据进行处理。
*
*
* Stream本身不负责存储数据,存储数据是用集合,数组等数据结构。
* Stream只负责对数据进行处理、加工。
*
* Stream的操作分为三步:
* 1、创建Stream:告知Stream数据的来源是哪里?
* 2、加工处理:这个过程可以很多步 0~n
* 3、终结操作:收集结果
* 一旦终结就不能再加工了,如果要加工需要重新创建Stream。
*
* Stream的特点:
* (1)Stream本身不负责存储数据,存储数据是用集合,数组等数据结构。
* (2)Stream是不可变,一旦修改,就会产生新的Stream对象。Stream不会修改数据源的数据。
* (3)Stream的操作是一个延迟操作。所有的操作都必须延迟到终结操作时,一起处理。
*
*/
public static void main(String[] args) {
ArrayList<Employee> list = new ArrayList<>();
list.add(new Employee(1, "张三"));
list.add(new Employee(1, "张三")); Stream<Employee> stream = list.stream();
stream = stream.distinct();//处理,中间操作
long count = stream.count();//统计个数 终结操作
System.out.println(count);
System.out.println("over");
System.out.println("--------------------");
//重新遍历list
for (Employee employee : list) {
System.out.println(employee);
} } } class Employee {
private int id;
private String name; public Employee(int id, String name) {
super();
this.id = id;
this.name = name;
} public Employee() {
super();
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} @Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + "]";
} @Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
} @Override
public boolean equals(Object obj) {
System.out.println("比较....");
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}

结果:

比较....
1
over
--------------------
Employee [id=1, name=张三]
Employee [id=1, name=张三]

Stream(一)的更多相关文章

  1. SQL Server-聚焦查询计划Stream Aggregate VS Hash Match Aggregate(二十)

    前言 之前系列中在查询计划中一直出现Stream Aggregate,当时也只是做了基本了解,对于查询计划中出现的操作,我们都需要去详细研究下,只有这样才能对查询计划执行的每一步操作都了如指掌,所以才 ...

  2. Node.js:理解stream

    Stream在node.js中是一个抽象的接口,基于EventEmitter,也是一种Buffer的高级封装,用来处理流数据.流模块便是提供各种API让我们可以很简单的使用Stream. 流分为四种类 ...

  3. node中的Stream-Readable和Writeable解读

    在node中,只要涉及到文件IO的场景一般都会涉及到一个类-Stream.Stream是对IO设备的抽象表示,其在JAVA中也有涉及,主要体现在四个类-InputStream.Reader.Outpu ...

  4. nodejs中流(stream)的理解

    nodejs的fs模块并没有提供一个copy的方法,但我们可以很容易的实现一个,比如: var source = fs.readFileSync('/path/to/source', {encodin ...

  5. Node学习笔记(一):stream流操作

    NodeJs中谈及较多的可能就是Stream模块了,先写一个简单的ajax回调 $.post("index.php",{data:'aaa',order:'ccc'},functi ...

  6. Stream

    Stream的好处 1.Stream AP的引入弥补了JAVA函数式编程的缺陷.2.Stream相比集合类占用内存更小:集合类里的元素是存储在内存里的,Stream里的元素是在访问的时候才被计算出来. ...

  7. Stream流

    在Node中,存在各式各样不同的数据流,Stream(流)是一个由不同对象实现的抽象接口.例如请求HTTP服务器的request是一个 流,类似于stdout(标准输出):包括文件系统.HTTP 请求 ...

  8. [LeetCode] Data Stream as Disjoint Intervals 分离区间的数据流

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  9. [LeetCode] Moving Average from Data Stream 从数据流中移动平均值

    Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...

  10. [LeetCode] Find Median from Data Stream 找出数据流的中位数

    Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...

随机推荐

  1. hystrix(4) 异常降级

    当执行HystrixCommand时,如果发生命令执行异常.熔断器熔断.信号量超过数量,就会执行降级fallback方法,并返回结果.本质上,当出现以上情况是,执行fallback方法,而不是run方 ...

  2. IDEA—使用插件反编译jar包

    关注微信公众号:CodingTechWork,一起学习进步. 引言   在产品上线后,如果遇到问题阻塞,我们第一步要做的就是去查看日志,但是代码不是一个人写的,说不定就会遇到没有日志的,遇到这种情况, ...

  3. CountDownLatch、CyclicBarrier

    CountDownLatch CountDownLatch类位于java.util.concurrent包下,利用它可以实现类似计数器的功能.比如有一个任务A,它要等待其他4个任务执行完毕之后才能执行 ...

  4. 原创-公司项目部署交付环境预检查shell脚本

    大型项目环境预检查脚本,根据自己实际情况修改脚本中变量,给大家一个思路~ #!/usr/bin/env bash root=$( cd $(dirname $0) pwd ) source " ...

  5. org.springframework.beans.factory.BeanCurrentlyInCreationException

    昨天下午的时候,给公司的项目打了个版,发现一直报502错误了,最后在服务器日志上看了一下异常信息,发现报了以下异常信息,导致项目启动就报错了(pc:该项目在我电脑本地启动不报错,之前也没报错). 错误 ...

  6. java获取一天前的时间

    获取一天前的时间 Date date = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); ...

  7. Oracle 11gR2

    OracleOraDb11g_home1TNSListener #其它客服端连接需要开启服务,如不开启,本机连接可以直接使用sqlplus OracleServiceORCL #实例SID服务  sq ...

  8. LinkedList,ArrayList,HashMap,TreeMap

    hashMap是无序的,TreeMap是有序的(hashmap是一个链表,而treemap实现了sortedmap()接口所以有序,其实现同时实现比较器) hashSet的底层是根据HashMap来存 ...

  9. 在IDEA创建类时自动创建作者日期等信息设定

    1.效果 1 package com.dream.test; 2 3 /* 4 * @author 匠人码农 5 * @date 2020/04/18 11:17 6 * 概要: 7 * XXXXX ...

  10. PADS Layout VX.2.3 灌铜之后只显示灌铜外框,没有显示整块铜皮

    操作系统:Windows 10 x64 工具1:PADS Layout VX.2.3 使用Copper Pour功能,画了一个灌铜区,并分配了网络. Tools > Pour Manager,打 ...