系统变量
当前系统的换行符
System.getProperty(“line.separator”)
Map
Map集合类用于存储元素对,也称为键值对,其中每个键映射到一个值。
BigDecimal
取整
对于BigDecimal有一个位数取整的问题。在实际的开发过程中,我们往往需要使用RoundingMode这个枚举来决定是使用哪种方式。那么,RoundingMode中有很多方式,各个方式都是什么意思呢?
先看看RoundingMode的可能取值:
- UP
- DOWN
- CEILING
- FLOOR
- HALF_UP
- HALF_DOWN
- HALF_EVEN
- UNNECESSARY
示例:
| 值 | UP | DOWN | CEILING | FLOOR | HALF_UP | HALF_DOWN | HALF_EVEN | UNNECEESARY |
|---|---|---|---|---|---|---|---|---|
| 5.5 | 6 | 5 | 6 | 5 | 6 | 5 | 6 | ArithmeticException |
| 2.5 | 3 | 2 | 3 | 2 | 3 | 2 | 2 | ArithmeticException |
| 1.6 | 2 | 1 | 2 | 1 | 2 | 1 | 2 | ArithmeticException |
| 1.1 | 2 | 1 | 2 | 1 | 1 | 1 | 1 | ArithmeticException |
| 1.0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
| -1.0 | -1 | -1 | -1 | -1 | -1 | -1 | -1 | -1 |
| -1.1 | -2 | -1 | -1 | -2 | -1 | -1 | -1 | ArithmeticException |
| -1.6 | -2 | -1 | -1 | -2 | -2 | -2 | -2 | ArithmeticException |
| -2.5 | -3 | -2 | -2 | -3 | -3 | -2 | -2 | ArithmeticException |
| -5.5 | -6 | -5 | -5 | -6 | -6 | -5 | -6 | ArithmeticException |
分类
取整方式可以分为直接取整,半数取整和必要取整。
- 直接取整
UP,DOWN,CEILING和FLOOR。 - 半数取整:
HALF_UP,HALF_DOWN和HALF_EVEN。 - 必要取整:
UNNECEESARY直接取整:
直接取整我们可以分正数取整和负数取整。
当正数取整的时候,UP和CEILING的行为是一样的,都是直接向上取整。同时,DOWN和FLOOR的行为是一样的,都是直接向下取整。
| 值 | UP | CEILING | DOWN | FLOOR |
|---|---|---|---|---|
| 5.5 | 6 | 6 | 5 | 5 |
| 2.5 | 3 | 3 | 2 | 2 |
| 1.6 | 2 | 2 | 1 | 1 |
| 1.1 | 2 | 2 | 1 | 1 |
| 1.0 | 1 | 1 | 1 | 1 |
当负数取数的时间,UP和FLOOR的行为是一样的,都是取绝对值小的负数。同时,DOWN和CEILING的行为是一样的,都是取绝对值大的负数。
| 值 | UP | FLOOR | DOWN | CEILING |
|---|---|---|---|---|
| -1.0 | -1 | -1 | -1 | -1 |
| -1.1 | -2 | -2 | -1 | -1 |
| -1.6 | -2 | -2 | -1 | -1 |
| -2.5 | -3 | -3 | -2 | -2 |
| -5.5 | -6 | -6 | -5 | -5 |
半数取整
对于半数取整,我们也来分为正数取整和负数取整:
正数取整:
HALF_UP是四舍五入HALF_DOWN是五舍六入HALF_EVEN是邻近取整,否则取偶 | 值 | HALF_UP | HALF_DOWN | HALF_EVEN | | —- | —- | —- | —- | | 5.5 | 6 | 5 | 6 | | 2.5 | 3 | 2 | 2 | | 1.6 | 2 | 1 | 2 | | 1.1 | 1 | 1 | 1 | | 1.0 | 1 | 1 | 1 |
负数取整:
HALF_UP是四舍五入(以绝对值为准)HALF_DOWN是五舍六入(以绝对值为准)HALF_EVEN是邻近取整,否则取偶 | 值 | HALF_UP | HALF_DOWN | HALF_EVEN | | —- | —- | —- | —- | | -1.0 | -1 | -1 | -1 | | -1.1 | -1 | -1 | -1 | | -1.6 | -2 | -2 | -2 | | -2.5 | -3 | -2 | -2 | | -5.5 | -6 | -5 | -6 |
必要取整
UNNECESSARY:非必要取整。没有不能直接取整,报错
| 值 | UNNECEESARY |
|---|---|
| 5.5 | ArithmeticException |
| 2.5 | ArithmeticException |
| 1.6 | ArithmeticException |
| 1.1 | ArithmeticException |
| 1.0 | 1 |
| -1.0 | -1 |
| -1.1 | ArithmeticException |
| -1.6 | ArithmeticException |
| -2.5 | ArithmeticException |
| -5.5 | ArithmeticException |
总结:
下面用一个表格的说明一下各个枚举值的含义:
| 值 | 含义 | 对应的BigDecimal值 |
|---|---|---|
| UP | 向距离0远的方向取整,即取整为绝对值大的方向 | BigDecimal.ROUND_UP |
| DOWN | 向距离0近的方向取整,即取整为绝对值小的方向 | BigDecimal.ROUND_DOWN |
| CEILING | 向上取整,即取整为值大的方向 | BigDecimal.ROUND_CEILING |
| FLOOR | 向下取整,即取整为值小的方向 | BigDecimal.ROUND_FLOOR |
| HALF_UP | 四舍五入取整 | BigDecimal.ROUND_HALF_UP |
| HALF_DOWN | 五舍六入取整 | BigDecimal.ROUND_HALF_DOWN |
| HALF_EVEN | 邻近取整,如果两边距离一样,则取偶数 | BigDecimal.ROUND_HALF_EVEN |
| UNNECESSARY | 必要取整。如果不能直接取整,报错 | BigDecimal.ROUND_UNNECESSARY |
Integer
在Java中Integer有一个特性:Integer会缓存-128~127之间的数值,如果进行 -128~127 之间的比较,进行的是数值的比较。而不在这个范围内的整数比较则是进行的 引用比较。所以就有了:
Integer a = 100;Integer b = 100;Integer c = 200;Integer d = 200;a==b; // truec==d; // false
Stream语法
list.stream.map(x->x.getId)
Peek
Stream中有一个操作符,叫做 peek ,这个操作符的作用就是改变输入中的某个内部值/状态。流过peek的值的内容会改变,而类型则保持不变。
Reduce
Stream中有一个聚合操作符Reduce。这个操作符是按照一定的规则把一个list中的值最终聚合成为一个值。比如把一个整型的数组聚合成其和,或其积。或把一个字符串数组聚合成一个字符串。示例如下:
class ReduceExamplte {public static void main(){List<Integer> list = new ArrayList<>();list.add(1);list.add(2);list.add(3);list.add(4);Integer sum = list.stream().reduce(0,(a,b)->a+b);// 下面是sum的另一种写法Integer sum1 = list.stream().reduce(Integer::sum)Integer multi = list.stream().reduce(1,(a,b)->a*b);List<String> strList = new ArrayList<>();strList.add("hello");strList.add("Java");strList.add("JS");strList.add("Rust");String hello = strList.stream().reduce("",(a,b)->a+b);}}
需要注意的是,reduce方法有三个重载形式。
T reduce(T identity, BinaryOperator<T> accumulator);Optional<T> reduce(BinaryOperator<T> accumulator);(U) U reduce(U identity, BiFunction<U, ? super T, U> accumulator, BinaryOperator<U> combiner);
Stream to HashMap
import java.math.BigDecimal;import java.util.HashMap;import java.util.HashSet;import java.util.Map;import java.util.stream.Collectors;class Stream2HashMap {public static void main(String[] args) {HashMap<String, BigDecimal> map = new HashMap<>();map.put("a", BigDecimal.valueOf(10));map.put("b", BigDecimal.valueOf(20));map.put("c", BigDecimal.valueOf(30));HashMap<String, BigDecimal> map1 = map.entrySet().stream().peek(x -> {BigDecimal bigDecimal = x.getValue();x.setValue(bigDecimal.multiply(BigDecimal.valueOf(10)));}).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, HashMap::new));map1.entrySet().forEach(System.out::println);}}
Optional类型
Optional类型是在Java函数式编程中常常用到的这个类型。这个类型可以使得在函数式编程中避免因为null的判断打断编程的流畅性。
Optional类型在Java中是一个可以为null的容器对象。这里要注意的是,Optional是一个容器对象。它可以保存类型T的值,也可以仅仅保存null。下面是Optional的类方法:
| 方法 | 描述 |
|---|---|
| static |
返回空的Optional实例 |
| boolean equals(Object obj) | 判断其他对象是否等于Optional |
| Optional |
如果值存在,并且这个值匹配给定的perdicate,返回一个Optional用以描述这个值,否则返回一个空的Optional |
| Optional flatMap(Function<? super T,Optional> mapper) | 如果值存在,返回基于Optional包含的映射方法的值,否则返回一个空的Optional |
| T get() | 如果在这个Optinal中包含这个值,返回值,否则抛出异常:NoSuchElementException |
| Int hasCode() | 返回存在值的哈希值,如果不存在,返回0 |
| void IfPersent(Consumer <? super T> consumer) | 如果值存在则使用该值调用 consumer,否则不做任何事情。 |
| boolean IsPersent() | 如果值存在则方法返回true,否则返回false. |
| Optional |
如果有值,则对于执行调用映射得到返回值。如果返回值不为null,则创建包含映射返回值的Optional作为map方法返回值,否则返回空Optional。 |
| static |
返回一个指定非Null值的Optional |
| static |
如果为非空,返回Optional描述的指定值,否则返回空的Optional。 |
| T orElse |
如果存在该值,返回值,否则返回 other. |
| T orElseGet(Supplier<? extends T> other) | 如果存在该值,返回值,否则触发other,并返回other调用的结果。 |
| 如果存在该值,返回包含的值,否则抛出Supplier继承的异常 | |
| String toString() | 返回一个Optional的非空字符串,用来调试 |
应用示例
避免null的空指针判断:
public class Test {public static void main(String args[]){List<String> stringListNull = null;List<String> stringList = new ArrayList<>();stringList.add("1");List<String> stringList1 = Optional.ofNullable(stringListNull).orElse(stringList);}}

