• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Collection

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When do i use

1.Vector or ArrayList ?
2.HashTable or HashMap ?
3.What should i do to get the objects in the collection in the same order as i put in.?
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hashtable and Vector are the original, synchronized collections. Even if you need a synchronized collection, I'd skip those and either use ArrayList/HashMap wrapped using Collections.synchronizedList/Map() or a collection specifically coded to do better synchronization. For example, JDK 1.5 has ConcurrentHashMap which allows multiple readers and only locks out other threads when writing. It has better multithreaded performance under average conditions (reads exceed writes).

List already maintains insertion order if you add items at the end. LinkedHashMap adds insertion-order-iteration to HashMap.
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.Vector or ArrayList ?
2.HashTable or HashMap ?
3.What should i do to get the objects in the collection in the same order as i put in.?


As 1. Use Vector when u want ur Collection object to be thread safe (safe while multiple threads invoking operations which JVM achieves by having Object level lock. No chances of data getting corrupted in the Vector). Use ArrayList when thread safety dosent matter to u (Chances of data getting corrupted if multiple threads working on the same reference).

As 2. same as above (Hashtable-thread safe, HashMap - not thread safe. Additionaly Hashtable does not permit any null key and null value, however HashMap permits single null key and null values)

As3. use LinkedHashMap,LinkedHashSet,LinkedList,Vector,ArrayList
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a Collections Crib Sheet I made because I couldn't keep them all in my head. The Collections class has some factory methods that return thread safe versions of collections and other specialized versions.
 
Your mind is under my control .... your will is now mine .... read this tiny ad
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic