Insung Park Insung Park - 1 year ago 71
Java Question

Convert some specific file format into Map concisely via functional paradigm in Java 8

I want to convert some file format into Java Map.

My team uses some specific file format like below.

<<<element1s_key1>>>value1
<<<element1s_key2>>>value2
<<<element1s_key3>>>value3

<<<element2s_key1>>>value1
<<<element2s_key2>>>value2
<<<element2s_key3>>>value3

<<<element3s_key1>>>value1
<<<element3s_key2>>>value2
<<<element3s_key3>>>value3


For example,

<<<name>>>Mike
<<<age>>>10
<<<gender>>>M

<<<name>>>Mary
<<<age>>>15
<<<gender>>>F

<<<name>>>Yoon
<<<age>>>12
<<<gender>>>M

...


It can be possible there is some optional entry, that mean whole key and value line cannot exist and each key names are same for each elements.
I'm not sure is there any name calling like that format.(is there any name?)

So, My question as the title is

Could i convert that format into Java List - Map Structure via Java 8 Functional Paradigm?

For example like below.

[[("name", "Mike"), ("age", 10), ("gender", "M")], [("name", Mary), ... ]]


This file is so huge, So i cannot upload into memory entire file.
So i want to use some laziness. (Like Stream Structure)

Answer Source

First, I'll say that the data file format would probably have been better off in XML, which is intended for structured data. That doesn't exactly solve your problem with this data file, though it might be helpful for the future; JSON is another option as that's also a well-known data format.

As far as for this file, Files.lines() breaks an input file into a Stream, but then you'd have to process the Stream by groups of four items. Streams are supposed to be parallelizable, and your particular need doesn't lend itself well to that.

If you're intending to use this file as a flat-file database, it would probably be better in the long run to write one program to permanently translate it to a structured format like XML or JSON, and another module to access the XML or JSON data via streams.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download