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.




For example,





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