File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Map typeCasting

 
Anu satya
Ranch Hand
Posts: 146
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you want to use a Comparator?
 
Anu satya
Ranch Hand
Posts: 146
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet wrote: . . . make your List generic . . .
Like this, I presumeAnd your Iterator should be an Iterator<String>.
 
Anu satya
Ranch Hand
Posts: 146
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic