question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Best way to save and load a HashMap as a sequence

See original GitHub issue

Hello, thank you for eo-yaml.

I am trying to use it to save/load my datastructure, which has a HashMap<String, CombinedEvent>.

I am confused as to how to use YamlSequence and it is not clear from the documentation how to do this. Documented sequences are basically lists of strings.

My code to dump and load the HashMap is as follows:

public Class CombinedEvents() {
private HashMap<String, CombinedEvent> all = new HashMap<>();
.
.
.

public YamlMapping toEOYaml() {
    YamlSequenceBuilder combinedEventSeq = Yaml.createYamlSequenceBuilder();
    for (string ceName : this.keySet()) {
        combinedEventSeq = combinedEventSeq.add(this.get(ceName.toEOYaml(ceName));
    }
    return Yaml.createYamlMappingBuilder()
                 .add("all", combinedEventSeq.build()
                 ).build();

public static CombinedEvents fromEOYaml(YamlMapping yml) {
    CombinedEvents ces = new CombinedEvents();
    final YamlSequence combinedEventSeq = ym.yamlSequence("all');
    for (final YamlNode ceNode : combinedEventSeq) {
        // -- not sure how to read back the sequence of CombinedEvents
   }

This writes out a good yaml file, but I cannot figure out how to read it back. Also, I am not sure what the best OO way is to write a sequence from a HashMap. All the examples are writing a fixed length sequence. My code looks a bit kludgy to me in not having combinedEventSeq final.

It would be good to have a working example of saving and loading such a hashmap and I would be willing to provide one for inclusion, if only I could get it to work.

Any help would be appreciated.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
amihaiemilcommented, Dec 10, 2020

@piet8stevens you’re welcome!

The only weird thing remaining, which does not preclude this from working, is that the ‘-’ is on its own line when a mapping follows it, but not when a String follows as per

This is the way we’ve implemented eo-yaml: mapping elements in sequences start on a second line after the -. We had quite a few complaints about that because apparently the YAML specification allows both options (on the same line and after).

It was only after we implemented this project that we realized the YAML spec is actually too flexible. Its authors are literally saying: “that should work, and that one as well… oh, let’s make it work in that other way too”…

0reactions
charlesmikecommented, Dec 10, 2020

@amihaiemil thank you for resolving this ticket. I’ve just added it to your active invoice. You can always check all your invoices and more on the Contributor Dashboard.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to write and read a file with a HashMap? - Stack Overflow
The simplest solution that I can think of is using Properties class. Saving the map: Map<String, String> ldapContent = new HashMap<String, ...
Read more >
Fastest way to save and load a HashMap - help
Another thing you could try is a flat memory-mapped file. This is possible if you can encode your data in a way that...
Read more >
Tips For a Successful HashMap Implementation In Java. - Xperti
The simple way to get multiple values from a HashMap is by iterating over the whole Map. In certain cases, it will be...
Read more >
HashMap in Java with Examples - GeeksforGeeks
This class makes no guarantees as to the order of the map. To use this class and its methods, you need to import...
Read more >
Java HashMap, LinkedHashMap and TreeMap - w3resource
When we need to maintain insertion order while iterating we should use LinkedHashMap. · LinkedHashMap provides all the methods same as HashMap.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found