1、UUID类
产生随机数:默认为32位,提供以下两个方法;
- andomUUID()是随机(适用于唯一订单号)的。
- nameUUIDFromBytes(byte[] n)会根据n产生唯一的uuid。只要有用户的唯一性信息。就能保证此用户的uuid的唯一性。例如(身份证号等)。
UUID uuid = UUID.randomUUID();
System.out.println(uuid.toString());
2、Optional类
进行null(NullPointerException))的相关处理。Optional 类是一个可以为null的容器对象。如果值存在则isPresent()方法会返回true,调用get()方法会返回该对象。
import java.util.Optional;
public class Java8Tester {
public static void main(String args[]){
Java8Tester java8Tester = new Java8Tester();
Integer value1 = null;
Integer value2 = new Integer(10);
Optional<Integer> a = Optional.ofNullable(value1);
Optional<Integer> b = Optional.of(value2);
System.out.println(java8Tester.sum(a,b));
}
public Integer sum(Optional<Integer> a, Optional<Integer> b){
System.out.println("第一个参数值存在: " + a.isPresent());
System.out.println("第二个参数值存在: " + b.isPresent());
Integer value1 = a.orElse(new Integer(0));
Integer value2 = b.get();
return value1 + value2;
}
}
执行结果
$ javac Java8Tester.java
$ java Java8Tester
第一个参数值存在: false
第二个参数值存在: true
10
3、ThreadLocal类
ThreadLocal是除了加锁这种同步方式之外的一种保证一种规避多线程访问出现线程不安全的方法。
public class ThreadLocalTest {
static ThreadLocal<String> localVar = new ThreadLocal<>();
static void print(String str) {
System.out.println(str + " :" + localVar.get());
localVar.remove();
}
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
localVar.set("localVar1");
print("thread1");
System.out.println("after remove : " + localVar.get());
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
localVar.set("localVar2");
print("thread2");
System.out.println("after remove : " + localVar.get());
}
});
t1.start();
t2.start();
}
}
执行结果:
thread2 :localVar2
thread1 :localVar1
after remove : null
after remove : null
4、定时调度
指定时间后执行任务
class MyTask extends TimerTask {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+":"+System.currentTimeMillis());
}
}
调用
Timer timer = new Timer();
timer.schedule(new MyTask() ,0);
5、Base64加密与解密
package com.alan;
import java.util.Base64;
public class Base64Test {
private static String src = "imooc security base64";
public static void main(String[] args) {
bouncyCastleBase64(src);
}
public static void jdkBase64(String src){
Base64.Encoder encoder = Base64.getEncoder();
String s = encoder.encodeToString(src.getBytes());
System.out.println("加密:"+s);
Base64.Decoder decoder = Base64.getDecoder();
System.out.println("解密:"+ new String(decoder.decode(s)));
}
public static void commonsCodesBase64(String src){
byte[] bytes = org.apache.commons.codec.binary.Base64.encodeBase64(src.getBytes());
System.out.println("加密:"+new String(bytes));
byte[] bytes1 = org.apache.commons.codec.binary.Base64.decodeBase64(bytes);
System.out.println("解密:"+ new String(bytes1));
}
public static void bouncyCastleBase64(String src){
byte[] encode = org.bouncycastle.util.encoders.Base64.encode(src.getBytes());
System.out.println("加密1:"+new String(encode));
byte[] decode = org.bouncycastle.util.encoders.Base64.decode(encode);
System.out.println("解密1:"+ new String(decode));
}
}
执行结果
加密:aW1vb2Mgc2VjdXJpdHkgYmFzZTY0
解密:imooc security base64
为了增加安全性;可以插入盐值,多次加密; 如加密:123456 ,可增加盐值变为:123{456},再次加密。
|