Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Which collection class would you use to store the objects?

 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
     
    rohan yadav
    Ranch Hand
    Posts: 156
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think its a LinkedList.
     
    Jonathon Stride
    Ranch Hand
    Posts: 34
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    LinkedHashMap

    what book you using , try K&B SCJP 6 mate ^
     
    Minhaj Mehmood
    Ranch Hand
    Posts: 400
    Hibernate Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jonathon Stride wrote:LinkedHashMap

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


    Can you explain it further?
     
    Minhaj Mehmood
    Ranch Hand
    Posts: 400
    Hibernate Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    rohan yadav wrote:I think its a LinkedList.


    can you explain your answer?
     
    Neha Daga
    Ranch Hand
    Posts: 504
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Jonathon Stride
    Ranch Hand
    Posts: 34
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    http://java.sun.com/javase/7/docs/api/java/util/LinkedHashMap.html
    for more info
     
    Ankit Garg
    Sheriff
    Posts: 9510
    22
    Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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...
     
    Minhaj Mehmood
    Ranch Hand
    Posts: 400
    Hibernate Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic