Code Craft

代碼篇
如果要隨機產(chǎn)生一個a到b(包括a和b)之間的整數(shù),可以使用下面的公式:
int num = (int)( Math.random() * ( b – a + 1 )) + a;
 
        // 隨機產(chǎn)生一個a到b(包括a和b)之間的整數(shù)
        int a = 0;
        int b = 10;
        int num = (int) ((Math.random()*Math.abs(a-b))+Math.min(a,b));


List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        //計算 List 集合中,所有元素的和
        int total = list.stream().map(item -> item).reduce((sum, n) -> sum + n).get();//total=6


#將list中所有POJO屬性set新值
pojoList.forEach(x -> x.setNeedStocktake(1));


// 獲取java.util.List<T> 范型類型
System.out.println(list.get(0).getClass());// com.ljheee.mail.MonitorPO

Object obj = list.get(0);//MonitorPO
Field[] declaredFields = obj.getClass().getDeclaredFields();

declaredFields[0].setAccessible(true);//orderId
System.out.println(declaredFields[0].get(obj));//獲取orderId字段值


    //數(shù)組逆轉(zhuǎn)
    public void reverse(char[] arr, int begin, int end) {
        while(end > begin) {
            char temp = arr[begin];
            arr[begin] = arr[end];
            arr[end] = temp;

            begin++;
            end--;
        }
    }


    int a[] = new int[]{1, 2, 3, 4, 5, 6, 7, 8};
    //經(jīng)典洗牌算法
    public static void shuffle(int[] a) {
        Random random = new Random();
        int n = a.length;
        for (int i = n - 1; i >= 1; i--) {
            int temp = a[i];
            int j = random.nextInt(n);
            a[i] = a[j];
            a[j] = temp;
            // swap(a[i],a[j])
        }
    }


    // 獲取日期的 0點時間戳,如2018-09-07 00:00:00的時間戳
    private long getDateFromTime(long timeMillis) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timeMillis);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }


    /**
     * 保留 bd 的后 scale位
     *
     * @param bd
     * @param scale  要保留的位數(shù);為0則取整
     * @return
     */
    public static String formatNum(BigDecimal bd, int scale) {
        bd = bd.setScale(scale, RoundingMode.HALF_UP); //向上取證整
        return bd.toString();
    }


    /**
     * 保留 小數(shù)點后n位(向上取整)
     * n可以取0
     * @param d
     * @param n
     * @return
     */
    public static String formatNum(double d, int n) {
        String patten = "#.";
        if (n == 0) {
            patten = "#";
        }
        for (int i = 0; i < n; i++) {
            patten += "#";
        }
        DecimalFormat df = new DecimalFormat(patten);
        String format = df.format(d);
        return format;
    }


        // 數(shù)值千分位轉(zhuǎn)化
        NumberFormat numberFormat = NumberFormat.getNumberInstance();
        System.out.println(numberFormat.format(11122.33)); //結(jié)果是11,122.33

        // 字符串 數(shù)值添加千分位
        BigDecimal a = new BigDecimal("123456789");
        DecimalFormat df = new DecimalFormat(",###,##0"); //沒有小數(shù)  
        //DecimalFormat df=new DecimalFormat(",###,##0.0"); //保留一位小數(shù)  
        System.out.println(df.format(a));//輸出123,456,789


命令使用篇
#git結(jié)合Linux下awk命令,統(tǒng)計[指定author]git提交代碼行數(shù)
#該命令在.git目錄下運行;更換author即可。去掉--author參數(shù),則統(tǒng)計整個項目代碼行
MacBook-Pro% git log --author="lijianhua" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -
added lines: 39, removed lines: 39, total lines: 0


配置文件篇
<!-- mybatis-config.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <!-- 分布式應(yīng)用必須禁用查詢緩存 -->
        <setting name="cacheEnabled" value="false"/>
        <!-- 禁用懶加載-->
        <setting name="lazyLoadingEnabled" value="false"/>
        <!-- 默認執(zhí)行超時時間(秒) -->
        <setting name="defaultStatementTimeout" value="180"/>
        <!-- 下劃線命名轉(zhuǎn)駝峰命名-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <!-- 默認開啟“獲取插入自增主鍵的值”(僅insert語句) -->
        <setting name="useGeneratedKeys" value="true"/>
        <setting name="logImpl" value="LOG4J2"/>
    </settings>
</configuration>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容