Java on this page 1.8 It is assembled with.
-Sort the classes in the List in a specific order ・ Try using Stream ・ Do not use Comparator
Hoge.java
private class Hoge {
private final String priority;
private final String name;
private Hoge(String priority, String name) {
this.priority = priority;
this.name = name;
}
}
Like this. There is no getter / setter because it is troublesome.
private List<Hoge> createList() {
List<Hoge> list = new ArrayList<>();
list.add(new Hoge("Hoge 1S", "S"));
list.add(new Hoge("Hoge 2A", "A"));
list.add(new Hoge("Hoge 3SS", "SS"));
list.add(new Hoge("Hoge 4B", "B"));
list.add(new Hoge("Hoge 5A", "A"));
list.add(new Hoge("Hoge 6SS", "SS"));
return list;
}
It's a sample, and simple is the best w So it looks like this.
This with priority SS > S > A > B Try to arrange in the order of
public static void main(String[] args) {
List<Hoge> list = createList();
Stream.of(
list.stream().filter(o -> "SS".equals(o.priority)),
list.stream().filter(o -> "S".equals(o.priority)),
list.stream().filter(o -> "A".equals(o.priority)),
list.stream().filter(o -> "B".equals(o.priority))
).flatMap(s -> s).forEach(h -> System.out.println(h.name));
}
The only special mention is how to use flatMap, which looks useless at first glance? If you don't use this here, it will be Stream \ <Stream \ <Hoge > >. Here, I want to make it Stream \ <Hoge >, so it looks like this.
Hoge 3SS Hoge 6SS Hoge 1S Hoge 2A Hoge 5A Hoge 4B
Completed for the time being.
Stream.of(
list.stream().filter(o -> "SS".equals(o.priority)),
list.stream().filter(o -> "S".equals(o.priority)),
list.stream().filter(o -> "A".equals(o.priority)),
list.stream().filter(o -> "B".equals(o.priority))
)
I want to do something about this part ...
Let's replace it with the Predicate type for the time being.
Predicate<Hoge> isSs = o -> "SS".equals(o.priority);
Predicate<Hoge> isS = o -> "S".equals(o.priority);
Predicate<Hoge> isA = o -> "A".equals(o.priority);
Predicate<Hoge> isB = o -> "B".equals(o.priority);
Stream.of(
list.stream().filter(isSs),
list.stream().filter(isS),
list.stream().filter(isA),
list.stream().filter(isB)
).flatMap(s -> s).forEach(h -> System.out.println(h.name));
It's subtle. It's a little that there are 4 Predicate types. ..
The four functions of the Predicate type only change the part before equals, I wonder if I can manage to pass only this String part.
It's like passing a String and returning a Predicate type. So, let's enclose it in Function type.
Function<String, Predicate<Hoge>> priority = p -> new Predicate<Hoge>(){
@Override
public boolean test(Hoge t) {
return p.equals(t.priority);
}
};
Stream.of(
list.stream().filter(priority.apply("SS")),
list.stream().filter(priority.apply("S")),
list.stream().filter(priority.apply("A")),
list.stream().filter(priority.apply("B"))
).flatMap(s -> s).forEach(h -> System.out.println(h.name));
I tried writing in an anonymous class once, but I wonder if it's much better.
Convert anonymous class to lambda.
Function<String, Predicate<Hoge>> priority = p -> t -> { return p.equals(t.priority); }
You don't even need a return. Let's take it.
Function<String, Predicate<Hoge>> priority = p -> t -> p.equals(t.priority);
Stream.of(
list.stream().filter(priority.apply("SS")),
list.stream().filter(priority.apply("S")),
list.stream().filter(priority.apply("A")),
list.stream().filter(priority.apply("B"))
).flatMap(s -> s).forEach(h -> System.out.println(h.name));
It turned out to be something like this. Will it be a little more manageable?
Recommended Posts