• Register

Share:- Whatsapp Facebook Facebook

------------------------------------- Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.

How to choose java collection?

0 votes
How to choose java collection?
asked Oct 17, 2013 in Java by Tarun Goyal

2 Answers

0 votes

You see the following diagram and choose an appropriate java collection:

Java Collection

answered Oct 21, 2013 by Tarun
0 votes

List :
        Essentially a variable-size array;
        You can usually add/remove items at any arbitrary position;
        The order of the items is well defined (i.e. you can say what position a given item goes in in the list).
        Most cases where you just need to store or iterate through a "bunch of things" and later iterate through them.
    Things can be "there or not"— when you add items to a set, there's no notion of how many times the item was added, and usually no notion of ordering.
    Remembering "which items you've already processed", e.g. when doing a web crawl;
    Making other yes-no decisions about an item, e.g. "is the item a word of English", "is the item in the database?" , "is the item in this category?" etc.

    Stores an association or mapping between "keys" and "values"
    Used in cases where you need to say "for a given X, what is the Y"? It is often useful for implementing in-memory caches or indexes. For example:
    For a given user ID, what is their cached name/User object?
    For a given IP address, what is the cached country code?
    For a given string, how many instances have I seen?

    Like a list, but where you only ever access the ends of the list (typically, you add to one end and remove from the other).
    Often used in managing tasks performed by different threads in an application (e.g. one thread receives incomming connections and puts them on a queue; other "worker" threads take connections off the queue for processing);
    For traversing hierarchical structures such as a filing system, or in general where you need to remember "what data to process next", whilst also adding to that list of data;
    Related to the previous point, queues crop up in various algorithms, e.g. build the encoding tree for Huffman compression.

answered Nov 8, 2013 by Vinod Kumar