String转Map的工具类

借鉴代码

public class StringToMapUtil {

    public static Map<String, String> getValue(String param) {
        Map map = new HashMap();
        String str = "";
        String key = "";
        Object value = "";
        char[] charList = param.toCharArray();
        boolean valueBegin = false;
        for (int i = 0; i < charList.length; i++) {
            char c = charList[i];
            if (c == '{') {
                if (valueBegin == true) {
                    value = getValue(param.substring(i, param.length()));
                    i = param.indexOf('}', i) + 1;
                    map.put(key, value);
                }
            } else if (c == '=') {
                valueBegin = true;
                key = str;
                str = "";
            } else if (c == ',') {
                valueBegin = false;
                value = str;
                str = "";
                map.put(key, value);
            } else if (c == '}') {
                if (str != "") {
                    value = str;
                }
                map.put(key, value);
                return map;
            } else if (c != ' ') {
                str += c;
            }
        }
        return map;
    }
}

 

转载于:https://www.cnblogs.com/atomicbomb/p/7477244.html

listmap是一种常见的数据处理方式,可以通过自定义工具类来实现。 首先,我们需要定义一个方法,该方法的输入参数是一个List对象,返回值是一个Map对象。方法的主要逻辑如下: 1. 创建一个空的Map对象,用于存储换后的数据。 2. 使用循环遍历List对象中的每一个元素。 3. 对于每个元素,获取map的key和value。 4. 将key和value添加到Map对象中。 5. 返回换后的Map对象。 以下是一个示例的listmap工具类的代码: ```java import java.util.HashMap; import java.util.List; import java.util.Map; public class ListToMapUtil { public static <K, V> Map<K, V> convertListToMap(List<MapEntry<K, V>> list) { Map<K, V> map = new HashMap<>(); for (MapEntry<K, V> entry : list) { K key = entry.getKey(); V value = entry.getValue(); map.put(key, value); } return map; } } class MapEntry<K, V> { private K key; private V value; public MapEntry(K key, V value) { this.key = key; this.value = value; } public K getKey() { return key; } public V getValue() { return value; } } ``` 使用该工具类,我们可以方便地将List对象换成Map对象。例如: ```java List<MapEntry<String, Integer>> list = new ArrayList<>(); list.add(new MapEntry<>("key1", 1)); list.add(new MapEntry<>("key2", 2)); Map<String, Integer> map = ListToMapUtil.convertListToMap(list); System.out.println(map); // 输出:{key1=1, key2=2} ``` 以上就是一个简单的listmap工具类的实现。根据具体的需求,我们可以对工具类进行进一步的扩展和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值