Take the 2-minute tour ×

I need to represent the below data (in Java):

  • 2012 (Year)
    • 01(Month)
      • 01 (Day)
        • Hello I am a string
      • 02
      • ...
    • 02
    • 03
    • 04

I was thinking of using a TreeMap, but not sure how. Any ideas?

share|improve this question
What are your requirements of the data structure, as in query time (O(?)), memory space, etc? –  Marvin Pinto Jan 25 '12 at 19:52
i not sure of those requirements. –  Michael Jan 25 '12 at 20:15
Is the object of the model to store Strings in days? –  paislee Jan 25 '12 at 20:23
Yep, so i can retrive the strings that was entered on a certain date. –  Michael Jan 25 '12 at 20:31
You could also try to use a graph by implementing an adjacency matrix. Although it is more used within GPS devices like tomtom's. Wikipedia gives you some nice hints for this. –  vlad Jan 26 '12 at 9:57
add comment

5 Answers

Considerations, assuming you're interested in managing calendar entries:

  • There are infinite possible dates -- don't waste memory on unused days
  • Given a date, you'd want to access it's day quickly -- use array or hash-based lookup
  • Each day has a unique date -- map date => day


// best to use ENUM for fixed set of constants
enum Month {

enum Weekday {

 * The day "data node". Fill in constructors/accessors.
class Day {
    int year;
    Month month;
    Weekday weekday;
    String date; // hashkey
    String entry; // the entry

 * The calendar, simply mapping a unique date to it's day.
 * Create a date like: year + "-" + MONTH + "-" + DAY
HashMap<String, Day> calendar;

Since our data structure is not sparse, an independent view would have to simulate a full calendar. Display all days/generate all dates according to the rules of calendars, but only add a day to the HashMap if a new entry is saved.


  • Pretty efficient in space and time.
  • Above is over-simplified: wrap the HashMap in a class to arbitrate CRUD operations on days.
  • Assumes you don't need to manipulate months/years but only days. If this is wrong, and you'd like to e.g. get all days in a month, or delete a year, consider having a three-level map like year => month => day alongside above.
share|improve this answer
add comment

JTree from Swing can also be used as data structure.

But the first question you should ask yourself is "How I want to access the data".

share|improve this answer
add comment

You would need some sort of tree structure. This might be your starting point:

public class Node {
    private String label;
    private List<Node> children;
    //add Constructors, getters, setters, and member methods, etc

Node root = new Node();
//children of root are "01", "02", etc.
share|improve this answer
add comment

Definitely separate model data from view data.

Here is a different model than the one proposed in the paislee answer.

Map<Calendar, String> thisIsAllYouNeedForTheModel = new HashMap<Calendar, String>();
Calendar thisIsTheKey = Calendar.getInstance();

thisIsTheKey.set(Calendar.YEAR, theYear);
thisIsTheKey.set(Calendar.MONTH, theMonth);
thisIsTheKey.set(Calendar.DAY_OF_MONTH, theMonth);
thisIsTheKey.set(Calendar.HOUR, theHour);
thisIsTheKey.set(Calendar.MINUTE, theMinute);
thisIsTheKey.set(Calendar.SECOND, theSecond);
thisIsAllYouNeedForTheModel.put(thisIsTheKey, data);

Edit: silly me. Map<Calendar, String> is my suggestion.

share|improve this answer
is Map<String> shorthand for Map<String,String> ? –  paislee Jan 26 '12 at 0:36
my bad. I just typed into the answer. Map<Calendar, String> is my suggestion. –  DwB Jan 26 '12 at 3:03
add comment

I would suggest the TreeMap, but if you want to make experiments, just go ahead and use LinkedList. Acessing the data is pretty complex if you iterate through many lists. But it was a lot of fun experimenting.

EDIT: Here is a tutorial including a package that allows you to use a TreeMap or something similiar to a Tree: http://code.google.com/p/qed-java/wiki/HowToUseTree

share|improve this answer
how would i implement the tree map? –  Michael Jan 25 '12 at 20:22
at least you do not have to implement it. it is already implemented there. but if you want to do it yourself just dive into the sourcecode of the TreeMap structure and go for it :-) –  vlad Dec 14 '12 at 21:24
add comment

Your Answer


