1.Class对象
类名.class
获取一个类的类对象,通过这个Class类型的对象,访问类中的方法等
2.数组
2.1.拷贝数组
System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
src – the source array.
srcPos – starting position in the source array.
dest – the destination array.
destPos – starting position in the destination data.
length – the number of array elements to be copied
2.2.转换为流
Arrays.stream(T[] array)
通过流对数组进行中间操作:过滤、操作元素等操作 ,最终通过终结操作得到想要的结果。
String strings[] = {"",""};
Arrays.stream(strings).filter(s->!"".equals(s)).forEach(item->{
//操作
System.out.println(item);
});
2.3.排序
int[] array = {2,1};
Arrays.sort(array);
2.4.转集合
List<Integer> list = Arrays.asList(1, 2);
使用时,不能使用其修改集合相关的方法
public static void main(String[] args) {
List<Integer> statusList = Arrays.asList(1, 2);
System.out.println(statusList);
System.out.println(statusList.contains(1));
System.out.println(statusList.contains(3));
//向集合中添加元素
statusList.add(3);
System.out.println(statusList.contains(3));
}
运行结果:
原因:
asList()方法返回的对象是Arrays类的内部类,虽然继承了AbstractList类,却没有重写相关方法
3.反射
3.1.反射获取参数名
Class<String> stringClass = String.class;
try {
Method method = stringClass.getMethod("indexOf", int.class);
Parameter[] parameters = method.getParameters();
for (Parameter parameter : parameters) {
System.out.println("name of param:"+parameter.getName());
}
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
4.集合
4.1.转为流
List<String> strings = new ArrayList<>();
List<String> insertUserList = strings.stream().filter(m -> m != null).collect(Collectors.toList());
4.2.排序
List<Integer> list = Arrays.asList(4, 3);
Collections.sort(list);
5.String
5.1.子串位置
String m = "java world hello";
System.out.println(aaaa.indexOf("world",0));
str – 字串.
fromIndex – 开始位置.
应用场景:
求子串重复出现的次数
5.2.拼串
每次拼串都会产生新的对象,如果想保证每次取出相同的对象,需要使用intern()方法,该方法就是把字符串的内容放入到常量池中。
String 想要的字符串 = 字符串内容.toString().intern();
public class MyStringSplice {
public static void main(String[] args) {
Thread[] threads = new Thread[8];
for (int i = 0; i < 8; i++) {
threads[i] = new Thread(new StringThread("192.168.1.1"));
}
for (int i = 0; i < 8; i++) {
threads[i].start();
}
}
public static class StringThread implements Runnable{
private static final String LOCK_PREFIX = "My---";
private String taskNo;
public StringThread(String taskNo) {
this.taskNo = taskNo;
}
@Override
public void run() {
//拼串
//String lock = buildLock();
//拼串后加入常量池=====start
String lock = LOCK_PREFIX +taskNo;
//拼串后加入常量池拼串后加入常量池=====end
String aaa = lock.intern();
synchronized (aaa) {
System.out.println("[" + Thread.currentThread().getName() + "]开始运行了");
// 休眠5秒模拟脚本调用
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("[" + Thread.currentThread().getName() + "]结束运行了");
}
}
private String buildLock() {
StringBuilder sb = new StringBuilder();
sb.append(LOCK_PREFIX);
sb.append(taskNo);
String lock = sb.toString();
System.out.println("[" + Thread.currentThread().getName() + "]构建了锁[" + lock + "]");
return lock;
}
}
}
6.Calendar
6.1.设置时间
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
//设置小时
calendar.set(Calendar.HOUR_OF_DAY,8);
//设置分钟
calendar.set(Calendar.MINUTE, 0);
//解决Mysql设置秒数多一秒
cal.set(Calendar.MILLISECOND,0);
//设置秒
calendar.set(Calendar.SECOND, 0);
//年份加1
calendar.add(Calendar.YEAR,1);
7.匿名内部类
定义匿名内部类的前提:内部类必须是继承一个抽象类或者实现接口。只要一个类是抽象的或是一个接口,那么其子类中的方 法都可以使用匿名内部类来实现
匿名内部类的格式: new 父类或者接口(){
定义子类的内容
}
8.多线程
8.1 保证顺序执行
CountDownLatch、CyclicBarrier、Phaser、Semaphore、AtomicInteger
、
CompletableFuture、Thread的join、ReentrantLock和Condition
8.2 两个线程交换数据
java.uitl.Concurrent.Exchanger:简洁高效地实现两个线程间的数据交换,避免复杂的锁和同步机制
public class MyExchanger {
public static void main(String[] args) {
Exchanger<String> exchanger = new Exchanger<>();
Runnable taskA = () -> {
try {
String message = exchanger.exchange("Data A");
System.out.println("taskA receive message from B:"+ message);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.err.println("Thread A was interrupted");
}
};
Runnable taskB = () -> {
try {
String message = exchanger.exchange("Data B");
System.out.println("taskB receive message from A:"+ message);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.err.println("Thread B was interrupted");
}
};
CompletableFuture.allOf(runAsync(taskA), runAsync(taskB)).join();
}
}
运行结果:
9.队列
9.1 控制线程的顺序
java.util.concurrent.BlockingQueue:利用put()、take()会一直阻塞的特点
public class MyBlockingQueue {
private static final BlockingQueue<Integer> queue1 = new ArrayBlockingQueue<>(1);
private static final BlockingQueue<Integer> queue2 = new ArrayBlockingQueue<>(1);
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
try {
System.out.println("Thread 1");
queue1.put(1); // Signal t2 to run
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Thread t2 = new Thread(() -> {
try {
queue1.take(); // Wait for signal from t1
System.out.println("Thread 2");
queue2.put(1); // Signal t3 to run
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Thread t3 = new Thread(() -> {
try {
queue2.take(); // Wait for signal from t2
System.out.println("Thread 3");
} catch (InterruptedException e) {
e.printStackTrace();
}
});
t1.start();
t2.start();
t3.start();
}
}