TechTalkz.com Logo Ask the Experts!

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Programing Languages > Java

why people use "Map m= new HashMap()" or "List l = new ArrayList()"?

Java

 
 
Thread Tools Display Modes
Unread 19-10-2007, 08:01 PM   #1
www
Guest
 
Posts: n/a
why people use "Map m= new HashMap()" or "List l = new ArrayList()"?

Hi,

I saw in many places that people use:

Map m= new HashMap();

or

List l = new ArrayList();

I *rarely* see people do:

HashMap m= new HashMap();

or

ArrayList l = new ArrayList();

Is any specific reason for such a practice?

I noticed Map does not have clone(), but HashMap does.
 
Unread 19-10-2007, 08:02 PM   #2
Richard Reynolds
Guest
 
Posts: n/a
Re: why people use "Map m= new HashMap()" or "List l = new ArrayList()"?


"www" <www@nospam.com> wrote in message
news:ffavfo$760$1@news.nems.noaa.gov...
> Hi,
>
> I saw in many places that people use:
>
> Map m= new HashMap();
>
> or
>
> List l = new ArrayList();
>
> I *rarely* see people do:
>
> HashMap m= new HashMap();
>
> or
>
> ArrayList l = new ArrayList();
>
> Is any specific reason for such a practice?
>
> I noticed Map does not have clone(), but HashMap does.


They're using the most "general" type that they can to give them the
greatest flexibility in their design. It's a trait of OO programming in
general. Try reading some basic OO design tutorials, in particulay you might
want to pay attention to generalisation, specialisation and, in Java for
instance, the use of abstract classes and interfaces.


 
Unread 19-10-2007, 08:02 PM   #3
Mark Space
Guest
 
Posts: n/a
Re: why people use "Map m= new HashMap()" or "List l = new ArrayList()"?

www wrote:

> I saw in many places that people use:
>
> Map m= new HashMap();


> I *rarely* see people do:
>
> HashMap m= new HashMap();


1. Convention. As you say, many people do it this way.

2. Flexibility. It may be easier to change the implementation of the
former than the latter. The latter form would encourage other
programmers to code to the specific HashMap() interface, rather than the
more general Map() interface. With Map(), you can just plug in a new
type of map like

Map m = new LinkedHashMap();

But if you've coded to a specific type of Hash Map this change may be
not so easy to make.

3. Probably some other reasons I can't think of right now... always it
depends on your application.
 
Unread 19-10-2007, 09:01 PM   #4
Lew
Guest
 
Posts: n/a
Re: why people use "Map m= new HashMap()" or "List l = new ArrayList()"?

Mark Space wrote:
> www wrote:
>
>> I saw in many places that people use:
>>
>> Map m= new HashMap();

>
>> I *rarely* see people do:
>>
>> HashMap m= new HashMap();

>
> 1. Convention. As you say, many people do it this way.
>
> 2. Flexibility. It may be easier to change the implementation of the
> former than the latter. The latter form would encourage other
> programmers to code to the specific HashMap() interface, rather than the
> more general Map() interface. With Map(), you can just plug in a new
> type of map like
>
> Map m = new LinkedHashMap();
>
> But if you've coded to a specific type of Hash Map this change may be
> not so easy to make.
>
> 3. Probably some other reasons I can't think of right now... always it
> depends on your application.


It is a best practice to prefer the most general type possible for the
compile-time type of a variable. This provides the most bug-free and
maintainable code.

The word is "prefer", not "demand", and "possible for the ... type", which
might be a specific implementation but usually isn't.

Consider the fairly common error of using Vector for a List when you don't
need its special features. If you declared

List <String> stuff = new Vector <String> ();

then it's much easier to change to

List <String> stuff = new ArrayList <String> ();

and later, if you see that you need specific performance characteristics,

List <String> stuff = new TreeList <String> ();

or even

List <String> stuff
= Collections.synchronizedList( new ArrayList <String> () );

No other code will depend on non-List methods such as those of Vector, so you
are much safer in making the changes.

Read Joshua Bloch's excellent book /Effective Java/ for details on this and
many other best practices.

--
Lew
 
Unread 20-10-2007, 02:59 AM   #5
Jim Korman
Guest
 
Posts: n/a
Re: why people use "Map m= new HashMap()" or "List l = new ArrayList()"?

On Fri, 19 Oct 2007 15:10:48 -0400, www <www@nospam.com> wrote:

>Hi,
>
>I saw in many places that people use:
>
>Map m= new HashMap();
>
>or
>
>List l = new ArrayList();
>
>I *rarely* see people do:
>
>HashMap m= new HashMap();
>
>or
>
>ArrayList l = new ArrayList();
>
>Is any specific reason for such a practice?
>
>I noticed Map does not have clone(), but HashMap does.


The rule I use is if the collection I'm using is local to a method,
or totally private to a class, then I'll use the specific collection
type. If the collection is visible anywhere outside the class or
especially subclasses, I use the collection interfaces.

Jim
 
Unread 20-10-2007, 03:00 AM   #6
Lew
Guest
 
Posts: n/a
Re: why people use "Map m= new HashMap()" or "List l = new ArrayList()"?

Jim Korman wrote:
> The rule I use is if the collection I'm using is local to a method,
> or totally private to a class, then I'll use the specific collection
> type. If the collection is visible anywhere outside the class or
> especially subclasses, I use the collection interfaces.


I use "declare the most general type" even for private items. Of course,
"most general" might mean a specific implementation if the algorithm demands it.

--
Lew
 
Unread 20-10-2007, 03:00 AM   #7
Knute Johnson
Guest
 
Posts: n/a
Re: why people use "Map m= new HashMap()" or "List l = new ArrayList()"?

Lew wrote:
> Mark Space wrote:
>> www wrote:
>>
>>> I saw in many places that people use:
>>>
>>> Map m= new HashMap();

>>
>>> I *rarely* see people do:
>>>
>>> HashMap m= new HashMap();

>>
>> 1. Convention. As you say, many people do it this way.
>>
>> 2. Flexibility. It may be easier to change the implementation of the
>> former than the latter. The latter form would encourage other
>> programmers to code to the specific HashMap() interface, rather than
>> the more general Map() interface. With Map(), you can just plug in a
>> new type of map like
>>
>> Map m = new LinkedHashMap();
>>
>> But if you've coded to a specific type of Hash Map this change may be
>> not so easy to make.
>>
>> 3. Probably some other reasons I can't think of right now... always
>> it depends on your application.

>
> It is a best practice to prefer the most general type possible for the
> compile-time type of a variable. This provides the most bug-free and
> maintainable code.
>
> The word is "prefer", not "demand", and "possible for the ... type",
> which might be a specific implementation but usually isn't.
>
> Consider the fairly common error of using Vector for a List when you
> don't need its special features. If you declared
>
> List <String> stuff = new Vector <String> ();
>
> then it's much easier to change to
>
> List <String> stuff = new ArrayList <String> ();
>
> and later, if you see that you need specific performance characteristics,
>
> List <String> stuff = new TreeList <String> ();
>
> or even
>
> List <String> stuff
> = Collections.synchronizedList( new ArrayList <String> () );
>
> No other code will depend on non-List methods such as those of Vector,
> so you are much safer in making the changes.
>
> Read Joshua Bloch's excellent book /Effective Java/ for details on this
> and many other best practices.
>


It doesn't make a lot of sense to me. Generics were added to closely
control the type of parameters passed to a class that we then want to
make less type specific? It makes no sense to turn a LinkedList for
example into a plain List as you lose all of what makes it a LinkedList.
Of course you could assign it to a Deque.

knute...
 
Unread 20-10-2007, 03:00 AM   #8
Lew
Guest
 
Posts: n/a
Re: why people use "Map m= new HashMap()" or "List l = new ArrayList()"?

Knute Johnson wrote:
> It doesn't make a lot of sense to me. Generics were added to closely
> control the type of parameters passed to a class that we then want to
> make less type specific? It makes no sense to turn a LinkedList for
> example into a plain List as you lose all of what makes it a LinkedList.
> Of course you could assign it to a Deque.


Well, duhy, if you need the specific type you use the specific type. No one
is saying to be stupid about it.

--
Lew
 
Unread 20-10-2007, 03:00 AM   #9
Wayne
Guest
 
Posts: n/a
Re: why people use "Map m= new HashMap()" or "List l = new ArrayList()"?

www wrote:
> Hi,
>
> I saw in many places that people use:
> Map m= new HashMap();
> or
> List l = new ArrayList();
> I *rarely* see people do:
> HashMap m= new HashMap();
> or
> ArrayList l = new ArrayList();
> Is any specific reason for such a practice?


You bet. When the map is visible outside of the
current method (or class), you only want external
code to know that you have a Map. They should not
care about the *implementation* of that Map, which
the implementer can then change without potentially
harming users of that code.

This is a standard practice called "data hiding"
or other more impressive terms.

Note: when you (someday) learn about "generics",
don't fall into the trap of:
List l = new ArrayList<String>();
Instead use:
List<String> l = new ArrayList<String>();

Verbally you would say that "l is a list of Strings".

-Wayne
 
Unread 20-10-2007, 06:59 AM   #10
Chris ( Val )
Guest
 
Posts: n/a
Re: why people use "Map m= new HashMap()" or "List l = new ArrayList()"?

On Oct 20, 12:41 pm, Wayne <nos...@all4me.invalid> wrote:
> www wrote:
> > Hi,

>
> > I saw in many places that people use:
> > Map m= new HashMap();
> > or
> > List l = new ArrayList();
> > I *rarely* see people do:
> > HashMap m= new HashMap();
> > or
> > ArrayList l = new ArrayList();
> > Is any specific reason for such a practice?

>
> You bet. When the map is visible outside of the
> current method (or class), you only want external
> code to know that you have a Map.


>From what I have read, I don't think that is quite

the reson behind it.

> They should not
> care about the *implementation* of that Map, which
> the implementer can then change without potentially
> harming users of that code.


Yes, however; that is typical of an interface hiding
its implementation, via a layer of abstraction.

> This is a standard practice called "data hiding"
> or other more impressive terms.


But "data hiding" is an artifact of encapsulation,
generally achieved through the implementation of
private accessors - This answers a different question
to what the OP really asked.

>From what I understand, the OP wants to know why

one construct "Map m= new HashMap();" is prefered
over another "HashMap m= new HashMap();".

[snip]

In my studies so far (and I have not seen anyone mention
this yet), but the following constructs:

"Map m= new HashMap();" or...
"public void someFunc( Map m ); // Accepts HashMap, etc.

....appear to be what is known as: "Programming To An Interface".

It is said to be preferred and encouraged because it offers the
most flexible solution of all.

Cheers,

Chris

 
 

Thread Tools
Display Modes



< Home - Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT. The time now is 09:39 PM.


vBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2013, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional