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);
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 – 开始位置.
应用场景:
求子串重复出现的次数
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();
}
}