1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| import com.sun.org.apache.regexp.internal.RE; import org.yyh.reptile.entity.Msg;
import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.stream.Collector;
public class TestExample { public static void main(String[] args) { List<Msg> msgList = Arrays.asList( new Msg("1", 1L), new Msg("2", 3L), new Msg("3", 3L), new Msg("4", 4L), new Msg("5", 4L) ); ArrayList<Object> collect = msgList.stream() .collect(Collector.of( ArrayList::new, ArrayList::add, (left, right) -> { left.addAll(right); return left; }, Collector.Characteristics.IDENTITY_FINISH )); collect.forEach(System.out::println); HashMap<Long, List<Msg>> hashMap = msgList.stream() .collect(Collector.of( HashMap::new, (map, msg) -> { map.computeIfAbsent(msg.getCreateTime(), k -> new ArrayList<>()).add(msg); }, (left, right) -> { right.forEach((k, v) -> left.merge(k, v, (list, newList) -> { list.addAll(newList); return list; })); return left; }, Collector.Characteristics.IDENTITY_FINISH, Collector.Characteristics.CONCURRENT, Collector.Characteristics.UNORDERED )); msgList.parallelStream() .forEachOrdered(System.out::println); } }
|