What data structure to use?

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

  • year 2012)
    • 01 (month)
      • 01 (day)
        • Hi i'm a string
      • 02
      • ...
    • 02
    • 03
    • 04
    • ...

I was thinking of using TreeMap, but I don't know how to do it. Any ideas?

+4
source share
5 answers

Considerations that you are interested in managing calendar entries :

  • There are endless possible dates - do not waste your memory on unused days.
  • Given the date you want to quickly access it - use an array or hash search
  • Each day has a unique date - card date => day

MODEL

// best to use ENUM for fixed set of constants enum Month { JANUARY, FEBRUARY, ... , NOVEMBER, DECEMBER } enum Weekday { SUNDAY, MONDAY, ... , FRIDAY, SATURDAY } /** * 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 day. * Create a date like: year + "-" + MONTH + "-" + DAY */ HashMap<String, Day> calendar; 

BROWSE
Since our data structure is not sparse, an independent view will need to model a complete calendar. Display all days / generate all dates according to calendar rules, but add only a HashMap day if a new record is saved.

NOTES

  • Pretty effective in space and time.
  • Simplified above: wrap the HashMap in a class to arbitrate CRUD operations for days .
  • It is assumed that you do not need to manipulate for months / years, but only for days. If this is not the case, and you would like, for example, to get all days in a month or delete a year , think of a three-layer map, such as year => month => day , next to it.
+1
source

Swing's JTree can also be used as a data structure.

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

0
source

You will need some kind of tree structure. This could 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(); root.setLabel("2012"); //children of root are "01", "02", etc. 
0
source

Definitely separate model data from view data.

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

 Map<Calendar, String> thisIsAllYouNeedForTheModel = new HashMap<Calendar, String>(); Calendar thisIsTheKey = Calendar.getInstance(); thisIsTheKey.clear(); 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: stupid me. Map<Calendar, String> is my suggestion.

0
source

I would suggest TreeMap, but if you want to do experiments just use LinkedList. Recognizing the data is quite difficult if you iterate through many lists. But it was a lot of fun experimenting.

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

-one
source

Source: https://habr.com/ru/post/1393030/


All Articles