Properties file with a list as the value for an individual key
Solution 1
Try writing the properties as a comma separated list, then split the value after the properties file is loaded. For example
a=one,two,three
b=nine,ten,fourteen
You can also use org.apache.commons.configuration and change the value delimiter using the AbstractConfiguration.setListDelimiter(char) method if you're using comma in your values.
Solution 2
The comma separated list option is the easiest but becomes challenging if the values could include commas.
Here is an example of the a.1, a.2, ... approach:
for (String value : getPropertyList(prop, "a"))
{
System.out.println(value);
}
public static List<String> getPropertyList(Properties properties, String name)
{
List<String> result = new ArrayList<String>();
for (Map.Entry<Object, Object> entry : properties.entrySet())
{
if (((String)entry.getKey()).matches("^" + Pattern.quote(name) + "\\.\\d+$"))
{
result.add((String) entry.getValue());
}
}
return result;
}
Solution 3
If this is for some configuration file processing, consider using Apache configuration. https://commons.apache.org/proper/commons-configuration/javadocs/v1.10/apidocs/index.html?org/apache/commons/configuration/PropertiesConfiguration.html It has way to multiple values to single key- The format is bit different though
key=value1,value2,valu3
gives three values against same key.
Solution 4
Your logic is flawed... basically, you need to:
- get the list for the key
- if the list is null, create a new list and put it in the map
- add the word to the list
You're not doing step 2.
Here's the code you want:
Properties prop = new Properties();
prop.load(new FileInputStream("/displayCategerization.properties"));
for (Map.Entry<Object, Object> entry : prop.entrySet())
{
List<String> categoryList = categoryMap.get((String) entry.getKey());
if (categoryList == null)
{
categoryList = new ArrayList<String>();
LogDisplayService.categoryMap.put((String) entry.getKey(), categoryList);
}
categoryList.add((String) entry.getValue());
}
Note also the "correct" way to iterate over the entries of a map/properties - via its entrySet()
.
Solution 5
Create a wrapper around properties and assume your A value has keys A.1, A.2, etc. Then when asked for A your wrapper will read all the A.* items and build the list. HTH
NIVESH SENGAR
I m a funluving person working as a Software developer. This is my learning phase and hope I'll learn sumthing from u guys...:)
Updated on July 14, 2022Comments
-
NIVESH SENGAR almost 2 years
For my program I want to read a key from a properties file and an associated List of values for the key.
Recently I was trying like thatpublic static Map<String,List<String>>categoryMap = new Hashtable<String, List<String>>(); Properties prop = new Properties(); try { prop2.load(new FileInputStream(/displayCategerization.properties)); Set<Object> keys = prop.keySet(); List<String> categoryList = new ArrayList<String>(); for (Object key : keys) { categoryList.add((String)prop2.get(key)); LogDisplayService.categoryMap.put((String)key,categoryList); } System.out.println(categoryList); System.out.println("Category Map :"+LogDisplayService.categoryMap); keys = null; prop = null; } catch (Throwable e) { e.printStackTrace(); }
and my properties file is like below -
A=APPLE A=ALPHABET A=ANT B=BAT B=BALL B=BUS
I want for key A there should be a list which contain
[APPLE, ALPHABET,ANT]
and B contain[BAT,BALL,BUS]
.
So Map should be like this{A=[APPLE, ALPHABET,ANT], B=[BAT,BALL,BUS]}
but I get
{A=[ANT], B=[BUS]}
I searched on the internet for such a way but found nothing. I wish there should be a way. Any help?