On Apr 21, 3:17 pm, pavan kumar <[email protected]> wrote:
> Hi,
>
> This is my first time using java passions...so something is wrong please
> teach me...
> public class treemap {
>
> public static void main(String[] args) {
> Map m = new HashMap();
> m.put(4, "four");
> m.put(1, "one");
> m.put(3, "three");
> m.put(2, "two");
put() takes an object as key. Above are <int>, but it still works. I
wonder why?
@ Bakoly
I do not encounter any problem with putAll(). Anyway, an alternative
is to construct a treeMap from a hashMap. For example:
Map treeMap=new TreeMap(hashMap);
> printMap(m);
> Map tm = new TreeMap();
> tm.putAll(m);
> printMap(tm);
> }
>
> private static void printMap(Map m) {
> System.err.println("priniting map:"+m);
> for (Iterator it = m.values().iterator(); it.hasNext();) {
> System.err.println(it.next());
> }
> System.err.println("----------------");
> }}
>
> output:
> priniting map:{1=one, 2=two, 3=three, 4=four}
> one
> two
> three
> four
> ----------------
> priniting map:{1=one, 2=two, 3=three, 4=four}
> one
> two
> three
> four
> ----------------
>
> so what is ur problem....
> tm.putAll(m); //what is 'm' object...
>
> On Tue, Apr 21, 2009 at 2:34 AM, Bakoly Takumah <[email protected]> wrote:
> > Hi everyone,
> > In the lab 1016, 3.1 build and run a simple application that uses HashMap,
> > and part 4 of this segment advise us to do our own exercise : please do the
> > following .... display them in both unsorted and sorted order.
> > I did the unsorted order and it goes well, but I have a problem with the
> > unsorted order, I got an execption : java.lang.ClassCastException.
> > I use putAll() method like that :
>
> > Map tm = new TreeMap();
> > tm.putAll(m);
> > And then display tm.
>
> > I have looked at the JavaDoc about this exception for putAll but I don't
> > really understand what it means :
> > ClassCastException - if the class of a key or value in the specified map
> > prevents it from being stored in this map
> > Please, help.
> > Thanks in advance.
> > Bako
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---