Forums Register Login

Problem in Creating HashMap object then display in sorted and unsorted order

+Pie Number of slices to send: Send
Hi All,
I'm really confused in that error
run:
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at myhashmap.MyHashMap.main(MyHashMap.java:44)
Java Result: 1


Create your own NetBeans project named as MyHashMap

Create your own HashMap object

Add the following objects to the newly created HashMap object.

2 String objects with keys "1st", "2nd"

2 MyOwnClass objects with keys "3rd", "4th"

1 Integer ojbects with keys "5th"

Display them in both unsorted and sorted order

I tried to write this;
________________________________________________

_____________________________________________________________

__________________________________________
There are lots of reference as you see, Please help to arrenge my code:(
+Pie Number of slices to send: Send
The problem is that you put in mixed type objects in the HashMap (such as String, Integer and MyOwnClass). Then when you retreive them you cast to String. This give you the ClassCastException.
+Pie Number of slices to send: Send
This is the problem

String value =(String) m.getValue();

when you getValue() from the map, it can be String object, Integer object or MyOwnClass object, so you can't simply cast everything to String.


Try this

String value = m.getValue().toString();


+Pie Number of slices to send: Send
 

Embla Tingeling wrote:The problem is that you put in mixed type objects in the HashMap (such as String, Integer and MyOwnClass). Then when you retreive them you cast to String. This give you the ClassCastException.


Thanks Embla,
I add like this, after your arrangement

__________________________________
now the error turns in to
run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - java.util.Comparator is abstract; cannot be instantiated
at myhashmap.MyHashMap.main(MyHashMap.java:26)
Java Result: 1

I think ,i am placed to wrong line. but, i don't know the correct way
+Pie Number of slices to send: Send
 

Lee Kian Giap wrote:This is the problem

String value =(String) m.getValue();

when you getValue() from the map, it can be String object, Integer object or MyOwnClass object, so you can't simply cast everything to String.


Try this

String value = m.getValue().toString();




wooow you're right. now it runs
run:
UnSorted order is : {2nd=Passion, 1sth=Java, 5th=9, 3rd=aa, 4th=bb}
Sorted map {1sth=Java, 2nd=Passion, 3rd=aa, 4th=bb, 5th=9}

Thanks thanks a lot, with my Regards Lee Kian Giap )
+Pie Number of slices to send: Send
Sarrah, welcome to JavaRanch

Could you try to Use Code Tags in the future? Thanks.
+Pie Number of slices to send: Send
 

Rob Prime wrote:Sarrah, welcome to JavaRanch

Could you try to Use Code Tags in the future? Thanks.



hahah ok ,i did,sorry
thanks Rob Prime
That is a really big piece of pie for such a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 4776 times.
Similar Threads
Sort a HashMap based on a Value object's attribute
Sorting Maps
Sorting values of a Map.
determine the input type of the value of the function parameter map
HashMap Doubt
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 01:03:22.