Java Program to Compare TreeSet Collections

You are currently viewing Java Program to Compare TreeSet Collections

Java program to compare TreeSet collections

// Java program to compare TreeSet collections

import java.io.*;
import java.util.*;

public class Main {
  public static void main(String args[]) {
    TreeSet < Integer > tree1 = new TreeSet < Integer > ();
    TreeSet < Integer > tree2 = new TreeSet < Integer > ();
    TreeSet < Integer > tree3 = new TreeSet < Integer > ();

    tree1.add(10);
    tree1.add(20);
    tree1.add(30);
    tree1.add(40);
    tree1.add(50);

    tree2.add(10);
    tree2.add(60);
    tree2.add(30);
    tree2.add(40);
    tree2.add(50);

    tree3.add(10);
    tree3.add(20);
    tree3.add(30);
    tree3.add(40);
    tree3.add(50);

    if (tree1.equals(tree2))
      System.out.println("TreeSets tree1 and tree2 have similar elements.");
    else
      System.out.println("TreeSets tree1 and tree2 have not similar elements.");

    if (tree1.equals(tree3))
      System.out.println("TreeSets tree1 and tree3 have similar elements.");
    else
      System.out.println("TreeSets tree1 and tree3 have not similar elements.");
  }
}

Output

TreeSets tree1 and tree2 have not similar elements.
TreeSets tree1 and tree3 have similar elements.

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply