-2

so i want to have Chat-Elements stored in a Collections and I still don't know what Collections to use. It is just like the Chatlist of WhatsApp where it is sorted by date but properties/parameters of each chat can change anytime (chat muted/marked ... )

  1. Elements (ID, Date, different parameters)
  2. Needs to be sorted by date [all the time]

    => TreeSet is optimal

  3. No duplicate entries allowed [doesnt happen often]

    => Treeset is optimal

  4. Parameters are changing [all the time]

    (Iterating by Id, setting specific parameters)

    => I think ArrayList is the better option

  5. Android App, Less memory usage is preferable

    => Arraylist is optimal

Currently I am using Arraylists but Im not really sure if Point 4 is true and if so, if it is that relevant.

Ayox
  • 691
  • 5
  • 15
  • This really depends on how you're going to *use* the container, so your description doesn't really tell us enough to recommend one over the other. Is your insertion order different from the sort order (are you inserting elements with yesterday's dates in to a container with today's dates)? Why can't you have two elements with the same date, and when does that occur? Do you use `contains(...)` frequently? Do you use `iterator()` frequently? And so on. – Radiodef May 27 '17 at 14:19
  • Normally i am just inserting elements with todays dates or setting some elements to todays date ... I can have elements with the same date but not with the same Id... I use iterator() more frequently than contains(..) – Ayox May 27 '17 at 14:23
  • Well, you should update your question with more information about what kind of program you're writing and what kinds of things you are doing with this container. As it stands, almost any container can be made to fit the requirements you've given. You can see that you've already received two pretty disparate suggestions, using `HashMap` and `PriorityQueue`, which goes to show that we probably have no idea what you should use at this point. It could also be that you should use more than one container. – Radiodef May 27 '17 at 14:37
  • updated, please have a look again :) – Ayox May 28 '17 at 12:20

1 Answers1

-1

you can use a hashmap

Design a hashcode generator, which maps the data according to the date, fetch components from the date string and assign it to the arraylist(used internally) so that it always saves in order on increasing dates. Map it using ID as the key.

Iteration by Id

using hashmap, eases the fetching of parameters and changing them frequently.

Memory will be used once only.

Mansi
  • 1
  • 1
  • `HashMap` is unordered. In particular, it can reorder the elements arbitrarily any time a new element is added. There is no way to generate hash codes in a way that will cause the elements to be sorted. – Andreas May 27 '17 at 15:01