Collectors toCollection() in JavaIn Java the Collectors.ToCollection() approach is a maximum beneficial application provided by means of the java.util.Stream.Collectors class which is going to allows you to collect elements from a circulation into a specific type of collection that you specify. The approach provides flexibility in choosing the sort of collection you want to collect your circulate factors into. Syntax:where each parameters are as follows: <T>: It is a universal type parameter that represents the kind of elements inside the circulate. When using the technique, you need to specify the real form of factors you are working with. For example, when you have a flow of String items, you would use String as the type parameter. <C extends Collection<T>>: It is other generic kind parameter that represents the sort of collection you want to accumulate the flow elements into. The C type parameter have to be a subtype of Collection<T>, making sure that the collected elements can be saved inside the specific series type. For example, if you need to gather elements into an ArrayList<String>, you would use ArrayList<String> as the type parameter. Collector<T, ?, C>: It is the return type of the toCollection() method. It represents a collector that accumulates the stream elements into the specified collection type (C). The T represents the type of elements in the stream, and the "?" denotes an unspecified intermediate result type. Supplier<C> collectionFactory: It is a functional interface representing a supplier function. It is liable for creating a new example of the favoured series kind (C). The collectionFactory parameter is used to supply the gathering advent good judgment. You can provide a manner reference or a lambda expression that returns a ultra-modern instance of the gathering. For example, ArrayList::new or HashSet::new can be used as the collection factory to create new instances of ArrayList or HashSet, respectively. Example 1:ALGORITHM:Step 1: Create a List named names containing some names. Step 2: Use the stream() method on names to create a stream of elements. Step 3: Call the collect() method on the stream, passing Collectors.toCollection(TreeSet::new) as the collector. Step 4: The TreeSet::new is a method reference that creates a new instance of TreeSet, which will be the collection to hold the unique names. Step 5: The collect() operation accumulates the stream elements into the TreeSet. Step 6: Assign the result to the nameSet variable. Step 7: Finally, print the contents of nameSet, which will contain the unique names in sorted order due to the nature of TreeSet. Implementation:The implementation of the above steps given below FileName: ToCollectionExample.java Output: Unique names: [Alice,Bob,Charlie] Complexity Analysis: Time Complexity -O(n) to O (n long) that it will depend on the stream operations Space Complexity-O(n) where n is the number of elements. Example 2:ALGORITHM:Step 1: Create a stream of numbers using Stream.of() and pass the numbers as arguments (1,2,3,4,5). Step 2: Call the collect() method on the stream and pass Collectors.toCollection(ArrayList::new) as the collector. Step 3: It specifies that the elements should be collected into an ArrayList. Step 4: The ArrayList::new method reference is used as the collectionFactory parameter, which creates a new instance of ArrayList to hold the collected elements. Step 5: The collected elements are assigned to the numberList variable of type List<Integer>. Step 6: Print the contents of the numberList using System.out.println() Implementation:The implementation of the above steps given below FileName: ToCollectionExample.java Output: Collected List: [1,2,3,4,5] Complexity Analysis: Time Complexity -O(n) to O(n long) that it will depend on the stream operations Space Complexity-O(n) where n is the number of elements. |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India