java工具类

 

/**
     * @Title:removeDuplicate
     * @author:踏步
     * @date:2019年5月23日 下午2:31:40 
     * @Description:TODO 去除list的重复项
     * @param list 需要去除重复项的list
     * @return List 返回去重后的list 
     */
    public static List removeDuplicate(List list) {
        try {
            HashSet hs = new HashSet(list);
            list.clear();
            list.addAll(hs);
        } catch (Exception e) {
            logger.error("去除List重复项removeDuplicate的方法 err", e);
            e.printStackTrace();
        }
        return list;
    }

    /**
     * @Title:objectTurnToBinary
     * @author:踏步
     * @date:2019年7月6日 下午4:12:08 
     * @Description:TODO 把对象转变成二进制
     * @param obj待转换的对象
     * @return byte[] 返回二进制数组
     */
    public static byte[] objectTurnToBinary(Object obj) {
        ByteArrayOutputStream bos = null;
        ObjectOutputStream oos = null;
        try {
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            // 读取对象并转换成二进制数据
            oos.writeObject(obj);
            return bos.toByteArray();
        } catch (IOException e) {
            logger.error("对象转换成二级制数据失败  err", e);
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * @Title:binaryTurnToObject
     * @author:踏步
     * @date:2019年7月6日 下午4:11:17 
     * @Description:TODO 把二进制数组的数据转回对象
     * @param b二进制数组
     * @return Object 
     */
    public static Object binaryTurnToObject(byte[] b) {
        ByteArrayInputStream bis = null;
        ObjectInputStream ois = null;
        try {
            // 读取二进制数据并转换成对象
            bis = new ByteArrayInputStream(b);
            ois = new ObjectInputStream(bis);
            return ois.readObject();
        } catch (ClassNotFoundException | IOException e) {
            logger.error("二进制数据转回对象失败  err", e);
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * @Title:getStringByString
     * @author:踏步
     * @date:2019年7月23日 下午4:36:03 
     * @Description:TODO 去除字符串中是所有数字
     * @param str字符串
     * @return String 
     */
    public static String getStringByString(String str) throws Exception {
        String reg1 = "[\d]";
        Pattern p = Pattern.compile(reg1);
        Matcher matcher = p.matcher(str);
        str = matcher.replaceAll("");
        return str;
    }

    /**
     * @Title:getIntByString
     * @author:踏步
     * @date:2019年7月23日 下午4:36:24 
     * @Description:TODO 提取字符串中的所有数字
     * @param str字符串
     * @return String 
     */
    public static String getIntByString(String str) throws Exception {
        String reg2 = "[^\d]";
        Pattern p = Pattern.compile(reg2);
        str = p.matcher(str).replaceAll("");
        return str;
    }
/**
     * @Title:sortString
     * @date:2020年5月14日 下午4:48:16  
     * @Description:TODO 对字符串经进行排序
     * @param string--待排序字符串
     * @return String
     * @throws Exception
     */
    public static String sortString(String string) throws Exception{
        QwyUtil.printMethodLogger(logger, "0");
        char[] charArray = string.toCharArray();
        Arrays.sort(charArray);
        return String.valueOf(charArray);
    }

》》》

原文地址:https://www.cnblogs.com/mjtabu/p/java-tool-class.html