I want to be able to get a particular element in a list. However, that list is contained in another list. The reason so, is that I read in a properties file, with keys having more than one value assigned to it separated by commas. I create a list for each key and its values, and add it to another list which contains multiple lists for each key and its values.
# Menu properties file
menuEntryPoint=0
indent=\t\t\t
lines=---------------------------------------------------------
mainMenu=M a i n M e n u
0.0=First Sub Menu, openMenu(1)
0.1=Second Sub Menu, openMenu(2)
0.2=Third Sub Menu, openMenu(3)
1=First Sub Menu
1.0=Execute this command, executeCommand(...)
1.1=Execute another command, executeCommand(...)
1.2=Execute other command, openMenu(A)
2=First Sub Menu
2.0=Execute this command, executeCommand(...)
2.1=Execute another command, executeCommand(...)
2.2=Execute other command, openMenu(A)
3=First Sub Menu
3.0=Execute this command, executeCommand(...)
3.1=Execute another command, executeCommand(...)
3.2=Execute other command, openMenu(A)
Below is code I wrote to print it to the screen:
List<List<String>> menuProperties = new ArrayList<List<String>>();
public void configMenuDisplayProperties() throws IOException {
Date date = new Date();
Properties menuProp = new Properties();
FileInputStream in = new FileInputStream("C:\\Test\\menu.properties");
menuProp.load(in);
//Create list of lists for properties
menuProperties.add(getPropertyList(menuProp, "0"));
menuProperties.add(getPropertyList(menuProp, "1"));
menuProperties.add(getPropertyList(menuProp, "2"));
menuProperties.add(getPropertyList(menuProp, "3"));
System.out.println(date.toString() + "\t\t" + menuProp.getProperty("developmentArea") + "\n\n");
System.out.println(menuProp.getProperty("indent") + menuProp.getProperty("mainMenu") + "\n");
for (Enumeration<?> e = menuProp.propertyNames(); e.hasMoreElements(); ) {
String name = (String)e.nextElement();
String value = menuProp.getProperty(name);
if (name.startsWith(menuProp.getProperty("menuEntryPoint"))) {
System.out.println(menuProp.getProperty("indent") + name + "\t" + menuProp.getProperty(name));
}
}
}
Output:
Mon Jun 08 13:39:43 EDT 2015
M a i n M e n u
0.2 Third Sub Menu, openMenu(C)
0.1 Second Sub Menu, openMenu(2)
0.0 First Sub Menu, openMenu(1)
How exactly can I print out a certain value of a key that is in a list of lists? I am trying to get my output to look like this, but I'm having trouble doing so.
Mon Jun 08 13:39:43 EDT 2015
M a i n M e n u
0.0 First Sub Menu
0.1 Second Sub Menu
0.2 Third Sub Menu
I tried doing the following
System.out.println(menuProp.getProperty("indent") + name + "\t" + menuProp.getProperty(name));
But I am getting the first output you see... Any help would be greatly appreciated.
Check below quick code based solution, there is no such API based solution which can meet your requirement.
If you want to get all the 1st comma values then use
HashMap<String, String> indexedHashMap = getValueBashedOnIndex(hashMap, 1);
, and if 3rd comma values then useHashMap<String, String> indexedHashMap = getValueBashedOnIndex(hashMap, 3);
Basically second parameter of
getValueBashedOnIndex
is for which index value you want.test.properties