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代码的 最近在刷各大公司的技术博客的 ...
随机推荐
- DP大大大大大赏
还是前置: 动态规划的三种实现方法: 递推,递归,记忆化搜索 然后还是从斐波那契数列开始引入: 两种求斐波那契数列的方法: 1.用其他位置的结果得到自己的结果: 2.用自己的结果算其他的结果: 以上两 ...
- matplotlib库绘制散点图
假设通过爬虫你获取到了北京2016年3,10月份每天白天的最高气温(分别位于列表a,b),那么此时如何寻找出气温随时间(天)变化的某种规律? a = [11,17,16,11,12,11,12,6,6 ...
- one:arguments对象伪数组
这是我的第一个博客 <script> //计算N个数字的和 //定义一个函数,如果不确定用户是否传入了参数,或者说不知道用户传入了几个参数,没办法计算, // 但是如果在函数中知道了参数的 ...
- paramiko模块(远程操作服务器)
paramiko模块(远程操作服务器) django+paramkio实现远程某些服务器执行命令+上传文件 用于帮助开发者通过代码远程连接服务器,并对服务器进行操作. pip3 install par ...
- python 基础(十八)--shutil模块
shutil模块 shutil.copyfileobj(src,dst):只拷贝文件内容,需要open文件:目标文件不存在时创建,存在时覆盖 shutil.copyfileobj(open('old. ...
- package[golang]学习笔记之runtime
*获取当前函数名称,文件名称,行号等信息.通过这个函数配合Println函数可以方便的获取错误信息的位置 var n int //n==0 当前 //n==1 调用函数 //n==2 调用函数的调用函 ...
- table与json的互转
json是键值对,在Lua中类型是string 主要运用在table中.表:local t={a="1",b="2",c="3",d=&qu ...
- Datetime 在C#中的用法 获取当前时间的各种格式
DateTime 获得当前系统时间: DateTime dt = DateTime.Now; Environment.TickCount可以得到“系统启动到现在”的毫秒值 DateTime now = ...
- react中怎么写css样式?
JSX基本语法中关于react如何写css样式主要有三种方法 1.基于class --(className) 基于className ,通过className在style中给该class名的DOM元素 ...
- Docker镜像构建文件Dockerfile及相关命令介绍
使用docker build命令或使用Docker Hub的自动构建功能构建Docker镜像时,都需要一个Dockerfile文件.Dockerfile文件是一个由一系列构建指令组成的文本文件,doc ...