1

How to Merge Two TreeSets into One while Preserving Natural Order?

 1 month ago
source link: https://www.geeksforgeeks.org/merge-two-treesets-into-one-while-preserving-natural-order/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to Merge Two TreeSets into One while Preserving Natural Order?

Last Updated : 21 Feb, 2024

In Java, TreeSetis a pre-defined class that implements the Setinterface, and it provides a sorted set of elements with no duplicates. This uses a Red-Black Tree data structure for storage. It is a part of the java.util package.

Key Terminologies:

  • Set: A set can be defined as a collection that contains unique elements. The Set interface can be implemented in the TreeSet class.
  • Comparator: The comparator is a pre-defined interface, and it is used to define the custom ordering for objects it can be provided custom ordering with specified rules by the comparator.

Syntax of Creating a TreeSet:

TreeSet<Type> treeSet = new TreeSet<>();

Step-by-Step Implementation:

  • Create the class named GfGMergeTreeSets and write the main method into the class.
  • Now, create the two tree sets and add the elements into the tree sets.
  • Create one more TreeSet with one TreeSet element, then add another tree element into it.
  • Print the resultant tree set.

Below is the Program to Merge Two TreeSets into One while Preserving Natural Order:

// Java program to merge two TreeSets
// Into one while preserving natural order
import java.util.TreeSet;
// Driver Class
public class GfGMergeTreeSets 
{
// Main Function
public static void main(String args[]) 
{
// Create two TreeSets
TreeSet<Integer> set1 = new TreeSet<>();
// Adding the elements into the set1
set1.add(5);
set1.add(25);
set1.add(15);
TreeSet<Integer> set2 = new TreeSet<>();
// Adding the elements into the set1
set2.add(3);
set2.add(8);
set2.add(12);
// Merge the two TreeSets
TreeSet<Integer> mergedSet = new TreeSet<>(set1);
mergedSet.addAll(set2);
System.out.println("Merged TreeSet: " + mergedSet);
}
}
Output
Merged TreeSet: [3, 5, 8, 12, 15, 25]

Explanation of the above Program:

  • The above Java program is the example of merging two tree sets into one while preserving natural order.
  • We have created two tree sets and added the elements to the two tree sets.
  • After that, we have created one more tree set with the set1 element and added the set2 using the addAll() method.
  • A last, all elements are merged and stored in the sorted order of the tree set.

Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:

  • Comprehensive Course
  • Expert Guidance for Efficient Learning
  • Hands-on Experience with Real-world Projects
  • Proven Track Record with 100,000+ Successful Geeks
Like Article
Suggest improvement
Share your thoughts in the comments

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK