I have List<Object>
, and I want to convert it into Map<Integer, List<Object>>
where key is the size of the list and value is the list itself using java 8 streams api. I can do it using Java 7 as
Map<Integer, List<Object>> map= new HashMap<>();
map.put(list.size(), list);
But how to do it using Java 8.
That should be just
Stream.of(list)
.collect(Collectors.toMap(List::size, Function.identity()));
But why do you want to do this? It's pointless. If you had written this and I were to review your code, I would definitely reject this.
The Streams API is not some magical key to just solve everything. The Streams API is built to operate on streams of elements , in a functional manner.
Just stick with the way you mentioned in your post.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.