昨日のコードをJava8という武器を無駄に使ってワンライナーで書きたい。
public class PivotUtil2 { /** * 旋回する */ public static <S, T, U, V extends Pivotable<S, T, U, V>> List<V> pivot(List<V> table) { return table.stream() .collect(Collectors.groupingBy(v -> v.groupBy())) .values().stream() .map(l -> l.get(0).setMap(l.stream() .collect(Collectors.toMap(t -> t.getFor(), u -> u.getValue())) )).collect(Collectors.toList()); } /** * 旋回を解除する */ public static <S, T, U, V extends Pivotable<S, T, U, V>> List<V> unPivot(List<V> table) { return table.stream().map( p -> p.getMap().entrySet().stream() .map(entry -> p.copy() .setFor(entry.getKey()) .setValue(entry.getValue()) ).collect(Collectors.toList())) .reduce((l1, l2) -> Stream.concat(l1.stream(), l2.stream()) .collect(Collectors.toList())).get(); } /** * ピボット可能インタフェース */ public static interface Pivotable<S, T, U, V extends Pivotable<S, T, U, V>> { public S groupBy(); public T getFor(); public V setFor(T t); public U getValue(); public V setValue(U u); public Map<T, U> getMap(); public V setMap(Map<T, U> map); public V copy(); } }
何が困るってインデントの付け方がさっぱりわからない。
どこで改行するのが正しいの?
もう少しうまく書けないものか。