Sebastián Estrella
Table table = new Table();
for (User user : users) {
if (user.isEnabled()) {
Row row = new Row();
row.addColumn(new Column(user.fullName());
String role = user.isAdmin() ? "Admin" : "Member";
row.addColumn(new Column(role));
table.addRow(row);
}
}
table.render();
Table table = new Table();
List<User> enabledUsers = new ArrayList<User>();
for (User user : users) {
if (user.isEnabled()) {
enabledUsers.add(user);
}
}
for (User user : enabledUsers) {
Row row = new Row();
row.addColumn(new Column(user.fullName());
String role = user.isAdmin() ? "Admin" : "Member";
row.addColumn(new Column(role));
table.addRow(row);
}
table.render();
List<User> enabledUsers = new ArrayList<User>();
for (User user : users) {
if (user.isEnabled()) {
enabledUsers.add(user);
}
}
List<Row> rows = new ArrayList<Row>();
for (User user : enabledUsers) {
Row row = new Row();
row.addColumn(new Column(user.fullName());
String role = user.isAdmin() ? "Admin" : "Member";
row.addColumn(new Column(role));
rows.add(row);
}
Table table = new Table();
table.addRows(rows);
table.render();
Readability
Testability
Clear code boundaries
List<User> enabledUsers = new ArrayList<User>();
for (User user : users) {
if (user.isEnabled()) {
enabledUsers.add(user);
}
}
List<User> enabledUsers = users.stream()
.filter(user -> user.isEnabled())
.collect(Collectors.toList());
List<Row> rows = new ArrayList<Row>();
for (User user : enabledUsers) {
Row row = new Row();
row.addColumn(new Column(user.fullName());
String role = user.isAdmin() ? "Admin" : "Member";
row.addColumn(new Column(role));
rows.add(row);
}
List<Row> rows = enabledUsers.stream()
.map(user -> {
Row row = new Row();
row.addColumn(new Column(user.fullName());
String role = user.isAdmin() ? "Admin" : "Member";
row.addColumn(new Column(role));
rows.add(row);
})
.collect(Collectors.toList());
List<Row> rows = users.stream()
.filter(user -> user.isEnabled())
.map(user -> {
Row row = new Row();
row.addColumn(new Column(user.fullName());
String role = user.isAdmin() ? "Admin" : "Member";
row.addColumn(new Column(role));
rows.add(row);
})
.collect(Collectors.toList());
Table table = new Table();
table.addRows(rows);
table.render();
JDK 5 | JDK 6 | JDK 8+ | |
---|---|---|---|
Interfaces | x | x | x |
Classes | x | x | x |
Anonymous Classes | x | x | x |
Generics | x | x | x |
Lambda Expressions | x |
JButton submitButton = new JButton("Submit");
submitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Do something
}
});
interface Filter<A> {
boolean filter(A element);
}
<A> List<A> filter(Filter<A> predicate, List<A> list) {
List<A> result = new ArrayList<A>();
for (A element : list) {
if (predicate.filter(element)) {
result.add(element);
}
}
return result;
}
Filter<User> isEnabled = new Filter<User>() {
public boolean filter(User user) {
return user.isEnabled();
}
};
List<User> enabledUsers = filter(isEnabled, users);
interface Mapper<A, B> {
B map(A element);
}
<A, B> List<B> map(Mapper<A, B> mapper, List<A> list) {
List<B> result = new ArrayList<B>();
for (A element : list) {
result.add(mapper.map(element));
}
return result;
}
List<Row> rows = map(new Mapper<User, Row>() {
public Row map(User user) {
Row row = new Row();
row.addColumn(new Column(user.fullName());
String role = user.isAdmin() ? "Admin" : "Member";
row.addColumn(new Column(role));
return row;
})
}, filter(new Filter<User>() {
public boolean filter(User user) {
return user.isEnabled();
}
}, users));
interface Filter<A> {
boolean filter(A element);
}
interface Mapper<A, B> {
B map(A element);
}
interface Filter<A> extends Function<A, Boolean> {
}
interface Function<A, B> {
B apply(A element);
}
public class FilterTest {
public int[] lambdaExpression(int[] numbers) {
IntPredicate isEven = x -> x % 2 == 0;
return Arrays.stream(numbers).filter(isEven).toArray();
}
}
public class FilterTest {
public int[] anonymousClass(int[] numbers) {
IntPredicate isEven = new IntPredicate() {
public boolean test(int x) {
return x % 2 == 0;
}
};
return Arrays.stream(numbers).filter(isEven).toArray();
}
}
public class FilterTest {
public int[] lambdaExpression(int[]);
private static boolean lambda$lambdaExpression$0(int);
}
public class FilterTest {
public int[] anonymousClass(int[]);
}
public class FilterTest$1 {
public boolean test(int);
}
- new // class FilterTest$1
- invokespecial // Method FilterTest$1."<init>":(LFilterTest;)V
+ invokedynamic // InvokeDynamic #0:test:()Ljava/util/function/IntPredicate;
invokestatic // Method java/util/Arrays.stream:([I)Ljava/util/stream/IntStream;
invokeinterface // InterfaceMethod java/util/stream/IntStream.filter:(Ljava/util/function/IntPredicate;)Ljava/util/stream/IntStream
invokeinterface // InterfaceMethod java/util/stream/IntStream.toArray:()[I
Benchmark | Mode | Score | Error | Units |
---|---|---|---|---|
anonymousClass | thrpt | 3905620.807 | ± 97753.350 | ops/s |
lambdaExpression | thrpt | 3940706.986 | ± 136971.008 | ops/s |
Throughput (thrpt): Measures the number of operations per second
GitHub: sestrella
Twitter: @sestrelladev
https://www.stackbuilders.com
Resources: