Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse Map from properties files

This is a follow-up to my previous question (found here). My properties files so far consists of simple key-value pairs, like ints and Strings. I would now like to use it to place some more advanced structures in it, more specifically I need a Map<A, Integer> where A is a class i have defined, for example like so:

foo=bar,5;baz,10

Is this possible? If so, how should I format the file and parse the map, respectively?

Are there perhaps better ways to solve this?

like image 251
pg-robban Avatar asked Feb 14 '26 19:02

pg-robban


1 Answers

Have you considered using a split() and trim().

Please see below:

ResourceBundle rb = ResourceBundle.getBundle(Prop.class.getName());

    String fooValue = rb.getString("foo");
    String[] firstSplit = fooValue.split(";");
    for(String first : firstSplit){
        String firstTrim = first.trim();

        String[] intAndString = firstTrim.split(",");
        if(intAndString.length == 2){
            String intString = intAndString[0].trim();
            String stringVal = intAndString[1].trim();

            //TODO add entries here or return value.

        }
    }
like image 144
Koekiebox Avatar answered Feb 17 '26 07:02

Koekiebox