Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java TreeSets -You MUST use java.util.TreeSet. -Build a set of unique Strings -I

ID: 3682375 • Letter: J

Question

Java TreeSets

-You MUST use java.util.TreeSet. -Build a set of unique Strings -Iterate over the contents of the set. -Print the values in ascending length order. -If 2 or more strings are the same length, print them in REVERSE lexicographical order. - you will need to pass a custom Comparator instance to your TreeSet constructor. -Make sure to implement this using an inner class, such that the entirety of Program 1 is in a single java file. -You can choose any String values -Program does not have to be interactive. -Make sure to show what happens when duplicate strings and multiple strings with the same lengths are included.
Here is an example of what your program should do:
public static void main(String[] args) { TreeSet<String> tester = new TreeSet<>(new LenComp()); tester.add("alpha"); tester.add("b"); tester.add("i"); tester.add("sample"); tester.add("a"); tester.add("slack"); for (String s : tester) { System.out.println(s); } }
output: i b a slack alpha sample
Java TreeSets

-You MUST use java.util.TreeSet. -Build a set of unique Strings -Iterate over the contents of the set. -Print the values in ascending length order. -If 2 or more strings are the same length, print them in REVERSE lexicographical order. - you will need to pass a custom Comparator instance to your TreeSet constructor. -Make sure to implement this using an inner class, such that the entirety of Program 1 is in a single java file. -You can choose any String values -Program does not have to be interactive. -Make sure to show what happens when duplicate strings and multiple strings with the same lengths are included.
Here is an example of what your program should do:
public static void main(String[] args) { TreeSet<String> tester = new TreeSet<>(new LenComp()); tester.add("alpha"); tester.add("b"); tester.add("i"); tester.add("sample"); tester.add("a"); tester.add("slack"); for (String s : tester) { System.out.println(s); } }
output: i b a slack alpha sample Java TreeSets

-You MUST use java.util.TreeSet. -Build a set of unique Strings -Iterate over the contents of the set. -Print the values in ascending length order. -If 2 or more strings are the same length, print them in REVERSE lexicographical order. - you will need to pass a custom Comparator instance to your TreeSet constructor. -Make sure to implement this using an inner class, such that the entirety of Program 1 is in a single java file. -You can choose any String values -Program does not have to be interactive. -Make sure to show what happens when duplicate strings and multiple strings with the same lengths are included.
Here is an example of what your program should do:
public static void main(String[] args) { TreeSet<String> tester = new TreeSet<>(new LenComp()); tester.add("alpha"); tester.add("b"); tester.add("i"); tester.add("sample"); tester.add("a"); tester.add("slack"); for (String s : tester) { System.out.println(s); } }
output: i b a slack alpha sample
-You MUST use java.util.TreeSet. -Build a set of unique Strings -Iterate over the contents of the set. -Print the values in ascending length order. -If 2 or more strings are the same length, print them in REVERSE lexicographical order. - you will need to pass a custom Comparator instance to your TreeSet constructor. -Make sure to implement this using an inner class, such that the entirety of Program 1 is in a single java file. -You can choose any String values -Program does not have to be interactive. -Make sure to show what happens when duplicate strings and multiple strings with the same lengths are included.
Here is an example of what your program should do:
public static void main(String[] args) { TreeSet<String> tester = new TreeSet<>(new LenComp()); tester.add("alpha"); tester.add("b"); tester.add("i"); tester.add("sample"); tester.add("a"); tester.add("slack"); for (String s : tester) { System.out.println(s); } }
output: i b a slack alpha sample
-You MUST use java.util.TreeSet. -Build a set of unique Strings -Iterate over the contents of the set. -Print the values in ascending length order. -If 2 or more strings are the same length, print them in REVERSE lexicographical order. - you will need to pass a custom Comparator instance to your TreeSet constructor. -Make sure to implement this using an inner class, such that the entirety of Program 1 is in a single java file. -You can choose any String values -Program does not have to be interactive. -Make sure to show what happens when duplicate strings and multiple strings with the same lengths are included.
Here is an example of what your program should do:
public static void main(String[] args) { TreeSet<String> tester = new TreeSet<>(new LenComp()); tester.add("alpha"); tester.add("b"); tester.add("i"); tester.add("sample"); tester.add("a"); tester.add("slack"); for (String s : tester) { System.out.println(s); } }
output: i b a slack alpha sample

Explanation / Answer



import java.util.Comparator;
import java.util.TreeSet;

public class MyCompUserDefine {

    public static void main(String a[]){
        //By using name comparator (String comparison)
        TreeSet<String> tester = new TreeSet<String>(new MyNameComp());
        tester.add("alpha");
        tester.add("b");
        tester.add("i");
        tester.add("sample");
        tester.add("a");
        tester.add("slack");
        for(String e:tester){
            System.out.println(e);
        }
        System.out.println("===========================");
      

class MyNameComp implements Comparator<String>{

    @Override
    public int compare(String e1, String e2) {
        return ((Comparable<java.lang.String>) e1.getName()).compareTo(e2.getName());
    }
}



class String{
    
    private String name;
    
    public String(String n){
        this.name = n;
    }
    
    public java.lang.String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public java.lang.String toString(){
        return "Name: "+this.name;
    }
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote