aspose file tools*
The moose likes Java in General and the fly likes Question about inserting elements into a treemap as compared to arraylist? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Question about inserting elements into a treemap as compared to arraylist?" Watch "Question about inserting elements into a treemap as compared to arraylist?" New topic
Author

Question about inserting elements into a treemap as compared to arraylist?

Mark O' Sullivan
Ranch Hand

Joined: Aug 17, 2009
Posts: 160
Hi,
Could someone tell me why I can insert elements into a treemap in an unordered way say treemap.put(3,"Hello") for element 3, and then treemap.put(1,"hello2") for element 1, without there being any problems as compared to an arraylist, when I try the arraylist.add(3,""hello) at start, it tells me the arraylist is empty and cannot be inserted, but I haven't this issue with treemap. I even tried setting initial capacity of arraylist but it still told me the arraylist was empty. Just curious as to why.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
What is the exact text of the Exception you are getting?

From the ArrayList source code you should only get a IndexOutOfBoundsException if the index is <0 or > size.

Bill
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3679
    
  16
Why are you comparing the functionality of the TreeMap put method and the ArrayList add method ? They do different things. The first parameter to the put method is not an index to indicate the position you want the object in the map, it is a key that is used to access an associated value in the map. The two calls only look alike becaus eyou used an integer as the key. If you do something like
it is more obvious that the two methods are totally unrelated.


Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
I presume you have read about that add() method?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3679
    
  16
Campbell Ritchie wrote:I presume you have read about that add() method?
Me or Mark ?
Mark O' Sullivan
Ranch Hand

Joined: Aug 17, 2009
Posts: 160
Cheers, thanks for replys. Thanks William, it was IndexOutOfBoundsException exception I was thinking. My fault, I was too restrictive in my thinking about comparing the 2 because I've been using integers as keys for the treemap. I have a much better understanding of how it works now. Just find it interesting though, that even if I allocate a predefined size for the arrayList, that it doesn't create predefined empty objects in memory, so I could do arraylist.add(3,""hello)? Thanks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
Joanne Neal wrote:
Campbell Ritchie wrote:I presume you have read about that add() method?
Me or Mark ?
Mark.

I know you have read it, Joanne.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
Mark O' Sullivan wrote:. . . even if I allocate a predefined size for the arrayList, that it doesn't create predefined empty objects in memory, so I could do arraylist.add(3,""hello)? Thanks.
I think you have missed out a "not" somewhere, probably after "could".

You cannot allocate a size for a List, but a capacity. Fred Rosenberger talks about egg-boxes, so I shall use the same analogy. Writing ... = new ArrayList<Egg>(25); creates a reference to something representing 25 egg-boxes, each large enough to hold one egg, but all empty. So the collection can hold (capacity) 25 eggs, but does hold (size) none. Writing eggs.add(3, myEgg); adds an Egg just beyond the 3rd egg, and moves all the other eggs along to make space for it (if necessary). If you only have two Eggs already, where should you add it? In the 3rd box or the 4th? You could design a List which adds that Egg in the 3rd space, but ArrayList was designed differently, to throw a tantrum Exception, in those circumstances.
Note there are some sorts of List (eg LinkedList) whose capacity and size are always exactly the same.

Bertrand Meyer in Object-Oriented Software Construction says that all procedures to add something to a collection should have the same name. But I think I disagree with him. That is why there are different names for adding: in a List or Set “add”, in a Queue “offer”, in a Stack “push”, and in a Map “put”. The different names are supposed to remind you they do different things. If you look in the Deque (pronounced Deck, a portmanteau word from Double-Ended QUeuE) interface, there is a list of method names for several different kinds of collection.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
Mark O' Sullivan wrote:
that it doesn't create predefined empty objects in memory, so I could do arraylist.add(3,""hello)? Thanks.

......... what is with the double quote above - ""hello ??

Looking at the source code, we see at ArrayList uses an array of Object references:



The default constructor uses a size of 10 for this array. These are "empty" references, ie null, not "empty objects."

You should therefore be able to place a reference at index 3 using add



AHA - further digging - Looks like size is initially zero so that add will only work for lists that already have at least index references

Bill


Mark O' Sullivan
Ranch Hand

Joined: Aug 17, 2009
Posts: 160
Thanks to everyone for all their help on this topic. I'll sure someone in the same situation will find the question useful when they hit the same problem. Learned loads.
 
jQuery in Action, 2nd edition
 
subject: Question about inserting elements into a treemap as compared to arraylist?