[JAVA] Sort in List, for personal notes

environment

Java on this page 1.8 It is assembled with.

What you are trying to do

-Sort the classes in the List in a specific order ・ Try using Stream ・ Do not use Comparator

I'll try it right away

First, the class in List

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.

List generation class


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

Try to run


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.

Execution result

Hoge 3SS Hoge 6SS Hoge 1S Hoge 2A Hoge 5A Hoge 4B

Completed for the time being.

Play with

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. ..

Tweak further

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

Sort in List, for personal notes
DDD personal notes
Notes for AST analysis
Jigsaw notes in Gradle
play framework personal notes
Notes in Android studio
List of MySQL sentences for programming beginners * Personal memo
[Java, stream] Sort object list in Japanese by property name
[Neta] Sleep Sort in Java
Basic usage notes for Jackson
apache POI personal notes crossfish21
First Play Framework personal notes
List aggregation in Java (Collectors.groupingBy)
Java List Group, Sort, etc.