栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

关于JAVA将List<T>中的某一key改变其value的工具类

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

关于JAVA将List<T>中的某一key改变其value的工具类

        在某些情况下,我们需要将数据库中的浮点值改变为具体数值发往前端这就需要改变List中的属性值,解决方法如下:

import java.util.ArrayList;
import java.util.List;

public class ListABswapUtil {

    //将list中的某一属性进行交换
    //list传入list, key传入需要替换的key, value传入需要替换的value
    public static List chattribute(List list, String key, String value){
        List list_f = new ArrayList();
        for(int i=0;i<=list.size()-1; i++){
            int b_d;//替换元素在结尾情况
            String temp_c;//被替换的元素
            String temp_a = list.get(i).toString();//得到单个总字符串
            int b_a = temp_a.indexOf(key)+key.length()+1;//b_a是总字符串到更改值下标的距离
            String temp_b = temp_a.substring(b_a,temp_a.length());//后半段
            int b_b = temp_b.indexOf(",");
            if (b_b==-1){
                b_d = temp_b.indexOf(")");
                temp_c = temp_b.substring(b_d);
            }else {
                temp_c = temp_b.substring(b_b);
            }
            String front = temp_a.substring(0,b_a);
            String fdata = front+value+temp_c;
            list_f.add(fdata);
        }
        return list_f;
    }

    //将单条list中的某一属性值转换
    //list传入list, key传入需要替换的key, value传入需要替换的value
    public static String chstr(String str, String key, String val){
        int c;
        String temp_b = null;
        int a = str.indexOf(key)+key.length()+1;//获取需要改变值的起始下标
        String temp_a = str.substring(a);
        int b = temp_a.indexOf(",");
        if (b==-1){
            c = temp_a.indexOf(")");
            temp_b= temp_a.substring(c);
        }else {
            temp_b= temp_a.substring(b);
        }
        String fdata = str.substring(0,a)+val+temp_b;
        return fdata;
    }
}

这样就可以在开发过程中快速解决这一问题。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1014816.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号