Sea~Sky 2020-01-16 07:10 采纳率: 71.4%
浏览 468
已采纳

将List中的一个值取出来作为键

[
{
"offline": 2,
"station": "北流村",
"online": 23
},
{
"offline": 31,
"station": "马池口",
"online": 210
},
{
"offline": 92,
"station": "北七家",
"online": 0
},
{
"offline": 50,
"station": "龙园",
"online": 88
},
{
"offline": 16,
"station": "南口",
"online": 162
},
{
"offline": 44,
"station": "兴寿",
"online": 40
},
{
"offline": 90,
"station": "百善",
"online": 39
},
{
"offline": 172,
"station": "平西府",
"online": 48
},
{
"offline": 41,
"station": "霍营",
"online": 40
},
{
"offline": 72,
"station": "南邵",
"online": 72
},
{
"offline": 17,
"station": "居庸关",
"online": 0
},
{
"offline": 51,
"station": "崔村",
"online": 62
},
{
"offline": 51,
"station": "回龙观",
"online": 272
},
{
"offline": 156,
"station": "天通苑",
"online": 104
},
{
"offline": 47,
"station": "史各庄",
"online": 39
},
{
"offline": 29,
"station": "延寿",
"online": 0
},
{
"offline": 114,
"station": "昌平",
"online": 95
},
{
"offline": 28,
"station": "十三陵",
"online": 0
},
{
"offline": 41,
"station": "东小口",
"online": 41
},
{
"offline": 87,
"station": "小汤山",
"online": 124
},
{
"offline": 26,
"station": "阳坊",
"online": 213
},
{
"offline": 354,
"station": "松园",
"online": 0
}
]
现在有这么一组数据存在List里面,我想把这组数据变成下面这样的数据格式
{
"tiantongyuan": [
{
"offline": 156,
"station": "天通苑",
"online": 104
}
],
"dongxiaokou": [
{
"offline": 41,
"station": "东小口",
"online": 41
}
],
"xingshou": [
{
"offline": 44,
"station": "兴寿",
"online": 40
}
],
"beiliucun": [
{
"offline": 2,
"station": "北流村",
"online": 23
}
],
.......
}

展开全部

  • 写回答

5条回答 默认 最新

  • ZS12345678ZS 2020-01-16 07:43
    关注

    //我理解你是想把List变成map,但是map里的键是Sting类型,值是List<实体>,但是下面我给出的是键是Sting类型,值是实体类型;
    //-------------创建实体类-------------

    @Data
    public class OfStOn{
        private  int offline;
        private String station;
        private int online;
    }
    

    //-----------------------------------------------------------------
    //-------------创建中文转拼音的类--------------

    import net.sourceforge.pinyin4j.PinyinHelper;  
    import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;  
    import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;  
    import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;  
    import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;  
    import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
    
    public class ChineseTurnPinyin {
        /**  
         * getPingYin 
         * 返回中文拼音
         * @param src
         * @return String  
        */
        public static String getPingYin(String src) {  
            char[] t1 = null;  
            t1 = src.toCharArray();  
            String[] t2 = new String[t1.length];  
            HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();  
    
            t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);  
            t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);  
            t3.setVCharType(HanyuPinyinVCharType.WITH_V);  
            String t4 = "";  
            int t0 = t1.length;  
            try {  
                for (int i = 0; i < t0; i++) {  
                    // 判断是否为汉字字符  
                    if (java.lang.Character.toString(t1[i]).matches(  
                            "[\\u4E00-\\u9FA5]+")) {  
                        t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);  
                        t4 += t2[0];  
                    } else  
                        t4 += java.lang.Character.toString(t1[i]);  
                }  
                // System.out.println(t4);  
                return t4;  
            } catch (BadHanyuPinyinOutputFormatCombination e1) {  
                e1.printStackTrace();  
            }  
            return t4;  
        }
    
    }
    

    //---------------------------------------------------------------------
    //--------实际转换的代码------------------

    public HashMap<String,OfStOn> turnListToMap(List<OfStOn> ofstonList){
        HashMap<String,OfStOn> ofstonMap = new HashMap<String,OfStOn>();
        String key ="";
        OfStOn  ofston = new OfStOn();
        for(int i ;i<ofston.size();i++){
            ofston=ofston.get(i);
            key = ChineseTurnPinyin.getPingYin(ofston.getStation());
            ofstonMap.put(key,ofston);
        }
        return ofstonMap;
    }
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部