File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes what is the use of different collections.. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "what is the use of different collections.." Watch "what is the use of different collections.." New topic
Author

what is the use of different collections..

harish pathak
Ranch Hand

Joined: Dec 17, 2005
Posts: 51
Hi All,

Anybody please tell me what is the use of different collections..

when we use ArrayList, when we use Vector, When we use TreeMap, when we use HashMap, when we use Hashtable, when we use Arrays.


please explain.


Please reply

Thanks
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Moving to Java In General (Intermediate) where you will, no doubt, be told that the best way to find out about the collection classes is to read the API.
http://java.sun.com/j2se/1.5.0/docs/api/index.html


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12907
    
    3

when we use ArrayList, when we use Vector, When we use TreeMap, when we use HashMap, when we use Hashtable, when we use Arrays.

1. Never use Vector; always use ArrayList instead. Vector is an old class, left over from Java version 1.1. The main difference between Vector and ArrayList is that Vector is synchronized, while ArrayList is not. That makes ArrayList faster.

2. TreeMap or HashMap: Both are dictionary data structures; they allow you to store key-value pairs, and you use the keys to find the values. HashMap is unordered. In a TreeMap, elements are sorted on the keys. Sometimes that is useful in applications.

3. Hashtable: The same as with Vector; this is an old Java 1.1 class. Don't use it; use HashMap instead.

4. Arrays: An array has a fixed size. If you need a list that can grow or shrink dynamically (i.e., while the program is running), you'll need to use one of the implementations of java.util.List instead of an array.

Look here for more information: The Collections Framework


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
crossposted in just about every Java forum I know...


42
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Crib Sheet


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Vivek Bhardwaj
Ranch Hand

Joined: Nov 07, 2011
Posts: 61

Jesper de Jong wrote:when we use ArrayList, when we use Vector, When we use TreeMap, when we use HashMap, when we use Hashtable, when we use Arrays.

1. Never use Vector; always use ArrayList instead. Vector is an old class, left over from Java version 1.1. The main difference between Vector and ArrayList is that Vector is synchronized, while ArrayList is not. That makes ArrayList faster.

2. TreeMap or HashMap: Both are dictionary data structures; they allow you to store key-value pairs, and you use the keys to find the values. HashMap is unordered. In a TreeMap, elements are sorted on the keys. Sometimes that is useful in applications.

3. Hashtable: The same as with Vector; this is an old Java 1.1 class. Don't use it; use HashMap instead.

4. Arrays: An array has a fixed size. If you need a list that can grow or shrink dynamically (i.e., while the program is running), you'll need to use one of the implementations of java.util.List instead of an array.



Great!! explanation Jesper, its really more valuable statements whatever you posted here, it'll help to understand the actual use of those for both who is learning java and also for them who's just started to work on the projects..actually some more points i also learnt from this thread..so thank you very much Jesper for this post.

__________________________________________________________________________________________________________________
-vivek
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: what is the use of different collections..
 
Similar Threads
collections
Choosing ESB
hashcode
Use of Collections class?
Null