CalUtil 计算的Util工具类

该代码提供了一系列静态方法,用于执行BigDecimal的数学运算(如相减并取绝对值、除法得到百分比)、Map的值相加、获取Map中最大/最小值的键以及按值排序。此外,还有根据值获取Map中的键和将JSON字符串转换为Map的功能。
摘要由CSDN通过智能技术生成
package com.wg.util;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: hezha
 */
public class CalUtil {

    /*
     * @description: 两数相减,获取绝对值
     * @return: java.math.BigDecimal
     * @author: hezha
     * @time: 2022/5/11 17:15
     */
    public static BigDecimal subtract(BigDecimal num1, BigDecimal num2){

        BigDecimal res = num1.subtract(num2);
        BigDecimal abs = res.abs();
        return abs;
    }

    /**
     * @description: 求百分比
     * @return: java.lang.String
     * @author: hezha
     * @time: 2022/4/8 18:37
     */
    public static String division(Long num1, Long num2){
        if(num1 ==0){
            return "0";
        }
        double f1 = new BigDecimal((float)num1/num2).setScale(6, BigDecimal.ROUND_HALF_UP).doubleValue();
        String res = String.valueOf(f1 * 100);
        return res;
    }

    public static BigDecimal division(BigDecimal num1, BigDecimal num2){
        if(num1.equals(BigDecimal.ZERO)){
            return BigDecimal.ZERO;
        }
        BigDecimal divide = num1.divide(num2, 6, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(100));
        return divide;
    }

    public static String division(Integer num1, Integer num2){
        if(num1 ==0){
            return "0";
        }
        double f1 = new BigDecimal((float)num1/num2).setScale(6, BigDecimal.ROUND_HALF_UP).doubleValue();
        String res = String.valueOf(f1 * 100);
        return res;

    }

    /**
     * 两个Map相加
     */
    public static Map<String, Integer> mapValueCount(Map<String, Integer> quoraListMapSource, Map<String, Integer> quoraListMapTarget) {
        Map<String, Integer> result = new HashMap<>();
        if(quoraListMapSource != null){
            for (String key : quoraListMapSource.keySet()) {
                if (result.containsKey(key)) {
                    result.put(key, result.get(key) + quoraListMapSource.get(key));
                } else {
                    result.put(key, quoraListMapSource.get(key));
                }
            }
        }
        if(quoraListMapTarget != null){
            for (String key : quoraListMapTarget.keySet()) {
                if (result.containsKey(key)) {
                    result.put(key, result.get(key) + quoraListMapTarget.get(key));
                } else {
                    result.put(key, quoraListMapTarget.get(key));
                }
            }
        }

        return result;
    }



    /**
     * 求Map<K,V>中Value(值)的最小值
     *
     * @param map
     * @return
     */
    public static Object getMinValue(Map<String, Long> map) {
        if (map == null) {
            return null;
        }
        Collection<Long> c = map.values();
        Object[] obj = c.toArray();
        Arrays.sort(obj);
        return obj[0];
    }


    /**
     * @description: 获取map中value值最大的key
     * @return: java.lang.String
     * @author: hezha
     * @time: 2022/4/14 15:41
     */
    public static String getMaxStr(Map<String, Long> map) {
        List<Map.Entry<String,Integer>> list = new ArrayList(map.entrySet());
        Collections.sort(list, (o1, o2) -> (o2.getValue() - o1.getValue()));//倒叙
        return list.get(0).getKey();
    }

    /**
     * 求Map<K,V>中Value(值)的最大值
     *
     * @param map
     * @return
     */
    public static Long getMaxValue(Map<String, Long> map) {
        if (map == null) {
            return null;
        }
        int length =map.size();
        Collection<Long> c = map.values();
        Object[] obj = c.toArray();
        Arrays.sort(obj);
        return (Long) obj[length-1];
    }

    public static Integer getMaxIntegerValue(Map<String, Integer> map) {
        if (map == null) {
            return null;
        }
        int length =map.size();
        Collection<Integer> c = map.values();
        Object[] obj = c.toArray();
        Arrays.sort(obj);
        return (Integer) obj[length-1];
    }


    public static String getMaxStrValue(Map<String, String> map) {
        if (map == null) {
            return null;
        }
        int length =map.size();
        Collection<String> c = map.values();
        Object[] obj = c.toArray();
        Arrays.sort(obj);
        return (String) obj[length-1];
    }

    /**
     * map中最小value
     */
    public static String getMinStrValue(Map<String, String> map) {
        if (map == null || map.isEmpty()) {
            return null;
        }
        int length =map.size();
        Collection<String> c = map.values();
        Object[] obj = c.toArray();
        Arrays.sort(obj);
        return (String) obj[0];
    }
    /**
     * @description: 根据value获取map中的key
     * @return: java.util.Set<K>
     * @author: hezha
     * @time: 2022/4/16 17:39
     */
    public static  List<String> getKeysByStream(Map<String, Long> map, Long value) {
        return map.entrySet()
                .stream()
                .filter(kvEntry -> Objects.equals(kvEntry.getValue(), value))
                .map(Map.Entry::getKey)
                .collect(Collectors.toList());
    }



    public static List<String> getKeysByStream(Map<String, Integer> map, Integer value) {
        return map.entrySet()
                .stream()
                .filter(kvEntry -> Objects.equals(kvEntry.getValue(), value))
                .map(Map.Entry::getKey)
                .collect(Collectors.toList());
    }

    /**
     * 获取map中指定value的key
     */
    public static List<String> getKeysByStream(Map<String, String> map, String value) {
        return map.entrySet()
                .stream()
                .filter(kvEntry -> Objects.equals(kvEntry.getValue(), value))
                .map(Map.Entry::getKey)
                .collect(Collectors.toList());
    }
    /**
     * @description: map 按照value倒序 
     * @return: java.util.List<java.util.Map.Entry<java.lang.String,java.lang.Integer>>
     * @author: hezha
     * @time: 2022/4/14 18:38
     * @param map
     */
    public static List<Map.Entry<String, Integer>> sortByMapValue(Map<String, Integer> map){
        ArrayList<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                if (o1.getValue() == null || o2.getValue() == null){
                    return -1;
                }
                return o2.getValue().compareTo(o1.getValue());
            }
        });
        return list;
    }



    public static List<Map.Entry<String, String>> sortByMapStringValue(Map<String, String> map){
        ArrayList<Map.Entry<String, String>> list = new ArrayList<>(map.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<String, String>>() {
            @Override
            public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
                if (o1.getValue() == null || o2.getValue() == null){
                    return -1;
                }
                return o2.getValue().compareTo(o1.getValue());
            }
        });
        return list;
    }

    public static List<Map.Entry<String,BigDecimal>> sortByMapValueDesc(Map<String, BigDecimal> map){
        ArrayList<Map.Entry<String, BigDecimal>> list = new ArrayList<>(map.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<String, BigDecimal>>() {
            @Override
            public int compare(Map.Entry<String, BigDecimal> o1, Map.Entry<String, BigDecimal> o2) {
                if (o1.getValue() == null || o2.getValue() == null){
                    return -1;
                }
                return o2.getValue().compareTo(o1.getValue());
            }
        });
        return list;
    }
    /**
     * @description: 两数相加 
     * @return: java.math.BigDecimal
     * @author: hezha
     * @time: 2022/4/15 17:39
     */
    public static BigDecimal addNum(String a, String b, String c){
        BigDecimal num1 =new BigDecimal(a);
        BigDecimal num2 =new BigDecimal(b);
        BigDecimal num3 =new BigDecimal(c);
        BigDecimal res = num1.add(num2).add(num3);
        return res;
    }

    // String txt = "{du=0.750000, no_du=1.000000}";
    //转map
    public static Map mapStringToMap(String str) {
        str = str.substring(1, str.length() - 1).replaceAll(" ","");
        String[] strs = str.split(",");
        Map map = new HashMap();
        for (String string : strs) {
            String key = string.split("=")[0];
            try {
                String value = string.split("=")[1];
                map.put(key, value);
            } catch (Exception e) {
                continue;
            }
        }
        return map;
    }

    //根据value找map中的所有key
    public static List<String> getMapKeysByValue(Map<String, Object> map, String value){
        List<String> list = new ArrayList<>();
        Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<String, Object> next = iterator.next();
            String key = next.getKey();
            if(value.equals( next.getValue().toString())){
                list.add(key);
            }
        }
        return list;
    }

    //KeyValueVo 是一个对象 里面是key和value value的值是String
    public static void main(String[] args) {

        String fumianSort = "{du=1.000000, tong=1.000000}";
        CalUtil.mapStringToMap(fumianSort);
//        BigDecimal bigDecimal = new BigDecimal(0);
//        System.out.println(bigDecimal.toString());
//        BigDecimal subtract = subtract(new BigDecimal(-1), new BigDecimal(23));
//        System.out.println(subtract);

//        BigDecimal division = division(new BigDecimal(1), new BigDecimal(23));
//        System.out.println(division);

//        Map<String, String> map = new HashMap<>();
//        map.put("1", "42.1053");
//        map.put("2", "100");
//        map.put("3", "100");
//        List<Map.Entry<String, String>> entries = sortByMapStringValue(map);
//        System.out.println(entries);
//        int country_determine =0;
//        String division = CalUtil.division(Long.valueOf(22), Long.valueOf(4));
//        if(division.compareTo("50") > 0){
//            country_determine = 1;
//        }else if(division.compareTo("25") <= 0 ){
//            country_determine = 3;
//        }else{
//            country_determine = 2;
//        }
//        System.out.println(country_determine);
//        BigDecimal bigDecimal = addNum("-1", "-0.5", "+2");



//        Map<String, Long> map = new HashMap<>();
//        map.put("1",0L);
//        map.put("2",7L);
//        map.put("6",7L);
//        map.put("5",0L);
//
//
//        Long maxValue = (Long) getMaxValue(map);
//        System.out.println(maxValue);
//        List<String> keysByStream = getKeysByStream(map, maxValue);
//        String join = String.join(",", keysByStream);
//        System.out.println(join);


//        String division = division(3L, 3L);
//        System.out.println(division);
//
//        if("52.3".compareTo("50") > 0){
//            System.out.println(1);
//        }
//
//        if("25".compareTo("25") <= 0){
//            System.out.println(1111);
//        }
//        HashMap<String, Integer> hashMap = new HashMap<>();
//        hashMap.put("1", 11);
        hashMap.put("2", 1);
//        hashMap.put("3", 0);
//        System.out.println(hashMap);
//
//        HashMap<String, Integer> map = new HashMap<>();
        map.put("1", 2);
//        map.put("2", 3);
//        map.put("3", 4);
//        System.out.println(map);
//
//        System.out.println(mapValueCount(hashMap, map));



    }



}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值