Why cant i do this?
Map m = new HashMap;
Set s = new TreeSet(m); // i get error
Set s = new TreeSet(m.entrySet()); // this compiles but i get a run-time
error
Whats with these above errors?
Set s = new TreeSet(m.keySet()); // this works fine, but what if i want both
keys + values?
Regards
Alana
Another question:
why do you use Map.Entry? entrySet & Map.Entry seem similar. Can anyone
clarify?
Map.Entry is an interface nested in Map. entrySet is a method that
returns a Set whose elements can be cast to type Map.Entry.
Patricia
Sted Alana wrote:
> Hi
>
> Why cant i do this?
>
> Map m = new HashMap;
> Set s = new TreeSet(m); // i get error
There is no TreeSet constructor that accepts a Map as parameter. A Map
is not a Set.
> Set s = new TreeSet(m.entrySet()); // this compiles but i get a run-time
> error
A TreeSet created without a specified Comparator needs to be able to
cast the element references to Comparable, so that it can put them in order.
If you don't care about the order of the elements, you could use a
different Set implementation instead of TreeSet.
If you do care about the order, one option would be to write a
Comparator that implements the order you want them in. Supply the
Comparator on the TreeSet constructor call and it will no longer need to
cast the element references to Comparable. After creating the TreeSet
with Comparator you can use addAll to add the elements of m.entrySet().
>
> Set s = new TreeSet(m.keySet()); // this works fine, but what if i want both
> keys + values?
Given a key, you can use m.get(key) to get the corresponding value. If
it works, presumably the keys are Comparable - you could use that fact
in writing a Comparator for an entrySet based TreeSet.
Patricia
Should be:
Map m = new HashMap();
> Set s = new TreeSet(m); // i get error
TreeSet has 4 constructors; one with no parameters, one which takes a
Collection, one which takes a Comparator, and one that takes a
SortedSet. Since Map is not a Collection, Comparator or SortedSet,
you cannot pass a Map object to the constructor of TreeSet.
> Set s = new TreeSet(m.entrySet()); // this compiles but i get a run-time
> error
You're getting a runtime error on *this* line? It looks fine to me.
> Whats with these above errors?
>
> Set s = new TreeSet(m.keySet()); // this works fine, but what if i want both
> keys + values?
Set s = new TreeSet( m.entrySet() );
for ( Iterator iter = s.iterator(); iter.hasNext(); )
{
Map.Entry entry = (Map.Entry)iter.next();
System.out.println(
"Key = " + entry.getKey().toString()
+ ", Value = " + entry.getValue().toString() );
}
--
Geoff Rimmer <> geoff....@sillyfish.com <> www.sillyfish.com
www.sillyfish.com - Make savings on your BT and Telewest phone calls.
16/08/2002 - New Personal Savings Chart, and new Tiscali and SkyTalk rates.
1. entrySet() is a method in interface Map.
2. Map.Entry is an interface.
They are linked as follows:
entrySet() returns a Set of Map.Entry objects
Geoff Rimmer wrote:
> "Sted Alana" <Sted_...@hotmail.com> writes:
...
>> Set s = new TreeSet(m.entrySet()); // this compiles but i get a run-time
>> error
>
> You're getting a runtime error on *this* line? It looks fine to me.
...
The error should be coming from inside TreeSet, but is to be expected.
The TreeSet constructor that is being used requires the elements of the
Collection to be Comparable, with compareTo methods that will let them
be compared to each other.
Map.Entry does not extend Comparable, and there is no reason to expect
its implementing classes to implement Comparable.
This program:
import java.util.*;
class Test{
public static void main(String[] args) throws Exception{
Map m = new HashMap();
m.put("aaa","bbb");
m.put("ccc","d");
Set s = new TreeSet(m.entrySet());
}
}
throws:
java.lang.ClassCastException: java.util.HashMap$Entry
at java.util.TreeMap.compare(TreeMap.java:1081)
at java.util.TreeMap.put(TreeMap.java:459)
at java.util.TreeSet.add(TreeSet.java:205)
at java.util.AbstractCollection.addAll(AbstractCollection.java:315)
at java.util.TreeSet.addAll(TreeSet.java:251)
at java.util.TreeSet.<init>(TreeSet.java:138)
at Test.main(Test.java:8)
Patricia
Thanks for clarifying this, Patricia. When using TreeSet, I normally
use the constructor that takes a Comparator, meaning that there is no
restriction on the elements I add to the set.
Unfortunately it doesn't look like Generics will help move this type
of error from runtime to compile time, since the elements of a
SortedSet only need to implement Comparable if no Comparator has been
specified, so we cannot define SortedSet to be:
public interface SortedSet<E extends Comparable> { }
Shame really.
I am positive I get a run-time error. Try it yourself.
Patricia explaination for this was buried deep in her message, which i was
not able to understand at all.
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.384 / Virus Database: 216 - Release Date: 21/08/2002
I assume a Map.Entry object are a pair of key-values
Sorry about that. How much do you know about the Comparable and
Comparator interfaces? Have you read the API documentation for TreeSet,
especially the constructor you are using?
Patricia
Correct.
>Patricia explaination for this was buried deep in her message, which i was
>not able to understand at all.
see http://mindprod.com/jglosscomparable.html
http://mindprod.com/jglosscompare.html
http://mindprod.com/jglosscomparator.html
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, contract programming for $50 US per hour or fixed price.
For rapid answers to Java questions, see the glossary at http://mindprod.com/jgloss.html
I confess I dont know much about Comparable and Comparator interfaces as I
have only used them once or twice. But do read the API documentation
whenever im stuck, but it doesnt always help.
Sun's tutorial on "object ordering" was a little helpful.
You don't need to know *too* much. You just need to know this: TreeSet
keeps its members sorted so that they can be retrieved in order. For
this to happen, there needs to be some definition for "in order".
In some cases, there's a "natural ordering"... that is, a common sense
order that objects get arranged in. That's obviously the case, for
example, with classes like java.lang.Integer; and to some extent, for
classes like java.lang.String, since strings are conventionally sorted
alphabetically. These classes implement an interface called Comparable,
and provide a method called compareTo, which allows them to compare
themselves to another object.
In other cases, you need to specify some kind of order. You can do that
with a Comparator. That's a special object that exists to define some
ordering of other objects.
If you need to keep objects in order, they either need to implement
Comparable or you need to give the TreeSet a Comparator. If you *don't*
need to keep them in order, then just drop the whole thing and use
HashSet instead.
Chris Smith