-2

I want to create a table such that one key store more than one value against it. Such that I can calculate the number of values against a particular key. I want to create a calendar kind of thing, for each date some number of tasks numbers will be there. Later I can calculate number of tasks against one particular date. I am unable to use TreeMap<Integer,ArrayList<Integer>>, as i can only insert list against integer but i need to add one integer value against key at a time. If I use TreeMap<Integer,Integer>, then after inserting one value against 1 key value, when I insert another value for the same key, the previous value is replaced. I have a function where I have to validate the key and then for that key I will insert one value. i.e one value can be inserted at a time. Please refer the image Please provide some solution.

enter image description here

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555

3 Answers3

1

Google has a library called 'guava', which has Multimaps.
You can add their jar to your solution and use it.
http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Multimap.html

Yoav Gur
  • 1,366
  • 9
  • 15
0

For Java, you can use Map<Calendar, List<Integer>>. The Calendar object is the key and the value is a list of Integer.

Dicky Ho
  • 244
  • 3
  • 15
0

You can try something like(just rough implementation you can change data types according to your need):

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class Task {

    public static void main(String[] args) {
        Map<String, List<String>> task = new TreeMap<String, List<String>>();

        List<String> taskList1 = new ArrayList<String>();
        taskList1.add("234");
        taskList1.add("56");
        List<String> taskList2 = new ArrayList<String>();
        taskList2.add("23");
        taskList2.add("34");
        taskList2.add("124");

        task.put("1", taskList1);
        task.put("2", taskList2);

        for (Map.Entry<String, List<String>> entry : task.entrySet()) {
            System.out.println(entry.getKey() + "/" + entry.getValue());
        }
    }
}
user2173372
  • 483
  • 7
  • 17