Pages

Tuesday, June 12, 2012

Collections


The collections are very similar to arrays, in fact there are special arrays. There are many collections (ArrayList, HashMap, Vector...) each one has different properties. Everyone must be instancied, and have common methods to get and set their values. Someone has different methods to set its values (like Map, HashMap, HashTable...) because every set of value is composed by a key and a value.

Every collection must specified the data type there are going to store. The data type must be specified when the variable is declared, and must be the same when it is instancied:
ArrayList<String> a;
a = new ArrayList<String>();

The collections do not need to specified its size, they have a dynamic size, you can put some values and the size of the collections will increase at the time. When you instancied a collection, you can specified some properties, like the initial size, and the value which will increase the collection at each insertion:
Vector<Integer> a = new Vector<Integer>(0,1); //initial size=0; increment value=1

Like I said before, the collections have common methods to access their values:
  • boolean add(Object o): add a new element at the end of the collection
  • boolean add(int index, Object o): add a new element at the specified index
  • boolean addAll(Collection<Object> c): add all the given collection to the end of this collection
  • void clear(): delete all the elements
  • boolean contains(Object o): return true if the collection has the given object
  • boolean containsKey(Object key): return true if the collection has the given object as a key
  • boolean containsValue(Object value): return true if the collection has the given object as a value
  • Object get(int index): return the object stored in the given index
  • int getIndexOf(Object o): return the index where is stored the given object
  • int lastIndexOf(Object o): return the index where is stored the given object starting to look at the end of the collection
  • Object put(Object key, Object value): put a set of key/value in the collection
  • Object remove(int index): remove the object stored at the given index
  • boolean remove(Object o): remove the given object from the collection, and returns true if it was successfull
  • int size(): return the size of the collection


Here are not shown all the methods, and not every of those methods are in every collection, each collection has its own properties. Here are explained to most commons:
  • ArrayList and Vector: those are both simples arrays. Every data you add will be stored at the end of the collection. The difference between them are very insignificant: The Vector store its values synchronizing multiples threads, and ArrayList does not. Internally, the ArrayList size is smaller than the Vector's.
  • HashMap and HashTable: those are a two dimensional arrays. Every value is defined from a key, not from an index like the previous one. The new values in those arrays are not stored at the end of the collection, but in the first place of internal memory. To get one of the stored values, you must give the key, and viceversa. The difference between HashMap and HashTable are similar to ArrayList and Vector. HashTable stores its values synchronizing multiples threads, and HashMap does not. But HashMap allows store null values, and only one null key, and HashTable does not store any null value (neither key or value).

No comments:

Post a Comment