• Post Reply Bookmark Topic Watch Topic
  • New Topic

some question of collections  RSS feed

 
fahimeh hashemian
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
I read head first in java book and i did not understand collections topic
I have some question:

1)What is difference between List and Map? whether index position of List just be integer ?and key value of Map, it can be anything?
2) what is difference between ArrayList and Array?

I'm totally confused, please help me ;)
 
vipul bondugula
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1)What is difference between List and Map? whether index position of List just be integer ?and key value of Map, it can be anything?


List and Map are both collections which store data. type of storing is different. List stores data by order using indexes. Map stores data in the form of key/value pairs. key must be unique. index position of List must be integer.

Collections does not store primitive data. But due to autoboxing concept primitives are automatically converted to corresponding wrapper objects.


2) what is difference between ArrayList and Array?



arrays are homogeneous, i.e., they store only declared type data. ArrayList prior to 1.5 stores any kind of data which is not type safe and may also throw ClassCastException.
ArrayList with generics stores declared type data..

arrays cannot grow. i.e., their size is fixed. During initialization we fix the size. An ArrayList can grow.

There are no methods to manipulate arrays. That means suppose if you want to remove an element from array , there are no methods for removing an element from array. ArrayList contains remove method to remove data from list.
 
fahimeh hashemian
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for answer
index position of List is also unique, this is true? because number of position could not duplicate.
key of Map can be String or integer or ...?

what is difference between HashMap and HasheSet?
 
Fyodor Sherstobitov
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
You can consider a List as an array with the ability to change size and some additional features.
Set (and HashSet as one of it implementations) is just a List in which you can't put two or more same items.
In both List and Set indexes are generated automaticaly. If you have a List of 3 items, indexes would be 0, 1, 2 and no other.
On the other hand, in Map (and HashMap as one of it implementations), you can assign a value to any key (index) you want to. You can have HashMap<Integer, String> of 3 key-value pairs with keys (indexes) 35, 115, 1024.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fahimeh hashemian wrote:index position of List is also unique, this is true? because number of position could not duplicate.

Yes that's true but, unlike for an array, it is NOT constant.
When you remove an element from a List, all indexes to the right of the removed element are decremented by 1.

key of Map can be String or integer or ...?

Yup. Anything your little heart desires. Just remember that they MUST be unique.

what is difference between HashMap and HasheSet?

I'd start off with : What is the difference between a Map (java.util.Map(←click)) and a Set (java.util.Set(←click)). Have a look at the API documentation first, and come back if you still have questions.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!