Question

I need help with a Java question... A) Create the tree map that should store following...

I need help with a Java question...

A)

Create the tree map that should store following strings.

Hello

Today

Is

Monday

Once you stored all the data in the tree map, then loop thru the tree map and remove things

B)

Then, Create a set that stores the folling strings.

-This
-Class
-is
-good
-We
-have
-learned
-lot
-of
-things.

Once you stored all the data in the set/map, then loop thru the set using iterator and remove all the strings that starts with either C or o.
And print the entire set as the single statement.

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

// TEXT CODE

import java.util.*;

public class Solution {

    public static void main(String[] args) {

        // Solution for part A.

        // creating a tree map, key is String and value is Integer
        TreeMap<String, Integer> treeMap = new TreeMap<>();

        // now put the given String into map
        treeMap.put("Hello", 0);
        treeMap.put("Today", 1);
        treeMap.put("Is", 2);
        treeMap.put("Monday", 3);

        // now loop through the treeMap
        for(Map.Entry<String, Integer> mapEntry: treeMap.entrySet()){

            // delete entry "Today"one by one
            if(mapEntry.getKey().equals("Today"))
                treeMap.remove("Today");
        }

        // now loop through the treeMap an print the remaining entries
        System.out.println("TreeMap entries after removing 'Today' is:");
        for(Map.Entry<String, Integer> mapEntry: treeMap.entrySet()){

            System.out.print(mapEntry.getKey() + " ");
        }


        // Solution for part B.


        // Create set using LinkedHashSet as child class
        // LinkedHashMap maintains the insertion order
        // add the given string
        Set<String> set = new LinkedHashSet<>(Arrays.asList(new String[]{"This", "Class", "is", "good", "We", "have", "learned,",
                "lot", "of", "things"}));

        // loop through the set using iterator
        Iterator iterator = set.iterator();
        while (iterator.hasNext()){
            // remove all String that starts with "C" or "o"
            String val = (String) iterator.next();
            if(val.charAt(0) == 'C' || val.charAt(0) == 'o'){
                // remove this val from set
                iterator.remove();
            }
        }

        // now print the set in single statement
        System.out.println("\n\nFinal set is: " + set);



    }
}

// SCREENSHOTS

// CODE

// OUTPUT

Add a comment
Know the answer?
Add Answer to:
I need help with a Java question... A) Create the tree map that should store following...
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
  • "Please Solve Problem 2 Please" Program 1(Total Point 15): You will use scanner class and ask...

    "Please Solve Problem 2 Please" Program 1(Total Point 15): You will use scanner class and ask users to enter numbers from 0 to 10 numbers. You will then extract only non prime numbers and store it in the set. You will then print the entire set contents. Problem 2(Total Point 15): Write a Java program that performs following things. Generate 10 random numbers Store all the numbers into the set Use the iterator or any other class to extract all...

  • Hello, I need some help creating this class in Java. This is for a larger project....

    Hello, I need some help creating this class in Java. This is for a larger project. I am using Eclipse if that helps 1. Create a class named State that will store information about a state and provide methods to get, and set the data, and compare the states by several fields. a. Fields: Name, Capital City, Abbreviation, Population, Region, US House Seats b. Constructor c. Get and set methods for each field d. Compare method to compare State objects...

  • I need help parsing a large text file in order to create a map using Java....

    I need help parsing a large text file in order to create a map using Java. I have a text file named weather_report.txt which is filled with hundreds of different indexes. For example: one line is "POMONA SUNNY 49 29 46 NE3 30.46F". There are a few hundred more indexes like that line with different values in the text file and they are not delimited by commas but instead by spaces. Therefore, in this list of indexes we only care...

  • I should use the array and loop to create a java program according to the instruction,...

    I should use the array and loop to create a java program according to the instruction, but I have no idea how to do it. Introduction This lab assignment continues to give you practice using loops, particularly loops with variable termination conditions, and it also provides you an opportunity to use one-dimensional arrays. Recall that an array is used to store a collection of data. The data can be values of Java primitive data types or else objects (for instance,...

  • java Part 1 Create a NetBeans project that asks for a file name. The file should...

    java Part 1 Create a NetBeans project that asks for a file name. The file should contain an unknown quantity of double numeric values with each number on its own line. There should be no empty lines. Open the file and read the numbers. Print the sum, average, and the count of the numbers. Be sure to label the outputs very clearly. Read the file values as Strings and use Double.parseDouble() to convert them. Part 2 Create a NetBeans project...

  • really need help. All information that i have is posted, In java Dynamic programming allows you...

    really need help. All information that i have is posted, In java Dynamic programming allows you to break a large problem into multiple subproblems. Each of these subproblems must be solved, and the solution must be stored as a memory-based solution. Solve the following binary search algorithm using dynamic programming (Adapted from Esquivalience, 2018): Graph To solve this problem, complete the following tasks: Create a binary search tree using a data structure. Insert the node values as described in the...

  • I need help with this coding. A. Create ArrayIntStack.java (one that's REALLY O(constant), totally!!!) B. Create...

    I need help with this coding. A. Create ArrayIntStack.java (one that's REALLY O(constant), totally!!!) B. Create the Javadoc web page (separate Assignment #15b). A. Create your own Stack Class, call it ArrayIntStack.java and write the standard four public Stack methods as listed below (this is the easy part of the assignment). Use at least one private helper method, maybe to ensure capacity of the array. All methods in your ArrayIntStack must have O(constant) run-time. Your ArrayIntStack extends no other Classes...

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

  • Write a simple Java program with the following naming structure: Open Eclipse Create a workspace called...

    Write a simple Java program with the following naming structure: Open Eclipse Create a workspace called hw1 Create a project called hw1 (make sure you select the “Use project folder as root for sources and class files”) Create a class called Hw1 in the hw1 package (make sure you check the box that auto creates the main method). Add a comment to the main that includes your name Write code that demonstrates the use of each of the following basic...

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