• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Map typeCasting

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 76099
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don't you want to use a Comparator?
 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 76099
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 76099
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic