I have a Prop file like this
abc.name = John
abc.id = 1234
abc.dep = sales
bac.name = Jen
bac.id = 3422
bac.dep = marketing
Now I need to convert it to a map where keys will be abc, bac and the value would be a object which has the 3 fields, name, id and dep.
public class Identity{
private String name;
private Long id;
private dep;
}
The map I want to create is HashMap<String, Identity>(). I have done this by reading the Prop file and iterate over the map the, split the keys and did computeIfAbsent then a switch statement for mapping the name, id and dep.
Is there any easier and cleaner way to do it. Is there any library present which can perform this.
I have a Prop file like this
abc.name = John
abc.id = 1234
abc.dep = sales
bac.name = Jen
bac.id = 3422
bac.dep = marketing
Now I need to convert it to a map where keys will be abc, bac and the value would be a object which has the 3 fields, name, id and dep.
public class Identity{
private String name;
private Long id;
private dep;
}
The map I want to create is HashMap<String, Identity>(). I have done this by reading the Prop file and iterate over the map the, split the keys and did computeIfAbsent then a switch statement for mapping the name, id and dep.
Is there any easier and cleaner way to do it. Is there any library present which can perform this.
I used Snake YML to resolve this. I have to change the prop file to YML file and read it through snake yml. the used ObjectMapper to convert it to Map<String, Identity>
Properties
is already essentially aMap
so, for variableprops
of typeProperties
, you could do something like the following:Map<String, Identity> identities = new TreeMap<>();for (Iterator<Map.Entry<Object, Object>> i = new TreeMap<>(props).entrySet().iterator();i.hasNext();) {var e = i.next();String key = e.getKey().toString().replaceAll("\\..*", "");identities.put(key, new Identity(e.getValue().toString(),Long.valueOf(i.next().getValue().toString()), i.next().getValue().toString()));}
– g00se Commented Jan 16 at 12:29