1、Collections.unmodifiableList(List list)
它可以返回一个只读的list,在防止调用方胡乱修改l时可以用它。
2、Class.forName()
1)可以在加载某一个类时使用
2)可以在判断一个类是否存在时也可以用它
举个例子
package com.spring.schema.spring_schema2;
import com.alibaba.dubbo.common.compiler.support.JavassistCompiler;
public class ComplieTest {
public static void main(String[] args) throws Throwable {
String code="package com.spring.schema.spring_schema2;";
code+="public class helloWorld {";
code+=" public void testHello(String name){";
code+="System.out.println( name+\":hello world\");";
code+="}";
code+="}";
//这是dubbo里面的类,借用下
JavassistCompiler jc=new JavassistCompiler();
try{
Object hl2 = Class.forName("com.spring.schema.spring_schema2.helloWorld").newInstance();
hl2.getClass().getMethod("testHello",String.class).invoke(hl2, "张三");
}catch(ClassNotFoundException e){
Object hl=jc.doCompile("com.spring.schema.spring_schema2.helloWorld", code).newInstance();
hl.getClass().getMethod("testHello",String.class).invoke(hl, "张三2");
}
try{
Object hl2 = Class.forName("com.spring.schema.spring_schema2.helloWorld").newInstance();
hl2.getClass().getMethod("testHello",String.class).invoke(hl2, "张三");
}catch(ClassNotFoundException e){
Object hl=jc.doCompile("com.spring.schema.spring_schema2.helloWorld", code).newInstance();
hl.getClass().getMethod("testHello",String.class).invoke(hl, "张三2");
}
}
}
打印结果为:
张三2:hello world
张三:hello world
3、获取当前机器可用的接口
public static int getAvailablePort(int port) {
if (port <= 0) {
return getAvailablePort();
}
for(int i = port; i < MAX_PORT; i ++) {
ServerSocket ss = null;
try {
ss = new ServerSocket(i);
return i;
} catch (IOException e) {
// continue
} finally {
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
}
}
}
}
return port;
}
4、spring 获取 默认的ClassLoader的方法
public static ClassLoader getDefaultClassLoader() {
ClassLoader cl = null;
try {
cl = Thread.currentThread().getContextClassLoader();
}
catch (Throwable ex) {
// Cannot access thread context ClassLoader - falling back...
}
if (cl == null) {
// No thread context class loader -> use class loader of this class.
cl = ClassUtils.class.getClassLoader();
if (cl == null) {
// getClassLoader() returning null indicates the bootstrap ClassLoader
try {
cl = ClassLoader.getSystemClassLoader();
}
catch (Throwable ex) {
// Cannot access system ClassLoader - oh well, maybe the caller can live with null...
}
}
}
return cl;
}
5、数组和list之间的转换
/**
* 数组转List
* @param obj
* @return
*/
public static <T> List<T> array2List(T[] obj){
if(obj==null){
return null;
}
List<T> list = new ArrayList<T>();
list.addAll(Arrays.asList(obj));
return list;
}
/**
* list转数组
* @param obj
* @return
*/
public static <T> T[] list2Array(List<T> list){
if(list==null || list.size()==0){
return null;
}
return list.toArray((T[])Array.newInstance(list.get(0).getClass(), list.size()));
}
6、代码里this代表谁(这个知识点貌似太简单了,可有时就会一根筋去较真,特别是在一眼看不见头的类里迷失了方向)
package com.cn.spring.util;
public class Person {
private String name;
private String age;
public Person(String name,String age){
this.name=name;
this.age=age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public Person getThis() {
return this;
}
}
package com.cn.spring.util;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.StringTokenizer;
public class SpringUtil {
Person p=new Person("bb", "12");
public void testThis(){
System.out.println(this);
System.out.println(this.p.getThis());
}
public static void main(String[] args) {
new SpringUtil().testThis();
}
}
结果是:
com.cn.spring.util.SpringUtil@37c390b8
com.cn.spring.util.Person@8523ca2
7、HashSet和HashMap的参数initialCapacity和loadFactor
HashSet的底层就是用HashMap实现的,所以对于它们两个来说,这两个参数的意义是一样的。hashset的两个构造函数如下:
/**
* Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
* the specified initial capacity and the specified load factor.
*
* @param initialCapacity the initial capacity of the hash map
* @param loadFactor the load factor of the hash map
* @throws IllegalArgumentException if the initial capacity is less
* than zero, or if the load factor is nonpositive
*/
public HashSet(int initialCapacity, float loadFactor) {
map = new HashMap<>(initialCapacity, loadFactor);
}
/**
* Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
* the specified initial capacity and default load factor (0.75).
*
* @param initialCapacity the initial capacity of the hash table
* @throws IllegalArgumentException if the initial capacity is less
* than zero
*/
public HashSet(int initialCapacity) {
map = new HashMap<>(initialCapacity);
}
那么这两个参数应该怎么用呢,initialCapacity是初始容量,loadFactor是加载因子,默认情况下初始容量为 16,负载因子为 0.75。也就是有16个链表,当75%的链表里有值的时候,hashset和hashmap就会重构,默认下一次是重构是当前的2倍大小,大家明白了吧,如果自己的hashset和hashmap要存储大量的数据比较说10000条,而不设置initialCapacity,就会造成多次的重构,重构一次就会把所有的数据重新计算位置,重新编排,很浪费性能。大家可以根据自己的需要来设置这两个值。