系统变量

当前系统的换行符

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,CEILINGFLOOR
  • 半数取整:HALF_UP,HALF_DOWNHALF_EVEN
  • 必要取整:UNNECEESARY

    直接取整:

    直接取整我们可以分正数取整和负数取整。
    当正数取整的时候,UPCEILING的行为是一样的,都是直接向上取整。同时,DOWNFLOOR的行为是一样的,都是直接向下取整。
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

当负数取数的时间,UPFLOOR的行为是一样的,都是取绝对值小的负数。同时,DOWNCEILING的行为是一样的,都是取绝对值大的负数。

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 之间的比较,进行的是数值的比较。而不在这个范围内的整数比较则是进行的 引用比较。所以就有了:

  1. Integer a = 100;
  2. Integer b = 100;
  3. Integer c = 200;
  4. Integer d = 200;
  5. a==b; // true
  6. c==d; // false

Stream语法

  1. list.stream.map(x->x.getId)

Peek

Stream中有一个操作符,叫做 peek ,这个操作符的作用就是改变输入中的某个内部值/状态。流过peek的值的内容会改变,而类型则保持不变。

Reduce

Stream中有一个聚合操作符Reduce。这个操作符是按照一定的规则把一个list中的值最终聚合成为一个值。比如把一个整型的数组聚合成其和,或其积。或把一个字符串数组聚合成一个字符串。示例如下:

  1. class ReduceExamplte {
  2. public static void main(){
  3. List<Integer> list = new ArrayList<>();
  4. list.add(1);
  5. list.add(2);
  6. list.add(3);
  7. list.add(4);
  8. Integer sum = list.stream()
  9. .reduce(0,(a,b)->a+b);
  10. // 下面是sum的另一种写法
  11. Integer sum1 = list.stream()
  12. .reduce(Integer::sum)
  13. Integer multi = list.stream()
  14. .reduce(1,(a,b)->a*b);
  15. List<String> strList = new ArrayList<>();
  16. strList.add("hello");
  17. strList.add("Java");
  18. strList.add("JS");
  19. strList.add("Rust");
  20. String hello = strList.stream()
  21. .reduce("",(a,b)->a+b);
  22. }
  23. }

需要注意的是,reduce方法有三个重载形式。

  1. T reduce(T identity, BinaryOperator<T> accumulator);
  2. Optional<T> reduce(BinaryOperator<T> accumulator);
  3. (U) U reduce(U identity, BiFunction<U, ? super T, U> accumulator, BinaryOperator<U> combiner);

Stream to HashMap

  1. import java.math.BigDecimal;
  2. import java.util.HashMap;
  3. import java.util.HashSet;
  4. import java.util.Map;
  5. import java.util.stream.Collectors;
  6. class Stream2HashMap {
  7. public static void main(String[] args) {
  8. HashMap<String, BigDecimal> map = new HashMap<>();
  9. map.put("a", BigDecimal.valueOf(10));
  10. map.put("b", BigDecimal.valueOf(20));
  11. map.put("c", BigDecimal.valueOf(30));
  12. HashMap<String, BigDecimal> map1 = map
  13. .entrySet()
  14. .stream()
  15. .peek(x -> {
  16. BigDecimal bigDecimal = x.getValue();
  17. x.setValue(bigDecimal.multiply(BigDecimal.valueOf(10)));
  18. })
  19. .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, HashMap::new));
  20. map1.entrySet().forEach(System.out::println);
  21. }
  22. }

Optional类型

Optional类型是在Java函数式编程中常常用到的这个类型。这个类型可以使得在函数式编程中避免因为null的判断打断编程的流畅性。
Optional类型在Java中是一个可以为null的容器对象。这里要注意的是,Optional是一个容器对象。它可以保存类型T的值,也可以仅仅保存null。下面是Optional的类方法:

方法 描述
static Optional empty() 返回空的Optional实例
boolean equals(Object obj) 判断其他对象是否等于Optional
Optional filter(Predicate<?super predicate) 如果值存在,并且这个值匹配给定的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 map(Function<? super T,? extends U mapper) 如果有值,则对于执行调用映射得到返回值。如果返回值不为null,则创建包含映射返回值的Optional作为map方法返回值,否则返回空Optional。
static Optional of(T value) 返回一个指定非Null值的Optional
static Optional ofNullable 如果为非空,返回Optional描述的指定值,否则返回空的Optional。
T orElse 如果存在该值,返回值,否则返回 other.
T orElseGet(Supplier<? extends T> other) 如果存在该值,返回值,否则触发other,并返回other调用的结果。
T orElseThrow exceptionSupplier> 如果存在该值,返回包含的值,否则抛出Supplier继承的异常
String toString() 返回一个Optional的非空字符串,用来调试

应用示例

避免null的空指针判断:

  1. public class Test {
  2. public static void main(String args[]){
  3. List<String> stringListNull = null;
  4. List<String> stringList = new ArrayList<>();
  5. stringList.add("1");
  6. List<String> stringList1 = Optional
  7. .ofNullable(stringListNull)
  8. .orElse(stringList);
  9. }
  10. }

欢迎赞赏