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代码的 最近在刷各大公司的技术博客的 ...
 
随机推荐
- Linux系列:进阶之tomcat安装
			
思路:作者是在Windows上从Apache官网下载的tomcat,之后将tomcat文件放到我的ftp站点中,在Linux访问ftp站点下载tomcat文件 ,将tomcat放在我自己的安装目录中, ...
 - php 合成图片,合成圆形图片
			
合成图片方法 <?php class Share { /* * 生成分享图片 * */ function cre_share_study_img(){ $auth = json_decode(b ...
 - django  CBV装饰器 自定义django中间件  csrf跨站请求伪造  auth认证模块
			
CBV加装饰器 第一种 @method_decorator(装饰器) 加在get上 第二种 @method_decorator(login_auth,name='get') 加在类上 第三种 @met ...
 - Python—None
			
None是一个特殊的常量. None不是False. None不是0. None不是空字符串. None有自己的数据类型NoneType,并且是NoneType中唯一的值. None只是一个空值的对象 ...
 - Java Web ActiveMQ与WebService的异同
			
Webservice 和MQ(MessageQueue)都是解决跨平台通信的常用手段 一.WebService:用来远程调用服务,达到打通系统.服务复用的目的.是SOA系统架构——面向服务架构的体现. ...
 - gcc/g++ 以及makefile
			
生成可执行文件 g++ mutiprocess.cpp -o test -fpic:产生位置无关码,位置无关码就是可以在进程的任意内存位置执行的目标码,动态链接库必须使用 -c : 只生成 .o ...
 - luogu P4428 [BJOI2018]二进制
			
luogu 先考虑怎样的二进制串才会被3整除.可以发现如果二进制位第\(0,2,4...2n\)位如果为\(1\),那么在模3意义下为1,如果二进制位第\(1,3,5...2n+1\)位如果为\(1\ ...
 - 【Javascript】 js的构造函数与原形对象的关系
			
构造函数只是提供了一个创建对象的模板,它并不是对象的原形. 对象的原形是构造函数的原形,即object. ----------------------------------------------- ...
 - javascript相关的增删改查以及this的理解
			
前两天做了一个有关表单增删改查的例子,现在贴出来.主要是想好好说一下this. 下面贴一张我要做的表格效果. 就是实现简单的一个增删改查. 1.点击增加后自动增加一行: 2.点击保存当前行会将属性改成 ...
 - 绑定css样式,点击高亮
			
<div class="flex-lay" style="color:#999"> <div bindtap="changeType ...