Question

3.       What Collection Framework classes can and can't do. Which of the following things can be done...

3.       What Collection Framework classes can and can't do. Which of the following things can be done by collection classes (List, Set, or Map)? If you can do it, outline how, mentioning relevant methods, else say that it is impossible and why.

  1. A program is designed to have a TreeMap<Customer, String>, but Customer has no compareTo method. Is it possible to get the TreeMap to work without changing Customer.java?
  2. You have a Map of Strings to Integers. You want to make a List of the key strings.
  1. You have a Map of Strings to Integers. You now want to use the same (String,Integer) pairs to make a Map of Integers to Strings, the inverse map. Is this always possible?
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Ans:

a) Yes , It is possible to get Treemap<Customer,String> to work by implementing compareTo() method inside an anonymous inner class .

eg: TreeMap<Customer,String> map = new TreeMap<>(new Comparator<Customer>() {

          public int compare(Customer C1 , Customer C2)

          {

               // Write comparison code here

           }

       });

b) Yes , It is possible to make a list of key strings from a map of string to integers by:

     List<String> list = map.keySet().stream().collect(Collectors.toList());

    It will make a list of Strings . For this we need to import java.util.stream.*;

c) No , It is not always possible because for a map the key should be unique but values may repeat . So , while reversing the map the repeated values will act as key which will produce error .

Add a comment
Know the answer?
Add Answer to:
3.       What Collection Framework classes can and can't do. Which of the following things can be done...
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
  • 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...

  • URGENT I have 3 hours for Submit this Homework so you can share yout codes with...

    URGENT I have 3 hours for Submit this Homework so you can share yout codes with me in 150mins Preliminary Notes: You have 4 hours to submit your solution. No late submission is accepted. Do not leave your submission to the last minute. You are not allowed to copy any code from anywhere. Your code will be checked by a software for similarity. Implement the code on your own and never share it with someone else. A test code is...

  • IN PYTHON Assignment Overview This assignment will give you experience on the use of classes. Understand...

    IN PYTHON Assignment Overview This assignment will give you experience on the use of classes. Understand the Application The assignment is to first create a class calledTripleString.TripleStringwill consist of threeinstance attribute strings as its basic data. It will also contain a few instance methods to support that data. Once defined, we will use it to instantiate TripleString objects that can be used in our main program. TripleString will contain three member strings as its main data: string1, string2, and string3....

  • Of the following four responses which do you feel is the most ethical and why? Why...

    Of the following four responses which do you feel is the most ethical and why? Why do you feel the way you di about HIV/AIDS prevention? Do an essay of 2 or 3 paragraphs pls and thank you A. Dr. Meadows is correct. We need to start with the least invasive approaches, such as awareness campaigns. Abstinence campaigns also present no cost to personal liberties, and should be part of that plan. There is no question that abstinence works; no...

  • Of the following four responses which do you feel is the most ethical and why? Why...

    Of the following four responses which do you feel is the most ethical and why? Why do you feel the way you di about HIV/AIDS prevention? Do an essay of 2 or 3 paragraphs pls and thank you A. Dr. Meadows is correct. We need to start with the least invasive approaches, such as awareness campaigns. Abstinence campaigns also present no cost to personal liberties, and should be part of that plan. There is no question that abstinence works; no...

  • Need Help! Please Solve using JAVA! TopResult Task: There are a number of situations in which we ...

    Need Help! Please Solve using JAVA! TopResult Task: There are a number of situations in which we want to keep track of the highest value we've seen so far - a highest score, a most recent entry, a name which is closest to the end (or the front) of a list, etc. We can imagine writing a simple class which will do this: a class which will let us enter new results one by one, and at any point will...

  • Please use Java only. Write the class TopResult, which keeps track of the best (highest numbered ...

    Please use Java only. Write the class TopResult, which keeps track of the best (highest numbered or highest ordered) result it has seen so far. The class will be a generic type, so the type of results it sees depends on how it is declared. TopResult Task: There are a number of situations in which we want to keep track of the highest value we've seen so far - a highest score, a most recent entry, a name which is...

  • Please use Java only. Write the class TopResult, which keeps track of the best (highest numbered ...

    Please use Java only. Write the class TopResult, which keeps track of the best (highest numbered or highest ordered) result it has seen so far. The class will be a generic type, so the type of results it sees depends on how it is declared. TopResult Task: There are a number of situations in which we want to keep track of the highest value we've seen so far - a highest score, a most recent entry, a name which is...

  • Assignment 5 will be way different. It will be more like what you will receive in...

    Assignment 5 will be way different. It will be more like what you will receive in a programming shop. By that I mean that some things are built for you and others you will need to create or finish. P.S. part of your grade will include: Did you add in the required files? Did you rename your project? does your linear searches work? Did you put your code in the correct modules? did you change modules that you weren't supposed...

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