Write a java application that removes duplicates from an array using HashSet and Treeset in Java

Problem:

Write a java application that removes duplicates from an array using HashSet and Treeset in Java

Output:

[Blue, Red, Green, Orange]
[Blue, Green, Orange, Red]
HeadSet: [Blue, Green]
TailSet: [Orange, Red]
First: Blue
Last: Red

Solution:

import java.util.Arrays;
import java.util.HashSet;
import java.util.TreeSet;

public class RemovingDuplicates {

 public static void main(String[] args) {
  String[] colors = {"Red", "Red", "Red", "Green", "Blue", 
    "Green", "Blue", "Orange"
  };
  
  HashSet<String> colorsAsHashSet = 
    new HashSet<>(Arrays.asList(colors));
    
  System.out.println(colorsAsHashSet);
  
  TreeSet<String> colorsAsTreeSet = 
    new TreeSet<>(Arrays.asList(colors));
  System.out.println(colorsAsTreeSet);
  
  System.out.println("HeadSet: " + 
    colorsAsTreeSet.headSet("Orange"));
  
  System.out.println("TailSet: " + 
    colorsAsTreeSet.tailSet("Orange"));
  
  System.out.println("First: " + colorsAsTreeSet.first());
  System.out.println("Last: " + colorsAsTreeSet.last());
 }

}

1 comment:

  1. Liposculpture female Korea Korea's #1 Liposculpture Clinic. Lydian plastic surgery is the home of VIP patients. Celebrities, Influencers and Diplomats all know and trust Doctor An and Lydian plastic surgery clinic to provide detailed results.

    ReplyDelete