NavigableSet addAll() method in Java
The java.util.NavigableSet.addAll(Collection C) method is used to append all of the elements from the mentioned collection to the existing NavigableSet. The elements are added randomly without following any specific order.
Syntax:
boolean addAll(Collection C)
Parameters: The parameter C is a collection of any type that is to be added to the NavigableSet.
Return Value: The method returns true if it successfully appends the elements of the collection C to this NavigableSet otherwise it returns False.
Note: The addAll() method of NavigableSet interface in Java is inherited from the Set interface.
Below programs illustrate the Java.util.NavigableSet.addAll() method:
Program 1 : Appending a tree NavigableSet.
import java.io.*;
import java.util.*;
public class TreeNavigableSetDemo {
public static void main(String args[])
{
NavigableSet<String> st1 = new TreeSet<String>();
st1.add( "Welcome" );
st1.add( "To" );
st1.add( "Geeks" );
st1.add( "4" );
st1.add( "Geeks" );
st1.add( "TreeNavigableSet" );
System.out.println( "NavigableSet: " + st1);
NavigableSet<String> st2 = new TreeSet<String>();
st2.add( "Hello" );
st2.add( "World" );
st1.addAll(st2);
System.out.println( "NavigableSet: " + st1);
}
}
|
Output:
NavigableSet: [4, Geeks, To, TreeNavigableSet, Welcome]
NavigableSet: [4, Geeks, Hello, To, TreeNavigableSet, Welcome, World]
Program 2 : Appending an ArrayList.
import java.io.*;
import java.util.*;
public class NavigableSetDemo {
public static void main(String args[])
{
NavigableSet<String> st1 = new TreeSet<String>();
st1.add( "Welcome" );
st1.add( "To" );
st1.add( "Geeks" );
st1.add( "4" );
st1.add( "Geeks" );
st1.add( "NavigableSet" );
System.out.println( "Initial NavigableSet: " + st1);
ArrayList<String> collect = new ArrayList<String>();
collect.add( "A" );
collect.add( "Computer" );
collect.add( "Portal" );
st1.addAll(collect);
System.out.println( "Final NavigableSet: " + st1);
}
}
|
Output:
Initial NavigableSet: [4, Geeks, NavigableSet, To, Welcome]
Final NavigableSet: [4, A, Computer, Geeks, NavigableSet, Portal, To, Welcome]
Last Updated :
30 Sep, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...