java extract key from map and creating sorted set

Duke001
Duke001 used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

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

Open in new window

Author

Commented:
i get the following error message:
Semantic error: java.util.ArrayList cannot be cast to java.lang.Comparable
Top Expert 2016

Commented:
OK. How are you envisaging the sort is meant to work?
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
I would like to create a set in alphabetical order, without duplication.
Thanks
Top Expert 2016

Commented:
But your values are already Lists of Strings ...

Author

Commented:
Please find attached the method used to populate the List<String> value of the map.
thanks
Top Expert 2016

Commented:
Nothing attached...

Author

Commented:
very strange ... but please have a look now.
method.txt
Top Expert 2016

Commented:
You need to post the real code - that would neither work nor even compile

Author

Commented:
please find attached all methods.
Problem2.txt
Top Expert 2016

Commented:
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?

Author

Commented:
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
Top Expert 2016
Commented:
Yes, that's quite a different thing. All you need do is add all volunteer drivers' names (including those held in arrays/List instances) to the Set, e.g.

    public SortedSet<String> allDrivers() {
	SortedSet<String> result = new TreeSet<String>();
	for(String[] array : whoDrivesWhat) {
	    result.addAll(Arrays.asList(array));
	}
	return result;
    }

Open in new window

Author

Commented:
error: "cannot find symbol - variable whoDrivesWhat

Author

Commented:
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

Author

Commented:
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
Top Expert 2016

Commented:
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

Author

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

Duke001
Top Expert 2016

Commented:
:)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial