0

I looked everywhere for a possible solution for this but I can't find anything and got stuck.

The problem:

I have two files containing data, when I read them into Java I store them in two separate arrays. These arrays look like this: (They are separated by a delimiter that is specified earlier on)

 [ID   pval   logFC ...  ...  ...,
 S145 0.02   2     ...  ...  ...,
 S252 0.01   4     ...  ...  ...,
 ...  ...   ...    ...  ...  ...]  

 [ID   pval   logFC ...  ...  ...,
 S145 0.02   2     ...  ...  ...,
 S252 0.01   4     ...  ...  ...,
 ...  ...   ...    ...  ...  ...]

    ... <--- can be anything

So they don't have to have the same header columns. What I want to do is merge these two arrays into one file, where shared headers are stacked beneath each other and not shared headers are added but filled for the other with null.

Shared header    not shared header
a                a
a                a
a                a
a                a
b                null
b                null
b                null

In the end everything should be written to a file that is called combined.

Does anyone have an idea what classes could be used to do this? Are there already existing classes that can handle this kind of merges?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • possible duplicate: http://stackoverflow.com/questions/8795945/merging-two-maps – guido Oct 09 '14 at 14:03
  • @guido looks more like he's asking what to use to do the merge, not how to merge. – RockOnRockOut Oct 09 '14 at 14:23
  • 1
    @ChrOertlin I don't get what you're saying about the header columns. Are these 2D arrays? – RockOnRockOut Oct 09 '14 at 14:24
  • @RockOnRockOut the arrays come from a file reading function: I read the files line by line and store the line in an array. So every array element is a representation of a row in my data file. So they only have an array[] dimension not an array[][] dimension – ChrOertlin Oct 10 '14 at 08:58

1 Answers1

0

You can use Collection<Map<String, String>> for storing your data instead of arrays. In this case your task will be solved trivially:

Collection<Map<String, String>> data1 = loadData("source1.dat");
Collection<Map<String, String>> data2 = loadData("source2.dat");
Collection<Map<String, String>> merged = new ArrayList<>();

merged.addAll(data1);
merged.addAll(data2);

Set<String> headers = new LinkedHashSet<>();

for (Map<String, String> map : merged) {
    headers.addAll(map.keySet());
}
ursa
  • 4,404
  • 1
  • 24
  • 38