Question

Create three classes in Java using either treeSet or hashSet implementation where users can able to...

Create three classes in Java using either treeSet or hashSet implementation where users can able to add items, delete items, getItems, and search items in the list or from the list.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Java code to demonstrate

// the working of TreeSet

import java.util.*;

class TreeSetDemo {

    public static void main(String[] args)

    {

        TreeSet<String> ts

            = new TreeSet<String>();

        // Elements are added using add() method

        ts.add("Geek");

        ts.add("For");

        ts.add("Geeks");

        System.out.println(ts);

    }

}

Accessing the Elements: After adding the elements, if we wish to access the elements, we can use inbuilt methods like contains(), first(), last(), etc.

filter_none

edit

play_arrow

brightness_4

// Java code to demonstrate

// the working of TreeSet

import java.util.*;

class TreeSetDemo {

    public static void main(String[] args)

    {

        TreeSet<String> ts

            = new TreeSet<String>();

        // Elements are added using add() method

        ts.add("Geek");

        ts.add("For");

        ts.add("Geeks");

        System.out.println("Tree Set is " + ts);

        String check = "Geeks";

        // Check if the above string exists in

        // the treeset or not

        System.out.println("Contains " + check

                           + " " + ts.contains(check));

        // Print the first element in

        // the TreeSet

        System.out.println("First Value " + ts.first());

        // Print the last element in

        // the TreeSet

        System.out.println("Last Value " + ts.last());

        String val = "Geek";

        // Find the values just greater

        // and smaller than the above string

        System.out.println("Higher " + ts.higher(val));

        System.out.println("Lower " + ts.lower(val));

    }

}

Removing the Values: The values can be removed from the TreeSet using the remove() method. There are various other methods which are used to remove the first value or the last value.

filter_none

edit

play_arrow

brightness_4

// Java code to demonstrate

// the working of TreeSet

import java.util.*;

class TreeSetDemo {

    public static void main(String[] args)

    {

        TreeSet<String> ts

            = new TreeSet<String>();

        // Elements are added using add() method

        ts.add("Geek");

        ts.add("For");

        ts.add("Geeks");

        ts.add("A");

        ts.add("B");

        ts.add("Z");

        System.out.println("Initial TreeSet " + ts);

        // Removing the element b

        ts.remove("B");

        System.out.println("After removing element " + ts);

        // Removing the first element

        ts.pollFirst();

        System.out.println("After removing first " + ts);

        // Removing the last element

        ts.pollLast();

        System.out.println("After removing last " + ts);

    }

}

/ Java code to demonstrate

// the working of TreeSet

import java.util.*;

class TreeSetDemo {

    public static void main(String[] args)

    {

        TreeSet<String> ts

            = new TreeSet<String>();

        // Elements are added using add() method

        ts.add("Geek");

        ts.add("For");

        ts.add("Geeks");

        ts.add("A");

        ts.add("B");

        ts.add("Z");

        // Iterating though the TreeSet

        for (String value : ts)

            System.out.print(value

                             + ", ");

        System.out.println();

    }

}

##That is all about your answer............please upvote my answer..........please.........

Add a comment
Know the answer?
Add Answer to:
Create three classes in Java using either treeSet or hashSet implementation where users can able to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program in Java using MVC (Model, View, Controller) architecture using either HashSet or TreeSet...

    Write a program in Java using MVC (Model, View, Controller) architecture using either HashSet or TreeSet implementation where users can able to getItem, searchItem, removeItem, and addItem to the list.

  • Objective: Read in the names of several grocery items from the keyboard and create a shopping...

    Objective: Read in the names of several grocery items from the keyboard and create a shopping list using the Java ArrayList abstract data type. 1) Create a new empty ArrayList 2) Ask the user for 5 items to add to a shopping list and add them to the ArrayList (get from user via the keyboard). 3) Prompt the user for an item to search for on the list. Output a message to the user letting them know whether the item...

  • what is the solution for this Java project? Create the an application that can use different...

    what is the solution for this Java project? Create the an application that can use different types of Linked List to manage either Checking Account or Saving Account. The application should allow users can select the type of Linked List to work on. After finishing one, users can select to work with other type of Linked List until they want to exit Singly Linked List Singly Linked List with Iterator Java Linked List with Iterator 1. 2. 3. You can...

  • For this week's lab, you will use two of the classes in the Java Collection Framework:...

    For this week's lab, you will use two of the classes in the Java Collection Framework: HashSet and TreeSet. You will use these classes to implement a spell checker. Set Methods For this lab, you will need to use some of the methods that are defined in the Set interface. Recall that if set is a Set, then the following methods are defined: set.size() -- Returns the number of items in the set. set.add(item) -- Adds the item to the...

  • For this week's lab, you will use two of the classes in the Java Collection Framework:...

    For this week's lab, you will use two of the classes in the Java Collection Framework: HashSet and TreeSet. You will use these classes to implement a spell checker. Set Methods For this lab, you will need to use some of the methods that are defined in the Set interface. Recall that if set is a Set, then the following methods are defined: set.size() -- Returns the number of items in the set. set.add(item) -- Adds the item to the...

  • I need help on Java Swing, how to create a Java Swing program that can able...

    I need help on Java Swing, how to create a Java Swing program that can able to Display all the name list from the text file then Display the Longest name...

  • Need a help with this one FoodListInterface.java specifies a Java interface for an ADT that maintains...

    Need a help with this one FoodListInterface.java specifies a Java interface for an ADT that maintains a list of foods, dishes or meals. This interface includes two operations: add(String food) – add the name of a food, dish or meal to the list onTheMenu(String food) – check if a given food item is on the list Develop three implementations of this interface, each with a different underlying data structure: Array Linked list ArrayList from the java.util package (a Java Collections...

  • using angular or java, VC code. customers should be able to request for service online Question...

    using angular or java, VC code. customers should be able to request for service online Question is asking to create an application that tenants can go online and request for service. Eg if the A/C is not working, the tenant can go online and request a service to have the A/C fixed create a management system application or an app that landloard can use to manage their rental properties. This application should contain Service requests that a tenant can go...

  • java Create the following classes: DatabaseType: an interface that contains one method 1. Comparator getComparatorByTrait(String trait)...

    java Create the following classes: DatabaseType: an interface that contains one method 1. Comparator getComparatorByTrait(String trait) where Comparator is an interface in java.util. Database: a class that limits the types it can store to DatabaseTypes. The database will store the data in nodes, just like a linked list. The database will also let the user create an index for the database. An index is a sorted array (or in our case, a sorted ArrayList) of the data so that searches...

  • You are asked to build and test the following system using Java and the object-oriented concepts ...

    You are asked to build and test the following system using Java and the object-oriented concepts you learned in this course: Project (20 marks) CIT College of Information technology has students, faculty, courses and departments. You are asked to create a program to manage all these information's. Create a class CIT to represents the following: Array of Students, each student is represented by class Student. Array of Faculty, each faculty is represented by class Faculty. Array of Course, each course...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT