Converting Set to List using Streams

Stream APIs were added in Java 8. You can read more about streams here : Java 8 Streams

With single line of code we can convert a set into a list. Other wise we have to loop through the list and add each element to the set

List<object width="300" height="150"> res;
  Set<object> keySet = //get the set;
  res = keySet.stream().collect(Collectors.toList());
  return res;
 
 
Similarly converting List to a Set
<pre lang="java">
  Set<object> res;
  List<object> keySet = //get the list;
  res = keySet.stream().collect(Collectors.toSet());
  return res;
</object></object>

Uday Ogra

Connect with me at http://facebook.com/tendulkarogra and lets have some healthy discussion :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *