• Post Reply Bookmark Topic Watch Topic
  • New Topic

add method in ArrayList  RSS feed

 
Ketung Xiao
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Class ArrayList has add method with this signature:


In the following code:



the add method does not necessary return boolean,
can someone give an example that explain the the purpose of boolean instead of void
in ?

Thanks.
 
Stephan van Hulst
Saloon Keeper
Posts: 7720
142
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For lists, the return value is not interesting. Any List will always return true when you call add(). List inherited the method from Collection, and there are some collections where the return value is meaningful. For instance, Set will return true if the value has been added to the set successfully, and false if it hasn't, because the set already contained that value.
 
Junilu Lacar
Sheriff
Posts: 10949
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JavaDocs for Collection.add says that add() will return true if the collection is changed as a result of calling this method. The JavaDocs for the List interface specifies that add() always returns true, meaning that a List will always change as a result of calling add().
 
Mohammed Sardar.
Ranch Hand
Posts: 164
1
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quick Note, Map Collection will override the duplicate instead of ignoring and returning false as like Set. Just to differentiate as this also
comes in collection.
 
Stephan van Hulst
Saloon Keeper
Posts: 7720
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, Map doesn't have an add() but a put() method. The semantics are very different.
 
Mohammed Sardar.
Ranch Hand
Posts: 164
1
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree semantics is different but functional wise same. All comes under Collection. Just to differentiate.
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohammed Sardar. wrote:Agree semantics is different but functional wise same. All comes under Collection. . . .
No, I think that is incorrect. A Map is not a Collection, and the functionality is different.
If you add something to a List which already contains it, that has one effect.
If you add something to a Set which already contains it, that has a second effect.
If you put something into a Map which already contains that Key, it has a third effect.
I shall leave you to work out what the differences are.
 
Mohammed Sardar.
Ranch Hand
Posts: 164
1
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Mohammed Sardar. wrote:Agree semantics is different but functional wise same. All comes under Collection. . . .
No, I think that is incorrect. A Map is not a Collection, and the functionality is different.
If you add something to a List which already contains it, that has one effect.
If you add something to a Set which already contains it, that has a second effect.
If you put something into a Map which already contains that Key, it has a third effect.
I shall leave you to work out what the differences are.


List -> One Effect -> Accepts Duplicates.
Set -> Second Effect -> Ignores Duplicates.
Map -> Third Effect -> Overrides Duplicates Key if already Exists.

But all does same adding an item functionality with different style if the element already is there. After that what is that third effect ? . Could you please show me some light on that ? Of course I will do work out but the third effect, I'm really thinking on that too much.

Thanks.
Still Learning.
 
Dave Tolls
Ranch Hand
Posts: 2835
30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The difference is that Set and List are extensions of Collection.
Map isn't.
Which is why it doesn't have an add() method.
 
Mohammed Sardar.
Ranch Hand
Posts: 164
1
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:The difference is that Set and List are extensions of Collection.
Map isn't.
Which is why it doesn't have an add() method.


When I posted I recalled from Head First Java page: 558 as .

    "Notice that the Map interface doesn't actually extend the Collection interface, but Map is still considered part of the "Collection Framework"(AKA "Collection API"). So Maps are still collections, even though they don't include java.util.Collections in their inheritance tree. ".

The Map does not have add() method but it has put method which does the job of add I said. Ultimately, it adds an item to the Collection what I tried to mean.

Kindest Regards
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you add the same element twice to a List, it is in there twice.
If you add the same element the second time to a Set, nothing happens.
If you add the same key to a Map, what happens depends on the associated Value.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!