How to sort list using stream

Hello,

As we all know that if we have to sort a list than we have to use Collections.sort(list)

but while we are using java 8 not necessary to go to Collections class while we have to sort Integer or String or like all other wrapper classes

It is as simple given below

List<Integer> list = new ArrayList<>();
list.add(1);
list.add(16);
list.add(8);
list.add(10);

list.add(3);
System.out.println(“before list:” + list);
list.stream().sorted().forEach(System.out::println);

just a simple sorted method  in stream api

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s