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...

  • 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...

  • 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...

  • 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...

  • can you solve it in java please Create the following: 1. Class Invoice ( the node...

    can you solve it in java please Create the following: 1. Class Invoice ( the node ) that includes three instance variables:     int No; // the Invoice No             String CustName; // the Customer name             int Amount; // the Invoice Amount Invoice next; // points to the next Invoice Default and overloaded constructors 2. Class Shop that includes three instance variables: Invoice head; Invoice Tail; Your class should have the following: • A method that initializes the instance variables....

  • For this question,  we want to implement the basic java classes to support the concept of an...

    For this question,  we want to implement the basic java classes to support the concept of an Online Store and a shopping cart. Consider an e-store with various types of items: books, flowers, gift cards, etc... and we like to be able to support the ability to handle a shopping cart that can contain various types of items. For this question you are asked to create the following classes: abstract class: “Item” every item has a unique item_id (a positive integer,...

  • USING JAVA, Use the binary search algorithm to create a game where you think of the...

    USING JAVA, Use the binary search algorithm to create a game where you think of the number and the computer guesses it. The computer should be able to guess any number between 1 and 500. When the computer chooses a number, you need to tell it if the guess is too high or too low. Have the program print out the number of guesses that it took to guess the number.

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
ADVERTISEMENT