My Blog List

Saturday, March 17, 2012

Java Collections - Array, ArrayList, HashTable

Array vs ArrayList in Java:
Array - Fixed Size, Same Data Types only
  1. int[] myArr = new int[10];
  2. myArr[0] = 10;
  3. System.out.println( myArr[0] );
  4. System.out.println( myArr.length );
ArrayList - Dynamic Size, Different data types but not primitive types, Synchronized.
  1. ArrayList<String> myList = new ArrayList<String>();
  2. myList.add("abcd");
  3. myList.add(10);   //Adds in end of list because index not mentioned.
  4. System.out.println( myList.get(0) );

    //Lot of methods are there here.
  5. add(index, Value);
  6. addAll(collection);
  7. clear()
  8. contains(value)
  9. indexOf(value)
  10. get(index)
  11. isEmpty()
  12. lastIndexOf(value)
  13. set(index, value) //Replace the value
  14. size()
  15. toArray()
  16. iterator()   // One-Directional, only access
  17. listIterator() //Bi-Dierectional, allows modification
HashTable vs HashMap vs HashSet:
HashTable - Syncronized, will not allow NULL key/value.
- put, get
HashMap - UnSyncronized, will allow NULL key/value (so maybe good performance than HashTable)
- put, get
HashSet - Will allow duplicate values
- add, contains, get

1 comment:

  1. Thanks for sharing great information in your blog. Got to learn new things from your Blog . It was very nice blog to learn about Selenium

    ReplyDelete