java小结

在开发过程中遇到的各种bug。

一、BigDecimal转json时候会丢失精度问题

在前台页面data中的BigDecimal数据类型,比如0.0000会变为0,导致精度丢失。

data: JSON.stringify(data),

解决方法

new DecimalFormat("#0.0000").format(map.get("percent"));--指定保留四位小数

二、在求概率问题时,两个long型相除误差大

解决方法

先将long型转为double型,再进行运算,将结果赋予double型。

三、前台传数组,后端接受

public void excel(HttpServletResponse response, HttpServletRequest request, 
                         String[] headers, String[] fields) throws Exception {

    String[] bureauArr = request.getParameterValues("bureau[]");
    String[] lineNameArr = request.getParameterValues("lineName[]");
    String[] headers;

两种方式:

一、传参时用String[]

二、使用request.getParameterValues("lineName[]") 接受,参数后边要加[].

四、手动分页

//分页
List bizRtmIceInfoPage = new ArrayList();
int page,rows;
int total = bizRtmIceInfoPage.size();
List pageList = new ArrayList<>();
//数量与页数的关系
if( total - page*rows > 0 ){
    for (int i = 0; i < rows; i++) {
        pageList.add(bizRtmIceInfoPage.get(rows * (page - 1) + i));
    }
}else{
    for (int i = 0; i < total - rows*(page-1) ; i++) {
        pageList.add(bizRtmIceInfoPage.get(rows * (page - 1) + i));
    }

}

五、字符串替换分割与去重

//取字符串中的数字
String[] NumberArr = "1好2huai3#".replaceAll("\\D", "_").replace("_+", "_")).split("_+");
//去重
List Number = (Arrays.asList(NumberArr)).stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());

六、List中对象的某个属性排序

private void CompareBiz(String sidx, String sord, List bizRtmIceInfoPage) {
    //升序
    if(sord.equals("asc")){
        if("company".equals(sidx)){
            bizRtmIceInfoPage.sort(Comparator.comparing(BizIceInfo::getCompany));
        }else if("iceHeight".equals(sidx)){
            bizRtmIceInfoPage.sort(Comparator.comparingDouble(BizIceInfo::getIceHeight));
        }else if("iceHeightR".equals(sidx)){
            bizRtmIceInfoPage.sort(Comparator.comparingDouble(BizIceInfo::getIceHeightR));
        }else if("mark".equals(sidx)){
            bizRtmIceInfoPage.sort(Comparator.comparing(BizIceInfo::getMark));
        }
    }else{
    //降序
        if("company".equals(sidx)){
            bizRtmIceInfoPage.sort(Comparator.comparing(BizIceInfo::getBsid).reversed());
        }else if("heightThan".equals(sidx)){
            bizRtmIceInfoPage.sort(Comparator.comparingDouble(BizIceInfo::getHeightThan).reversed());
        }else if("mark".equals(sidx)){
            bizRtmIceInfoPage.sort(Comparator.comparing(BizIceInfo::getMark).reversed());
        }
    }

七、数组 List Set 互相转化

//数组转List
List list = Arrays.asList(arr);

//数组转Set
Set set = new HashSet<>(Arrays.asList(arr));

//List转数组
Object[] arr = list.toArray();

//List转Set
Set result = new HashSet(list);

//Set转数组
Object[] arr = set.toArray();

//Set转List
List list = new ArrayList<>(set);

你可能感兴趣的:(java小结)