Avatar of Duke001
Duke001Flag for United Kingdom of Great Britain and Northern Ireland

asked on 

java extract key from map and creating sorted set

Hi experts,

I am a novice in Java and I would like to extract the values of a map and add them into a sorted set.

Map<String, List<String>> xxx

Thanks

Duke001
JavaProgramming

Avatar of undefined
Last Comment
CEHJ
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Try
SortedSet<List<String>> set = new TreeSet<List<String>>(xxx.values());

Open in new window

Avatar of Duke001
Duke001
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

i get the following error message:
Semantic error: java.util.ArrayList cannot be cast to java.lang.Comparable
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

OK. How are you envisaging the sort is meant to work?
Avatar of Duke001
Duke001
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

I would like to create a set in alphabetical order, without duplication.
Thanks
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

But your values are already Lists of Strings ...
Avatar of Duke001
Duke001
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Please find attached the method used to populate the List<String> value of the map.
thanks
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Nothing attached...
Avatar of Duke001
Duke001
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

very strange ... but please have a look now.
method.txt
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You need to post the real code - that would neither work nor even compile
Avatar of Duke001
Duke001
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

please find attached all methods.
Problem2.txt
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

OK. I suspect you're not quite asking for the right thing. You can't really have a SortedSet of Lists of Strings, since they wouldn't really be orderable. If you think otherwise, please tell me how they are to be ordered.

Are you sure you don't just want to eliminate duplicates from something you've got already?
Avatar of Duke001
Duke001
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

This is waht the question says:

"Write a public instance method for EmergencyManager called
allDrivers(), with no argument, that, based on the data available in
qualifiedDrivers, extracts, assembles and returns a collection of a
suitable type that itemizes all of the volunteer drivers in alphabetical
order, without duplication. Note that this method does not need to used
by any other method of EmergencyManager."

Thanks
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Duke001
Duke001
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

error: "cannot find symbol - variable whoDrivesWhat
Avatar of Duke001
Duke001
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Sorry,  but having read the question:

"based on the data available in qualifiedDrivers" ... am wrong to say that the information must be retrieved from the map and not from the whoDrivesWhat?

Thanks
Avatar of Duke001
Duke001
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

I have compiled the code with the "String[][] whoDrivesWhat" content  in it and it works but I think that is against the assumption of the question.

Thanks
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Well i'm not sure of that. It will certainly give you 'all of the volunteer drivers in alphabetical
order, without duplication'. If that is not what's required, then the method name 'allDrivers' is a bad one
Avatar of Duke001
Duke001
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Ok, I am going to accept your suggestion and let see what happens.
Thanks for your support.

Duke001
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

:)
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo