TreeSet - Bug Reaper

                  Bug Reaper

Lean about Automation Testing,Selenium WebDriver,RestAssured,Appium,Jenkins,JAVA,API Automation,TestNG,Maven, Rest API, SOAP API,Linux,Maven,Security Testing,Interview Questions

Saturday, 7 March 2020

TreeSet

TreeSet is implementation of Set interface

We have other implementation  as well like HashSet and LinkedHashset.

We use HashSet if we don't need any order
LinkedHasSet we use when we need to maintain insertion order.

But why we need TreeSet.....?

If you want elements to be ordered according to some Comparator, then use TreeSet.

TreeSet Example With No Comparator 

Class

package TreeSetWithComparator;

import java.util.TreeSet;

public class TreeSetWithoutComparator {
    public static void main(String[] args) {

        //Elements will be saved in ascending order
        TreeSet<Integer> treeSet=new TreeSet<>();
        treeSet.add(11);
        treeSet.add(23);
        treeSet.add(5);
        treeSet.add(18);
        treeSet.add(15);

        System.out.println(treeSet);
    }

}


Output

[5, 11, 15, 18, 23] // ascending order

Class

MyComparator

package TreeSetWithComparator;

import java.util.Comparator;

public class MyComparator implements Comparator<Employee> {
    @Override
    public int compare(Employee e1, Employee e2) {

        if(e1.name == e2.name) // here if name is equal then skip 
        {
            return 0;
        }
        else
        return e2.salary-e1.salary; // here e2-e1 not e1-e2
    }

}

Employee Class With TreeSet With Comparator

Find Employee with Highest Salary and Remove Duplicate Enteries

package TreeSetWithComparator;

import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.TreeSet;

public class Employee {
    String name;
    int salary;

    //Creating Constructor
    public Employee(String name, int salary) {

        this.name = name;
        this.salary = salary;
    }

    /*
    The toString() method returns the string representation of the object.
    If you print any object, java compiler internally invokes the toString() method on the object.
     So overriding the toString() method, returns the desired output, it can be the state of an object
     */
    @Override
    public String toString() {
        return name + " and " + salary;
    }

    public static void main(String[] args) {
        Employee employee = new Employee("Neeraj", 400000);
        System.out.println(employee);

        MyComparator comparator = new MyComparator();

        TreeSet<Employee> treeSet = new TreeSet<Employee>(comparator);

        treeSet.add(new Employee("AAA", 500));
        treeSet.add(new Employee("EEE", 1000)); //Highest Salary
        treeSet.add(new Employee("BBB", 100));
        treeSet.add(new Employee("AAA", 100));// Duplicate
        treeSet.add(new Employee("CCC", 100));
        treeSet.add(new Employee("CCC", 100));//Duplicate
        treeSet.add(new Employee("AAA", 100));//Duplicate
        treeSet.add(new Employee("BBB", 100));//Duplicate
        treeSet.add(new Employee("DDD", 900));//Second Highest Salary

        //Output will have max salary Employee on top and Duplicate Employees would be removed //keeping one entry of employee only other duplicate enteries would be removed
        System.out.println(treeSet);

    }
}


Output

Neeraj and 400000

[EEE and 1000, DDD and 900, AAA and 500, BBB and 100]

No comments:

Post a Comment