简体   繁体   English

将 Class 拆分为 Lambda 中的属性 Java 中的表达式

[英]Splitting Class into its Attributes in Lambda Expression in Java

Let's say you have a custom class:假设您有一个自定义 class:

public class T {
    int a;
    int b;
}

Is there a way you can split up the class in a lambda expression in the following way:有没有办法可以通过以下方式在 lambda 表达式中拆分 class :

(Stream of T instances).forEach((a, b) -> {});

Java doesn't have the concept of tuples, so no! Java 没有元组的概念,所以没有! But there can be a workaround by converting the stream to a Map ( which has a lot of limitations and consequences ):但是可以通过将 stream 转换为Map这有很多限制和后果)来解决问题:

(Stream of T instances)
    .collect(Collectors.toMap(t -> t.a, t -> t.b))
    .forEach((a, b) -> {});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM