简体   繁体   中英

Canonical way to store lists in properties

I have a Map<String, Set<String>> object populated by categories and sets of tag mapped to them, eg. <"disease", Stream.of("nn", "nnp", "nns", "nnps)> . I would like to move this to a properties file, where keys (categories) would map to Sets (values). That would then be loaded during setup, rather than being hardcoded in the codebase.

An obvious solution would be to have a single category in line and then POS tags separated by commas. However, this solution seems very inelegant; is there a better way of doing that?

Another approach. Have a look at the file created here called objects.xml, which you could write by hand:

import java.util.*;
import java.io.*;
import java.beans.XMLEncoder;

public class MapSet {
    public static void main(String[] args) {
        try {
            Map<String, Set<String>> objects;
            objects = new HashMap<>();
            objects.put("disease", new HashSet<String>(Set.of("nn", "nnp", "nns", "nnps")));
            try(XMLEncoder enc = new XMLEncoder(new FileOutputStream("objects.xml"))) {
                enc.writeObject(objects);
            }
        }
        catch(Throwable t) {
            t.printStackTrace();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM