java 中Consumer的使用

参考链接 https://blog.csdn.net/chuji2012/article/details/77871011

在java8中添加了Consumer,本文讲下其使用。Represents an operation that accepts a single input argument and returns no result. 表示接受单个输入参数且不返回结果的操作。

我理解其初衷是,从架构的角度提供对目标对象的加工处理accept(T t)及相关操作的逻辑顺序andThen(Consumer<? super T> after),使代码结构更加优雅。

 

使用方法:step1,构造处理对象

public class Item implements Comparable {
    private int index;
    private int value;

    public Item(int index, int value) {
        this.index = index;
        this.value = value;
        System.out.println("constructor中初始数据为:" + toString());
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
        System.out.println("setValue(int):current value=" + value);
    }

    @Override
    public int compareTo(@NonNull Object o) {
        if (o instanceof Item) {
            Item item = (Item) o;
            return this.value - item.value;
        } else {
            System.err.println("wrong item obj");
        }
        return 1;
    }

    @Override
    public String toString() {
        return "Item:index=" + index + ".value=" + value;
    }
}

step2,使用Consumer处理目标对象及打印结果

public static void main(String[] args) {
	/**
	 * item.value=item.value*2
	 */
	Consumer<Item> consumerDouble = new Consumer<Item>() {
		@Override
		public void accept(Item item) {
			if (item != null) {
				int value = item.getValue();
				item.setValue(value * 2);
			}
		}
	};

	/**
	 * item.value=1_000_000+item.value
	 */
	Consumer<Item> consumerAddMilion = new Consumer<Item>() {
		@Override
		public void accept(Item item) {
			if (item != null) {
				int value = item.getValue();
				item.setValue(1_000_000 + value);
			}
		}
	};

	/**
	 * print item info
	 */
	Consumer<Item> consumerPrint = new Consumer<Item>() {
		@Override
		public void accept(Item item) {
			String content = item == null ? "null" : item.toString();
			System.out.println("consumerPrint.print content=[" + content + "]");
		}
	};

	//使用方法事例1
    Item item = new Item(1, 111);
    consumerDouble.andThen(consumerAddMilion)
            .andThen(consumerPrint)
            .accept(item);

    //使用方法事例2
    item = new Item(2, 222);
    consumerDouble.andThen(consumerAddMilion)
            .andThen(consumerPrint)
            .accept(item);

    //使用方法事例3.等价于使用方法事例1,显然事例1更加优雅
    item = new Item(1, 111);
    consumerDouble.accept(item);
    consumerAddMilion.accept(item);
    consumerPrint.accept(item);
}

//打印结果
//使用方法事例1
constructor中初始数据为:Item:index=1.value=111
setValue(int):current value=222
setValue(int):current value=1000222
consumerPrint.print content=[Item:index=1.value=1000222]

//使用方法事例2
constructor中初始数据为:Item:index=2.value=222
setValue(int):current value=444
setValue(int):current value=1000444
consumerPrint.print content=[Item:index=2.value=1000444]

//使用方法事例3
constructor中初始数据为:Item:index=1.value=111
setValue(int):current value=222
setValue(int):current value=1000222
consumerPrint.print content=[Item:index=1.value=1000222]

 

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值