Java 代码的精优化
一、避免使用BigDecimal(double)
反例:
// BigDecimal 反例
BigDecimal decimal = new BigDecimal(11.801D);
System.out.println("decimal:"+decimal);
正例:
// BigDecimal 正例
BigDecimal bDecimal = BigDecimal.valueOf(11.801D);
System.out.print("bDecimal:"+bDecimal);
执行结果:
decimal:11.8010000000000001563194018672220408916473388671875
bDecimal:11.801
二、String.split(String regex)部分关键字需要转译
使用字符串String 的plit 方法时,传入的分隔字符串是正则表达式,则部分关键字(比如 .[]()| 等)需要转义。
2.1
String str = "small.sun.shine";
String[] strSplit = str.split("."); // .需要转义,反例
String[] strSplit1 = str.split("\\.");// 正例
String[] strSplit2 = str.split("[.]");// 正例
System.out.println("strSplit:" + Arrays.toString(strSplit));
System.out.println("strSplit1:" + Arrays.toString(strSplit1));
System.out.println("strSplit2:" + Arrays.toString(strSplit2));
执行结果:
strSplit:[]
strSplit1:[small, sun, shine]
strSplit2:[small, sun, shine]
2.2
String str = "small|sun|shine";
String[] strSplit = str.split("|"); // | 需要转义,反例
String[] strSplit1 = str.split("\\|");// 正例
String[] strSplit2 = str.split("[|]");// 正例
System.out.println("strSplit:" + Arrays.toString(strSplit));
System.out.println("strSplit1:" + Arrays.toString(strSplit1));
System.out.println("strSplit2:" + Arrays.toString(strSplit2));
执行结果:
strSplit:[]
strSplit1:[small, sun, shine]
strSplit2:[small, sun, shine]
三、迭代entrySet() 获取Map 的key 和value
当循环中只需要获取Map 的主键key时,迭代keySet() 是正确的;但是,当需要主键key 和取值value 时,迭代entrySet() 才是更高效的做法,其比先迭代keySet() 后再去通过get 取值性能更佳。
//Map 获取value 反例:
HashMap<String, String> map = new HashMap<>();
for (String key : map.keySet()){
String value = map.get(key);
}
正例:
//Map 获取key & value 正例:
HashMap<String, String> map = new HashMap<>();
for (Map.Entry<String,String> entry : map.entrySet()){
String key = entry.getKey();
String value = entry.getValue();
}
四、MyBatis 不要为了多个查询条件而写 1 = 1
当遇到多个查询条件,使用where 1=1 可以很方便的解决我们的问题,但是这样很可能会造成非常大的性能损失,因为添加了 “where 1=1 ”的过滤条件之后,数据库系统就无法使用索引等查询优化策略,数据库系统将会被迫对每行数据进行扫描(即全表扫描) 以比较此行是否满足过滤条件,当表中的数据量较大时查询速度会非常慢;此外,还会存在SQL 注入的风险。
反例:
<select id="queryBookInfo" parameterType="com.tjt.platform.entity.BookInfo" resultType="java.lang.Integer">
select count(*) from t_rule_BookInfo t where 1=1
<if test="title !=null and title !='' ">
AND title = #{title}
</if>
<if test="author !=null and author !='' ">
AND author = #{author}
</if>
</select>
正例:
<select id="queryBookInfo" parameterType="com.tjt.platform.entity.BookInfo" resultType="java.lang.Integer">
select count(*) from t_rule_BookInfo t
<where>
<if test="title !=null and title !='' ">
title = #{title}
</if>
<if test="author !=null and author !='' ">
AND author = #{author}
</if>
</where>
</select>
Java 代码的精优化的更多相关文章
- java 代码的细节优化
前言 代码优化,一个很重要的课题.可能有些人觉得没用,一些细小的地方有什么好修改的,改与不改对于代码的运行效率有什么影响呢?这个问题我是这么考虑 的,就像大海里面的鲸鱼一样,它吃一条小虾米有用吗?没用 ...
- Java代码中可以优化性能的小细节
避免对boolean类型的判定 反例: 12 if("a".equles("a")==true)`{} 正例: 12 if(Objects.equles(&qu ...
- 如何在Android上编写高效的Java代码
转自:http://www.ituring.com.cn/article/177180 作者/ Erik Hellman Factor10咨询公司资深移动开发顾问,曾任索尼公司Android团队首席架 ...
- java代码实现队列的优化
package com.voole.queun; /** * @Decription 队列 * @author TMAC-J * */ public class Queun { /** * 初始化队列 ...
- [大牛翻译系列]Hadoop(15)MapReduce 性能调优:优化MapReduce的用户JAVA代码
6.4.5 优化MapReduce用户JAVA代码 MapReduce执行代码的方式和普通JAVA应用不同.这是由于MapReduce框架为了能够高效地处理海量数据,需要成百万次调用map和reduc ...
- Linkedin工程师是如何优化他们的Java代码的(转)
英文原文:LinkedIn Feed: Faster with Less JVM Garbage 最近在刷各大公司的技术博客的时候,我在Linkedin的技术博客上面发现了一篇很不错博文.这篇博文介绍 ...
- 利用封装、继承对Java代码进行优化
注:本文实例分别可以在oldcastle(未优化的代码)和newcastle(优化后的代码)中查看,网址见文末 城堡游戏: 城堡中有多个房间,用户通过输入north, south, east, wes ...
- java代码之美(11)---java代码的优化
java代码的优化 随着自己做开发时间的增长,越来越理解雷布斯说的: 敲代码要像写诗一样美.也能理解有一次面试官问我你对代码有洁癖吗? 一段好的代码会让人看就像诗一样,也像一个干净房间会让人看去很舒服 ...
- Linkedin工程师是如何优化他们的Java代码的
http://greenrobot.me/devpost/java-faster-less-jvm-garbage/ Linkedin工程师是如何优化他们的Java代码的 最近在刷各大公司的技术博客的 ...
随机推荐
- ARC099E. Independence
考虑一个子问题.给定无向图 $G$,如何判断能否将 $G$ 的点集分成两部分 $S$.$T$ 使得 $S$ 和 $T$ 导出的子图都是完全图? 这个问题把我难住了.解法是考虑 $G$ 的补图 $G'$ ...
- C#与C++的区别
C# 参考链接:https://www.runoob.com/csharp/csharp-tutorial.html ------------------C#数据类型----------------- ...
- hard or 9102 字符串DP---Educational Codeforces Round 57 (Rated for Div. 2)
题意:http://codeforces.com/problemset/problem/1096/D 思路:参考:https://blog.csdn.net/qq_41289920/article/d ...
- Analyzing Polyline -- Codeforces Round #123 (Div. 2)
题意:https://codeforc.es/problemset/problem/195/D 求折线段数. 思路: 对pos进行sort,对不同区间段加k,两个dp处理不同k>0 or k&l ...
- PostgreSQL查看表、表索引、视图、表结构以及参数设置
-- 表索引select * from pg_indexes where tablename='person_wechat_label';select * from pg_statio_all_ind ...
- Bean属性复制,字段名可不同,字段类型不同需要自行处理
@Setter @Getter public class SourceA { private String name; private String text; public SourceA(Stri ...
- unity ugui image更换图片
1:利用资源加载方式 using UnityEngine; using System.Collections; using UnityEngine.UI; public class ChangeIma ...
- mvc验证码图片生成
/// <summary> ///生成验证码 /// </summary> public class VerifyCode { /// <summary> /// ...
- QByteArray详解
QByteArray在串口通讯中经常被使用,有一定必要较为全面详细的对QByteArray进行阐述.本文通过以下几个部分加以介绍: 1. 初始化 2. 访问与赋值 3. 添加.删除.插入与替换操作 4 ...
- git pull文件时和本地文件冲突 方法之一
1.先将本地修改存储起来 2.pull内容 3.还原暂存的内容 4.解决文件中冲突的的部分 打开 dsa.txt 文件手动解决冲突. 其中Updated upstream 和=====之间的内容就是p ...