0

I have a stream of strings from a csv file. These strings are converted to arrays and must be put in an Object's setter and the Object in a hashMap as a value. How do i concatenate all comming Arrays into one and only then use the Set method? Is there any better solution than concatenating the arrays before the set method?

Here is my code:

HashMap<Integer, Publication> innerMap = new HashMap<>();
try {

        CsvReader csv = new CsvReader(filename);

        csv.readHeaders();

        while (csv.readRecord()) { 
            int id = Integer.parseInt(csv.get("ID"));             
            Publication pub = new Publication();
            String names = csv.get("Names");
            String[] namesArr = names.split(",");                
            if (!innerMap.containsKey(id)) {
                innerMap.put(id, new Publication());
            } 
            String[] merged = ????
            pub.setNames(merged);
            innerMap.put(au.getIdx(), pub);
        }
        csv.close();

    } catch (IOException e) {
        System.out.println("Exception : " + e);

    }
Deksterious
  • 116
  • 2
  • 12

3 Answers3

1

Store them in a List first:

List<String[]> list = new ArrayList<>;
...
list.add(namesArr);

Then, once you've finished reading:

int size = 0;
for (String[] arr : list) {
   size += arr.length;
}
List<String> all = new ArrayList<>(size);
for (String[] arr : list) {
  all.addAll(Arrays.asList(arr));
}

The first loop helps to allocate the necessary memory to hold all of the data (otherwise there may be lots of reallocations and array copying in the ArrayList internally while you are adding elements to it in the second loop).

Dragan Bozanovic
  • 23,102
  • 5
  • 43
  • 110
  • I can't do that. I need to store arrays of specific id's. This is why i am using a hashmap and storing the an object with a concatenated array as a value – Deksterious Jan 29 '16 at 19:28
  • Ok, then just use `Map>` to store all of the arrays per id, then just do the above for all of the lists. – Dragan Bozanovic Jan 29 '16 at 19:44
1

this has already been answered using Apache commons - How can I concatenate two arrays in Java?

Here's a pure java 8 way

    String[] arr1 = { "a", "b", "c", "d" };
    String[] arr2 = { "e", "f", "g" };
    Stream<String> stream1 = Stream.of(arr1);
    Stream<String> stream2 = Stream.of(arr2);
    String[] arr = Stream.concat(stream1, stream2).toArray(String[]::new);
Community
  • 1
  • 1
denov
  • 11,180
  • 2
  • 27
  • 43
0

It looks like that if the map key exists, you want to extract the values, append additional values and then put it back in.

I would use a getter, then run this concat function which returns a new array. Since an Array is capped by its size, you cant grow it unless you make a new array and copy everything over.

to concat 2 string arrays where A comes first:

String[] concat( String[] a, String[] b){
  String[] out = new String[a.length + b.length]();
  int i = 0;
  for (int j = 0; j < a.length; j++){
    out[i] = a[j]
    i++;
  }
  for (int j = 0; j < b.length; j++){
    out[i] = b[j];
    i++;
  }
  return out;
}
Fallenreaper
  • 10,222
  • 12
  • 66
  • 129