IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    如何使HashMap一个key对应多个value?

    ShelWee发表于 2013-07-20 12:22:36
    love 0

    jscharts
    最近在做图表统计的时候,将数据存储在XML文件中。于是需要在后台处理好数据后再创建一个XML文件。由于JSCharts图表工具需要XML文件格式大体相同,所以以饼状图为例有:一个JSChart根节点,再有处于同一级的dataset 、colorset、optionset。
    我的思路是创建一个方法,参数为HashMap,传入dataset 、optionset节点下的属性值,第一个属性值作为HashMap的key,第二个属性值作为value。Colorset只有一个属性值,因此可用ArrayList来接收值。

    要生成的目标XML文件:

    
    
    	
    		
    		
    		
    		
    	
    	
    		
    		
    		
    		
    	
    	
    		
    

    Java使用XmlPull创建XML文件示例代码:

    public static void createJSChartsXml(HashMap datas,ArrayList colors,HashMap options) throws Exception{
    		XmlSerializer serializer = new KXmlSerializer();
    		serializer.setOutput(new FileOutputStream(new File("D:\\data.xml")),"utf-8");
    		serializer.startDocument(null, true);
    		serializer.startTag(null, "JSChart");
    		serializer.startTag(null, "dataset");
    		serializer.attribute(null, "type", "pie");
    		Iterator iterator = datas.keySet().iterator();
    		while(iterator.hasNext()){
    			String key = iterator.next();
    			serializer.startTag(null, "data");
    			serializer.attribute(null, "unit", key);
    			serializer.attribute(null, "value",datas.get(key));
    			serializer.endTag(null, "data");
    		}
    		serializer.endTag(null, "dataset");
    		serializer.startTag(null, "colorset");
    		for(String color : colors){
    			serializer.startTag(null, "color");
    			serializer.attribute(null, "value", color);
    			serializer.endTag(null, "color");
    		}
    		serializer.endTag(null, "colorset");
    		serializer.startTag(null, "optionset");
    		Iterator op_iterator = options.keySet().iterator();
    		while(op_iterator.hasNext()){
    			String op_key = op_iterator.next();
    			serializer.startTag(null, "option");
    			serializer.attribute(null, "set", op_key);
    			serializer.attribute(null, "value",options.get(op_key));
    			serializer.endTag(null, "option");
    		}
    		serializer.endTag(null, "optionset");
    		serializer.endTag(null, "JSChart");
    		serializer.endDocument();
    }

    这段代码在创建的时候由于HashMap的key不能重复,所以导致生成下面四句的时候变成只有一句,因为后面三个value都被覆盖了。

    为保证正确生成XML需要对代码进行部分修改,将value类型改为字符串数组即可解决:

    import java.io.File;
    import java.io.FileOutputStream;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Iterator;
    
    import org.kxml2.io.KXmlSerializer;
    import org.xmlpull.v1.XmlSerializer;
    
    public class XmlTools {
    	
    	/**
    	 * 创建JSCharts xml文件
    	 * @author ShelWee
    	 * @param datas key为unit的值,value为value的值
    	 * @param colors
    	 * @param options key为set的值,value为value的值
    	 * @throws Exception
    	 */
    	public static void createJSChartsXml(HashMap datas,ArrayList colors,HashMap options) throws Exception{
    		XmlSerializer serializer = new KXmlSerializer();
    		serializer.setOutput(new FileOutputStream(new File("D:\\data.xml")),"utf-8");
    		serializer.startDocument(null, true);
    		serializer.startTag(null, "JSChart");
    		serializer.startTag(null, "dataset");
    		serializer.attribute(null, "type", "pie");
    		Iterator iterator = datas.keySet().iterator();
    		while(iterator.hasNext()){
    			String key = iterator.next();
    			serializer.startTag(null, "data");
    			serializer.attribute(null, "unit", key);
    			serializer.attribute(null, "value",datas.get(key));
    			serializer.endTag(null, "data");
    		}
    		serializer.endTag(null, "dataset");
    		serializer.startTag(null, "colorset");
    		for(String color : colors){
    			serializer.startTag(null, "color");
    			serializer.attribute(null, "value", color);
    			serializer.endTag(null, "color");
    		}
    		serializer.endTag(null, "colorset");
    		serializer.startTag(null, "optionset");
    		Iterator op_iterator = options.keySet().iterator();
    		while(op_iterator.hasNext()){
    			String op_key = op_iterator.next();
    			String[] values = options.get(op_key);
    			for(String op_value:values){
    				serializer.startTag(null, "option");
    				serializer.attribute(null, "set", op_key);
    				serializer.attribute(null, "value",op_value);
    				serializer.endTag(null, "option");
    			}
    		}
    		serializer.endTag(null, "optionset");
    		serializer.endTag(null, "JSChart");
    		serializer.endDocument();
    	}
    
    	/**
    	 * 调用示例
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		HashMap hashMap = new HashMap();
    		hashMap.put("A", "40");
    		hashMap.put("B", "16");
    		hashMap.put("C", "20");
    		hashMap.put("D", "10");
    		hashMap.put("E", "10");
    		hashMap.put("F", "4");
    		
    		ArrayList colors = new ArrayList();
    		colors.add("#99CDFB");
    		colors.add("#3366FB");
    		colors.add("#0000FA");
    		colors.add("#F8CC00");
    		colors.add("#F89900");
    		colors.add("#F76600");
    		
    		HashMap options = new HashMap();
    		options.put("setSize",new String[]{"600, 300"});
    		options.put("setTitle", new String[]{"'XML自动创建图表'"});
    		options.put("setTitleFontFamily", new String[]{"'Times New Roman'"});
    		options.put("setTitleFontSize", new String[]{"14"});
    		options.put("setTitleColor", new String[]{"'#0F0F0F'"});
    		options.put("setPieRadius",new String[]{ "95"});
    		options.put("setPieValuesColor", new String[]{"'#FFFFFF'"});
    		options.put("setPieValuesFontSize",new String[]{ "9"});
    		options.put("setPiePosition", new String[]{"180, 165"});
    		options.put("setShowXValues", new String[]{"false"});
    		options.put("setLegend", new String[]{"'#99CDFB', 'Papers where authors found'","'#3366FB', 'Papers which cite from other articles'",
    				"'#0000FA', 'Papers which cite from news'","'#F8CC00', 'Papers which lack crucial'","'#F89900', 'Papers with different conclusion'",
    				"'#F76600', 'Papers with useful information'"});
    		options.put("setLegendShow", new String[]{"true"});
    		options.put("setLegendFontFamily",new String[]{ "'Times New Roman'"});
    		options.put("setLegendFontSize",new String[]{ "10"});
    		options.put("setLegendPosition",new String[]{ "350, 120"});
    		options.put("setPieAngle",new String[]{ "30"});
    		options.put("set3D", new String[]{"true"});
    		try {
    			createJSChartsXml(hashMap, colors, options);
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    }



沪ICP备19023445号-2号
友情链接