SortedSet addAll() method in Java with Examples8 May 2025 | 3 min read A SortedSet's addAll() method preserves the set's uniqueness and order by adding every element from a given collection. All of the elements from the specified collection are appended to the current set using the addAll(Collection C) function. There is no set order in which the elements are inserted; they are added at random. Technically, when addAll() is called, the add() function is used to iterate through the elements of the supplied collection and add each one to the set one at a time. The insertion operation maintains the items' natural order or custom comparator-defined order since SortedSet implementations, such as TreeSet, depend on a self-balancing binary search tree. Furthermore, as a set does not permit duplicates, duplicate elements in the collection are immediately ignored. Syntax: Parameters: The collection of any kind that is to be added to the set is the parameter C. Return Value: If the elements of the collection C are successfully appended to this Set, the process returns true; if not, it returns False. Example 1:The code shows how to use the addAll() method with a SortedSet that has been converted to a TreeSet. The TreeSet precludes duplication and guarantees that the components in the set are arranged in their lexicographical order, which is the set's natural order for strings. The addAll() method is used in this example to combine all of the members of set2 into set1 after two sets (set1 and set2) have been established. To keep the set unique, duplicate items like "World" are ignored. After the combination, the merged set is still sorted and contains every unique element from both sets. The sorted and unique attributes of a SortedSet are preserved while sets are efficiently combined using the addAll() method, as demonstrated by this code. Implementation:Output: The initial Set is given by: [TpointTech, TreeSet, Welcome, World, the, to] The Set after applying addAll() method: [Hello, TpointTech, TreeSet, Welcome, World, the, to] Example 2:The following example stores elements in a sorted way without duplicates using a SortedSet (implemented by TreeSet). We can include aspects from another collection (an ArrayList) in the set by using the addAll() method. All of the elements are entered in the proper sorted order when addAll() is called. SortedSet ensures uniqueness; therefore, duplicates between the set and the collection being added are avoided. The set's parts are consistently kept in ascending order thanks to the TreeSet. The code shows how addAll() facilitates the effective merging of another collection into the set while maintaining uniqueness and sorting. Implementation:Output: The initial Set is given by: [TpointTech, TreeSet, Welcome, World, the, to] The Set after applying addAll() method: [Hello, TpointTech, TreeSet, Welcome, Welcome!!!!, World, the, to] Next TopicRectangular Number in Java |
The java.text.ChoiceFormat is a class containing a getFormats() as a function. When the ChoiceFormat object is being initialized, the ChoiceFormat class is utilized to retrieve the connected format. It provides an array of the specified type. Syntax: public Object[] getFormats() Parameter: There are no parameters accepted by this...
2 min read
JavaBeans, a component architecture introduced by Sun Microsystems, has been a fundamental part of Java development for building reusable software components. Introspection is a key concept within JavaBeans, allowing developers to inspect and manipulate the properties, methods, and events of JavaBean components at runtime. In this...
4 min read
Among other optimisations in Java development programming the object pooling pattern serves to handle efficient object reuse management, especially in costly object life cycle events. The following article explains Java object creation techniques for establishing multiple pools of objects with variable sizes while discussing system...
5 min read
Java, a versatile and widely-used programming language, has evolved over the years, offering a rich set of features and functionalities. One of the key aspects that contribute to Java's popularity is its ability to cater to various application types. In this section, we will delve into...
4 min read
Finding the factor (divisor) that appears the most frequently among all of the array's elements is known as the Maximum Factor Score. We see the divisors of each integer in the array and tally how often they occur. The factor with the highest count determines the...
5 min read
Image processing is a significant aspect of computer vision and is widely used in various applications such as medical imaging, security, and multimedia. One of the fundamental operations in image processing is converting a colored image to a grayscale image. Grayscale images are simpler and...
4 min read
In this section, we will learn how to reverse a number in Java using while loop, for loop and recursion. To reverse a number, follow the steps given below: First, we find the remainder of the given number by using the modulo (%) operator. Multiply the variable reverse by...
4 min read
? In this article, we are going to learn what projects are in Java and how to create them in an Integrated Development Environment (IDE). JAVA projects topic will help us in getting better at using Java and making a working application using Java. Let us go...
4 min read
One of the most common computer games ever created is the Tetris game. Nowadays we can play this game on mobile too. The game was designed, as well as created by Alexey Pajitnov in 1985. He was a Russian Programmer. A lot of different variation has...
11 min read
What is a Java Agent? Java agents are instruments that can help to modify bytecode since they run concurrently with a Java program. These agents can be attached with the JVM by using the -javaagent option that enable them to intercept ClassLoaders and perform transformation to the...
4 min read
We request you to subscribe our newsletter for upcoming updates.
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