list去重、list统计,以及(String、JSONObject、String转list、List<String>转List<Integer>、JSONArray)之间的转换

package com.wendao.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Transformer;
import org.json.JSONArray;
import org.json.JSONObject;

public class Test4 {
    public static void main(String[] args) {
        String s = "{\r\n"
                + "    \"zqt\": \"101,102,102,103,103,103,104,104,104,104,105,106,106,106,106,107,108,109,110\",\r\n"
                + "\r\n"
                + "    \"cwt\": \"101,102,102,103,103,103,104,104,104,104,105,106,106,106,106,107,108,109,110\"\r\n" + "}";
        //String转JSONObject
        JSONObject os = new JSONObject(s);
        System.out.println("String转JSONObject:  "+os);
        
        //获取JSONObject其中一个的值
        String z = os.get("zqt").toString();
        System.out.println("获取JSONObject其中一个的值:  "+z);
        
        //String转list
        List<String> Z = Arrays.asList(z.split(","));
        System.out.println("String转list:  "+Z);
        
        //List<String>转List<Integer>
        List<Integer> a = new ArrayList<Integer>();
        CollectionUtils.collect(Z, new Transformer() {
            public Object transform(Object o) {
                return Integer.valueOf(o.toString());
            }
        }, a);
        System.out.println("List<String>转List<Integer>:  "+a);
        
        //List之统计元素项以及元素出现的次数
        List<Map<String, Object>> collectionsZqt = CollectionsList(Z);
        System.out.println("List之统计元素项以及元素出现的次数:  "+collectionsZqt.toString());
        
        //List转JSONArray
        JSONArray jsonArray = new JSONArray(collectionsZqt);
        System.out.println("List转JSONArray:  "+jsonArray);
        
        //list去重并转成String
//        String z = "101,102,102,103,103,103,104,104,104,104,105,106,106,106,106,107,108,109,110";
        Replace(z);
    }

    
    
    
//静态方法

    //list去重并转成String
    private static void Replace(String z) {
        //先转换成list
        List<String> str = Arrays.asList(z.split(","));
        //转成set去重
        Set<String> hs = new HashSet<String>(str);
        //再转换成list
        List result = new ArrayList(hs);
        //把list拆分之后拼成String
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < result.size(); i++) {
             if (i == hs.size() - 1)
                 buffer.append(result.get(i).toString());
             else
                 buffer.append(result.get(i).toString()).append(",");
        }
        System.out.println("去重前:  "+z);
        System.out.println("去重后:  "+buffer);
    }
    
    
    
    
    //List之统计元素项以及元素出现的次数
    private static List<Map<String, Object>> CollectionsList(List<String> Z) {
        //new一个list准备装取统计后的数据
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();  
        //转换成set去重
        Set<String> set = new HashSet<String>(Z);  
        //for循环统计set中元素出现的次数
        for(String id : set){
            //new一个map来装取统计后的元素次数
            Map<String,Object> map = new HashMap<String, Object>();  
            //转换成int类型
            int ids = Integer.parseInt(id);
            map.put("th",ids);  
            map.put("count", Collections.frequency(Z, id));  
            list.add(map);  
        }  
//        System.out.println(list.toString());  
        return list;
    }
    
    //List<String>转List<Integer>
    private List<Integer> ListToint(List<String> Cwt) {
        List<Integer> a = new ArrayList<Integer>();
        //使用apache.commons工具包,转换list的类型
        CollectionUtils.collect(Cwt, new Transformer() {
            public Object transform(Object o) {
                return Integer.valueOf(o.toString());
            }
        }, a);
        return a;
    }
    
}


输出结果:

String转JSONObject:

{"cwt":"101,102,102,103,103,103,104,104,104,104,105,106,106,106,106,107,108,109,110","zqt":"101,102,102,103,103,103,104,104,104,104,105,106,106,106,106,107,108,109,110"}

获取JSONObject其中一个的值:  101,102,102,103,103,103,104,104,104,104,105,106,106,106,106,107,108,109,110
String转list:  [101, 102, 102, 103, 103, 103, 104, 104, 104, 104, 105, 106, 106, 106, 106, 107, 108, 109, 110]
List<String>转List<Integer>:  [101, 102, 102, 103, 103, 103, 104, 104, 104, 104, 105, 106, 106, 106, 106, 107, 108, 109, 110]
List之统计元素项以及元素出现的次数:  [{th=110, count=1}, {th=101, count=1}, {th=102, count=2}, {th=103, count=3}, {th=104, count=4}, {th=105, count=1}, {th=106, count=4}, {th=107, count=1}, {th=108, count=1}, {th=109, count=1}]
List转JSONArray:  [{"th":110,"count":1},{"th":101,"count":1},{"th":102,"count":2},{"th":103,"count":3},{"th":104,"count":4},{"th":105,"count":1},{"th":106,"count":4},{"th":107,"count":1},{"th":108,"count":1},{"th":109,"count":1}]
去重前:  101,102,102,103,103,103,104,104,104,104,105,106,106,106,106,107,108,109,110

去重后:  110,101,102,103,104,105,106,107,108,109


maven所需依赖包

    <!-- Json包 -->
         <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20160810</version>
        </dependency>
     <!-- commons依赖 -->
     <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-collections4</artifactId>
        <version>4.0</version>
    </dependency>


猜你喜欢

转载自blog.csdn.net/luker_/article/details/80631839