• 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

Collection question

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just learned that it is better programming practice if I change TreeMap at the beginning of the second line of code to Map.

Map<String, FilmBean>filmBeans = new TreeMap<String, FilmBean>();

after I do this, I get a compiler error for the return statement at the end of the method.


 
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

Originally posted by henri henri:
I just learned that it is better programming practice if I change TreeMap at the beginning of the second line of code to Map.

Map<String, FilmBean>filmBeans = new TreeMap<String, FilmBean>();

after I do this, I get a compiler error for the return statement at the end of the method.



Well, you can't make the change and everything magically still works. You have to change your method to return a Map instead of a TreeMap. And you have to change all the places in your program to use a Map instead of a TreeMap.

Henry
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's all about object typing.

Please read about this in a good Java textbook, I recommend Bruce Eckel's thinking in Java.

Keywords: Object type, class type, object references, handle, casting.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stuart Ash:
... I recommend Bruce Eckel's thinking in Java...


I second that recommendation. And here's a link to an online version...

http://www.faqs.org/docs/think_java/TIJ3.htm
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To sum up some previous responses, yes it's good programming practice to
"program to the interface" when possible. In your example, it's more important
for your getFilmBeansByLetter method to have return type Map (or Map<String FilmBean>
in Java 1.5) that for its local value filmBeans to have type Map<String FilmBean>
versus TreeMap<String FilmBean>, although the two can go hand-in-hand.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic