• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

generics

 
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HashMap<Integer, String> hm = new HashMap<Integer,String>();
Iterator<Map.Entry> a = hm.entrySet().iterator(); //compilation error

Why the raw type must be parametrized? I thought that generics is flexible to fit the legacy code.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This will work



Is your question, 'why cant I just say the iterator contains Map.Entry elements instead of telling the compiler it contains Map.Entry<Integer, String>' ?
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have mistaken. That works:
Iterator<Map.Entry<Integer, String>> iterator = hm.entrySet().iterator();

But why the type must be parametrized?

That runs well:
HashMap hm = new HashMap<Integer, String>();

So why must the first case be parametrized?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why the raw type must be parametrized? I thought that generics is flexible to fit the legacy code.



Well, your two choices are...



This...



is just using Generics, but not correctly. Either you use generics or you don't -- using it partially is not allowed.

Henry
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what about that:
HashMap hm = new HashMap<Integer, String>();
HashMap<Integer, String> hm = new HashMap();
Both run pretty well.


EDIT:
Well, I see. Your answer if for nested generics. OK, I understand!
Thanks!

EDIT2:
But one more thing:
List<List> list = new ArrayList<List>(); //runs well

Can you explain me that again?

EDIT3:
OK, I will explain it to myself:
List<List> list = new ArrayList<List<String>>(); //doesn't work
Everything is clear!
 
See where your hand is? Not there. It's next to this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic