File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Which collection class would you use to store the objects? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Which collection class would you use to store the objects?" Watch "Which collection class would you use to store the objects?" New topic
Author

Which collection class would you use to store the objects?

Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

You are designing a class that will cache objects. This class should work by tracking the "last accessed times" of the objects.
Which collection class would you use to store the objects?


  • HashSet
  • ArrayList
  • LinkedHashMap
  • LinkedList
  • TreeMap



  • NOTE:I copied this question from jqplus.


    SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
    rohan yadav
    Ranch Hand

    Joined: Oct 13, 2009
    Posts: 156
    I think its a LinkedList.


    Sage of The Monstrous Toad of Mount Myoboku
    Jonathon Stride
    Ranch Hand

    Joined: Aug 06, 2009
    Posts: 34
    LinkedHashMap

    what book you using , try K&B SCJP 6 mate ^


    2 interviews failed cause of not having SCJP ( and counting...)
    not anymore !

    SCJP 6 (70%)

    now ready to count other reasons :P...
    Minhaj Mehmood
    Ranch Hand

    Joined: Jan 22, 2007
    Posts: 400

    Jonathon Stride wrote:LinkedHashMap

    what book you using , try K&B SCJP 6 mate ^


    Can you explain it further?
    Minhaj Mehmood
    Ranch Hand

    Joined: Jan 22, 2007
    Posts: 400

    rohan yadav wrote:I think its a LinkedList.


    can you explain your answer?
    Neha Daga
    Ranch Hand

    Joined: Oct 30, 2009
    Posts: 504
    I think it should be LinkedHashMap.

    From K&B:
    the LinkedHashMap collection maintains insertion order (or, optionally, access order).


    and I think Linkedlist keeps and order based on index.


    SCJP 1.6 96%
    Jonathon Stride
    Ranch Hand

    Joined: Aug 06, 2009
    Posts: 34
    http://java.sun.com/javase/7/docs/api/java/util/LinkedHashMap.html
    for more info
    Ankit Garg
    Sheriff

    Joined: Aug 03, 2008
    Posts: 9321
        
      17

    The question says that objects will be tracked by the last accessed time, by that I think it means that this will be the key of the objects (value). So LinkedList cannot be used here. That leaves us with LinkedHashMap and TreeMap, but since TreeMap is sorted, so the key needs to be comparable (or we'll have to use comparator). Since nothing is mentioned about sorting, so we'll use LinkedHashMap...


    SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
    Minhaj Mehmood
    Ranch Hand

    Joined: Jan 22, 2007
    Posts: 400

    Ankit Garg wrote:The question says that objects will be tracked by the last accessed time, by that I think it means that this will be the key of the objects (value). So LinkedList cannot be used here. That leaves us with LinkedHashMap and TreeMap, but since TreeMap is sorted, so the key needs to be comparable (or we'll have to use comparator). Since nothing is mentioned about sorting, so we'll use LinkedHashMap...


    yes absolutely right!
    following is the explanation given into jqplus:
    The LinkedHashMap class maintains the elements in the order of their insertion time. This property can be used to build the required cache as follows:
    1. Insert the key-value pairs as you do normally.
    2. When a key is requested, remove it from the LinkedHashMap and then insert it again. This will make sure that this pair marked as inserted latest.
    Note that, you cannot simply insert the key-value again (without first removing it) because a reinsertion operation does not affect the position of the pair.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Which collection class would you use to store the objects?