Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Map + Set

0 views
Skip to first unread message

Sted Alana

unread,
Sep 8, 2002, 9:02:55 AM9/8/02
to
Hi

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

Sted Alana

unread,
Sep 8, 2002, 10:10:08 AM9/8/02
to

Another question:

why do you use Map.Entry? entrySet & Map.Entry seem similar. Can anyone
clarify?

Patricia Shanahan

unread,
Sep 8, 2002, 11:21:05 AM9/8/02
to

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

Patricia Shanahan

unread,
Sep 8, 2002, 12:00:22 PM9/8/02
to

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

Geoff Rimmer

unread,
Sep 8, 2002, 4:02:11 PM9/8/02
to
"Sted Alana" <Sted_...@hotmail.com> writes:
>
> Why cant i do this?
>
> Map m = new HashMap;

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.

Geoff Rimmer

unread,
Sep 8, 2002, 4:04:08 PM9/8/02
to
"Sted Alana" <Sted_...@hotmail.com> writes:
>
> Another question:
>
> why do you use Map.Entry? entrySet & Map.Entry seem similar. Can anyone
> clarify?

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

Patricia Shanahan

unread,
Sep 8, 2002, 4:54:08 PM9/8/02
to

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

Geoff Rimmer

unread,
Sep 9, 2002, 6:28:21 AM9/9/02
to
Patricia Shanahan <pa...@acm.org> writes:
>
> 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.

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.

Sted Alana

unread,
Sep 9, 2002, 7:07:25 AM9/9/02
to

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

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


Sted Alana

unread,
Sep 9, 2002, 7:11:05 AM9/9/02
to

> 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

I assume a Map.Entry object are a pair of key-values

Patricia Shanahan

unread,
Sep 9, 2002, 10:32:55 AM9/9/02
to
Sted Alana wrote:
>> > 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.
>
> 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.

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

Geoff Rimmer

unread,
Sep 9, 2002, 11:58:24 AM9/9/02
to
"Sted Alana" <Sted_...@hotmail.com> writes:
>>
>> 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
>
> I assume a Map.Entry object are a pair of key-values

Correct.

Roedy Green

unread,
Sep 9, 2002, 12:38:08 PM9/9/02
to
On Mon, 9 Sep 2002 20:37:25 +0930, "Sted Alana"
<Sted_...@hotmail.com> wrote or quoted :

>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


Sted Alana

unread,
Sep 10, 2002, 8:07:00 AM9/10/02
to

"Patricia Shanahan" <pa...@acm.org> wrote in message
news:3D7CB123...@acm.org...

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.

Chris Smith

unread,
Sep 10, 2002, 7:49:35 PM9/10/02
to
Sted Alana wrote ...

> 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

0 new messages