简体   繁体   中英

Splitting Class into its Attributes in Lambda Expression in Java

Let's say you have a custom 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:

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

Java doesn't have the concept of tuples, so no! But there can be a workaround by converting the stream to a Map ( which has a lot of limitations and consequences ):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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