1

It is part of code of a weather application.

I had written following code in strings.xml, but now I want to write in java file. I don't know how to write the following code in HashMap and obtain the value from it.

Following are the string-arrays in my XML.

   <!-- Weather condtion 5,6 together with Dress -->
    <!-- Weather condtion 11,12 together with Dress -->
    <string-array name="eleven">
        <item name="dress_6">dress_6</item>
        <item name="dress_0">dress_0</item>
        <item name="dress_1">dress_1</item>
    </string-array>
    <!-- Weather condtion 9 together with Dress -->
    <string-array name="nine">
        <item name="dress_6">dress_6</item>
        <item name="dress_4">dress_4</item>
        <item name="dress_14">dress_14</item>
    </string-array>
    <!-- Weather condtion 5,6 together with Dress -->
    <string-array name="five">
        <item name="dress_2">dress_2</item>
        <item name="dress_8">dress_8</item>
        <item name="dress_6">dress_6</item>
    </string-array>
    <!-- condtion 2 -->
    <string-array name="two">
        <item name="dress_11">dress_11</item>
        <item name="dress_5">dress_5</item>
        <item name="dress_0">dress_0</item>
    </string-array>
    <string-array name="twentyFive">
        <item name="dress_0">dress_0</item>
        <item name="dress_3">dress_3</item>
        <item name="dress_8">dress_8</item>
    </string-array>
    <string-array name="twentySix">
        <item name="dress_12">dress_12</item>
        <item name="dress_13">dress_13</item>
        <item name="dress_5">dress_5</item>
    </string-array>
    <string-array name="thirtySix">
        <item name="dress_11">dress_11</item>
        <item name="dress_9">dress_9</item>
        <item name="dress_4">dress_4</item>
    </string-array>
    <string-array name="thirtySeven">
        <item name="dress_12">dress_12</item>
        <item name="dress_13">dress_13</item>
        <item name="dress_3">dress_3</item>
    </string-array>
Sufian
  • 6,405
  • 16
  • 66
  • 120
J V
  • 11
  • 6

2 Answers2

0

Lets try this:

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

import java.util.AbstractMap;
import java.util.HashMap;
import java.util.Map;

public class Test {

public static void main(String[] args) {

    Map<String,String> map = new HashMap<String,String>();
    map.put("name","chris");
    map.put("island","faranga");

    XStream magicApi = new XStream();
    magicApi.registerConverter(new MapEntryConverter());
    magicApi.alias("root", Map.class);

    String xml = magicApi.toXML(map);
    System.out.println("Result of tweaked XStream toXml()");
    System.out.println(xml);

    Map<String, String> extractedMap = (Map<String, String>) magicApi.fromXML(xml);
    assert extractedMap.get("name").equals("chris");
    assert extractedMap.get("island").equals("faranga");

}

public static class MapEntryConverter implements Converter {

    public boolean canConvert(Class clazz) {
        return AbstractMap.class.isAssignableFrom(clazz);
    }

    public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {

        AbstractMap map = (AbstractMap) value;
        for (Object obj : map.entrySet()) {
            Map.Entry entry = (Map.Entry) obj;
            writer.startNode(entry.getKey().toString());
            Object val = entry.getValue();
            if ( null != val ) {
                writer.setValue(val.toString());
            }
            writer.endNode();
        }

    }

    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {

        Map<String, String> map = new HashMap<String, String>();

        while(reader.hasMoreChildren()) {
            reader.moveDown();

            String key = reader.getNodeName(); // nodeName aka element's name
            String value = reader.getValue();
            map.put(key, value);

            reader.moveUp();
        }

        return map;
    }

}

}

Check THIS out for more information.

Community
  • 1
  • 1
Almett
  • 876
  • 2
  • 11
  • 34
0

I think you are looking for a way to populate the HashMap

You could use the following

Map<String, List<String>> map = new HashMap<>();
map.put("eleven", Arrays.asList("dress_6", "dress_0", "dress_1"));

if you need a more complex structure you could use something like

Map<String, Map<String, String>> map = new HashMap<>();

To retrieve from the map you could use something like

List<String> list = map.get("eleven");

This list will contain the elements that you added "dress_6", "dress_0", "dress_1"

You can access these elements in a for loop as

for(String item : list) {
    System.out.println("Item: " + item);
}

You haven't specified your exact use case but I would recommend not hard coding the configuration. You could read from the existing string.xml file and populate the Map on startup or first access.

Akshay Gehi
  • 362
  • 1
  • 12
  • Thanks for the nice reply. I would like to know about the following code more in detail like how to retrieve the value from it. map.put("eleven", Arrays.asList("dress_6", "dress_0", "dress_1")); Thanks in advance. Please use the for loop – J V Nov 19 '15 at 05:58