-1

So I have a Map defined as

Map<Integer, Object[]> data = new TreeMap<Integer, Object[]>();

and I am adding data as I loop through and read a text file. For most of the data, I'll add it and move on. However, there is data in the file that needs to applied back to an existing entry without deleting it or removing it's position. Example.

data.put(counter, new Object[] {ex1, ex2, ex3, ex4});

So, if I were to add an entry expecting to have data later on I would need to add (EDIT: it can be appended), is there way to keep the existing data and append new data? Example.

First,

data.put(counter, new Object[] {"testData1", "testData2", "testData3", "testData4"});

When I loop to data that needs to be added, I need to be able to add "testData5" to the end position while only knowing the counter's value when the data was originally added.

Is there a way to do this without deleting the existing data in that specific entry?

Edit: The data can be appended, changed example for this.

Racehorse35
  • 121
  • 10
  • So you don't have any way of knowing that the data must be put in the third element of the array, in this example? – Zircon Jun 16 '16 at 15:16
  • Im not sure I understand your question. Let me revise, the element that needs to be added can be appended, but I need to have all other information preserved. – Racehorse35 Jun 16 '16 at 15:22
  • 4
    How about using List instead of arrays? – Leonard Brünings Jun 16 '16 at 15:25
  • 1
    It's not clear to me what you're asking. An array cannot change size. Do you want a `List` as the value type or are you looking for a `Multimap`, where multiple mappings can occur for a single key? – Sotirios Delimanolis Jun 16 '16 at 15:27

1 Answers1

1

Using your arrays, it's quite messy. I agree with the comments that you should use Lists, which allow you to just use the list reference and not have to set anything back on the map.

Using Arrays (Nasty!)

while(/* fancy argument */) {
    int counter; //you promised me this
    String argument; //and this

    Object[] foo = data.get(counter); //find what is stored on the map  
    Object[] bar; //our expected result

    if(foo == null) { //create a new array and append it
        bar = new Object[1];
        bar[0] = argument;
    }
    else { //fill the new array
        bar = new Object[foo.length+1];
        for(int i = 0; i < foo.length; i++) {
            bar[i] = foo[i];
        }
        bar[foo.length] = argument;
    }

    data.set(counter, bar); //put the updated array into the map
}

Using Lists (Clean!)

while(/* fancy argument */) {
    int counter; //you promised me this
    String argument; //and this

    List<Object> foo = data.get(counter); //find what is stored on the map  
    //and we don't need bar since we can play with foo

    if(foo == null) { //we have a new list we need to add
        foo = new ArrayList<>();
        data.set(counter, foo); //put it in the map
    }

    foo.add(argument); //add to list, don't need to re-put since it's already there
}
Compass
  • 5,867
  • 4
  • 30
  • 42