1、map.values() 转换成List
Collection<String> valueCollection = map.values();
List<String> valueList = new ArrayList<String>(valueCollection);
2、List元素排序
Collections.sort(timeList, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1.compareTo(o2);
}
});
3、Velocity VM模版加载相对路径设置
<!-- velocity 配置相关属性如VM加载路径-->
<bean class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="velocityProperties">
<map>
<entry key="resource.loader" value="class"/>
<entry key="class.resource.loader.class" value="org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"/>
</map>
</property>
</bean>4、double保留指定位数
private double truncateFloatNum(double source){
BigDecimal b = new BigDecimal(source);
return b.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
}
5、查看crontab message&log
tail -fn 10 /var/log/messages
tail -fn 10 /var/log/cron
6、两个整数相除保留指定位小数
private double divideTwoInteger(int divisor, int dividend){
double a = divisor;
double b = dividend; if (Double.isInfinite(result) || Double.isNaN(result))
return 0.0;
return new BigDecimal(a / b).setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
}
本文介绍了几种实用的Java编程技巧,包括如何将Map的值转换为List并进行排序,使用Velocity模板引擎时如何正确配置VM加载路径,以及如何对浮点数和整数除法结果进行精确控制等。

377

被折叠的 条评论
为什么被折叠?



