File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Map typeCasting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Map typeCasting" Watch "Map typeCasting" New topic
Author

Map typeCasting

Anu satya
Ranch Hand

Joined: Mar 17, 2005
Posts: 146
Hi,
I have a map, which is Map<String, String>, i want to typecast it to Map <Integer, String>
is it possible to do it?
if yes, how i can do it ?

my code as follows:

I don't want to use comparator. So, is it possible to typecast key in map to Integer?
Please Help


With Regards,
Anu
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Anu satya wrote:Hi,
I have a map, which is Map<String, String>, i want to typecast it to Map <Integer, String>
is it possible to do it? . . .
No. Not unless you want lots of Exceptions and compiler errors.

If you want to sort the List in numerical order rather than lexical order, try iterating the key set, and using the Integer constructor to create Integers and populate the List with those.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Why don't you want to use a Comparator?


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Anu satya
Ranch Hand

Joined: Mar 17, 2005
Posts: 146
Thanks Ritchie,
now, it is working fine for me.
i have modified my code to

Now, sorting is proper. Thanks a lot.

Wouter Oet wrote:Why don't you want to use a Comparator?

I had two options either to convert String to Integer and sort or use comparator and pass as an argument to Collections.sort() method. So, i thought converting String to Integer is easier than creating a comparator.
Please correct me if i am wrong.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

It's not necessaraly wrong. But I would advice you to make your List generic so that you don't need to cast again when you try to use the elements.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Wouter Oet wrote: . . . make your List generic . . .
Like this, I presumeAnd your Iterator should be an Iterator<String>.
Anu satya
Ranch Hand

Joined: Mar 17, 2005
Posts: 146
Campbell Ritchie wrote:
Wouter Oet wrote: . . . make your List generic . . .

Thanks Ritchie, My code is working fine now. But due to dealing with two Lists and iterating through it might hit the performance.

so, what i need to do if i am going to use comparator?

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
To use a Comparator<Foo> you would have to work out in which order you are going to sort Foo. You can have several Comparators<Foo>, with different sorting.

Start with pencil and paper, and eraser (when I go to lectures I carry a big eraser which looks as if it would cause real injury if I threw it at a student and I say that eraser is their most important piece of hardware).
Write down how you ought to sort your Foo.
Remember that String and Integer already implement Comparable<String> and Comparable<Integer> and there is a Comparator<String> inside the String class ready for you to use. So you may not need Comparators there.
When you have written how to sort your Foo, covert that to code and put it in the Comparator<T>#compare(T, T) method, in a class which implements Comparator. You can forget about the equals() method.
Pass an object of that Comparator to any sort() method you are using.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Map typeCasting