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代码的 最近在刷各大公司的技术博客的 ...
随机推荐
- SQL SERVER 数据有CHAR(10),CHAR(13),CHAR(9)隐藏字符
原文:SQL SERVER 数据有CHAR(10),CHAR(13),CHAR(9)隐藏字符 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/Givi ...
- JavaScript设计模式(单例模式)
单例模式是一种简单但非常实用的模式,特别是惰性单例技术,在合适的时候才创建对象,并且只创建唯一的一个.下面我们来逐步了解单例模式的用法. 一.简版单例模式: var Singleton = funct ...
- 关于HA(2.102 -2.103 服务器排障)
关于处理RHCA故障的报告: ,2.102 和 2.103 两台机器在重启之后拉不起来 原因是这两台服务比较怪 先要启动service rpcbind restart 然后再要起service nfs ...
- 第一次碰到%*s这个鬼东西。。
printf("%*s",5,"123"); 输出为 ##123 (其中##表示空格) 这个鬼东西是用来控制格式的. 当然也可以用来输出空格个数
- Validator自动验证与手动验证
自动: public JResult projectAdd(@Valid Project project, BindingResult result) {Map<String,Object> ...
- 进阶Java编程(3)线程的同步与死锁
线程的同步与死锁 1,同步问题引出 在多线程的处理之中,可以利用Runnable描述多个线程操作的资源,而Thread描述每一个线程对象,对于当多个线程访问统一资源的时候如果处理不当就会产生数据的错误 ...
- TreeMap——实现comparable接口并重写CompareTo方法
public class TreeMapTest { public static void main(String[] args) { Map<Student,Integer> stude ...
- 文件名后面加(1).text
; //在重复名称后加(序号) while (File.Exists(path)) { if (path.Contains(").")) { int start = path.La ...
- css;js学习(一)
推荐基础前端学习地址https://ke.qq.com/course/315961蝉壳学院 清除浮动 .clearfix:before,.clearfix:after{ content: " ...
- Winfrom 简单的进度条小程序
使用Winform空间编写简单的进度条小程序: 所需控件:Lable 标签 TextBox 文本框 progressBar 进度条控件 timer 定时器 下面是源码及效果图: /// &l ...