VOOZH about

URL: https://www.geeksforgeeks.org/java/set-addall-method-in-java-with-examples/

⇱ Set addAll() Method in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Set addAll() Method in Java

Last Updated : 11 Jul, 2025

In Java, the addAll() method of the Set class is used to add all the elements of a specified collection to the current collection. The elements are added randomly without following any specific order.

Example 1: This example demonstrates how to merge two TreeSet using the addAll() method.


Output
Initial Set: [A, B, C]
Final Set: [A, B, C, D, E]

Syntax of addAll() Method

boolean addAll(Collection<? extends E> c)

  • Parameter: The method takes a collection as an argument, which can be any subclass of the Collection interface.
  • Return Type: This method returns "true" if the element were added successfully, otherwise returns "false".

Example 2: This example demonstrates how to append elements from an ArrayList to a TreeSet using the addAll() method.


Output
Initial Set: [100, 200, 300]
Final Set: [100, 200, 300, 400, 500, 600]
Comment