Java10で匿名クラス遊び
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
import static java.util.stream.Collectors.toList; | |
// I've referenced this post: | |
// https://developer.oracle.com/java/jdk-10-local-variable-type-inference | |
public class Main { | |
public static void main(String[] args) { | |
var products = List.of( | |
new Product("Apple", 100, 3), | |
new Product("Orange", 50, 10), | |
new Product("Peach", 200, 6)); | |
var reports = products.stream() | |
.map(p -> new Object() { | |
String name = p.name; | |
int total = p.price * p.count; | |
}).collect(toList()); | |
reports.forEach(r -> | |
System.out.println("name = " + r.name + ", total = " + r.total)); | |
} | |
private static class Product { | |
String name; | |
int price; | |
int count; | |
Product(String name, int price, int count) { | |
this.name = name; | |
this.price = price; | |
this.count = count; | |
} | |
} | |
} |
Author
bufferings
commented
Mar 21, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment