Objects.equals(a,b) //检验A和B的相等性
//equals方法要求具有自反性、对称性、传递性、一致性(反复调用结果应当一致),以及非空!=null
getclass().getName()//可以获得类名字符串
A instanceof B运算可以判定一个实例是否是给定类的实例
泛型
//用以下方式定义一个泛型类:
public class ClassName<T,U>{
private T field;
...
}
//用以下方式定义一个泛型方法:
class ClassName {
public <T> T getString(T a){
...
}
}
//用以下方式对T的方法进行限定(良心啊):
class ClassName {
public <T extends SomeInterfaceOrClass & SomeOtherInterface> T getString(T a){//限定使用&分隔,而类型变量用逗号分隔;限定中至多有一个是类
...
}
}
//泛型类/接口可以普通地继承:
public class Something<E> implements Parent<E>
import java.util.Collections;
//Collection 接口的基本方法:
boolean add(E ele); // add element
Iterator<E> iterator();//return iterator
int size(); //return size
boolean isEmpty()
boolean contains(Object obj)
boolean addAll(Collection c)
boolean remove(Ojbect obj)
void clear()
//Iterator<E>'s basic method:
boolean hasNext();
E hasNext();
//so you can iterate over the collection like this:
while (iter.hasNext()){
ClassName ele = iter.next();
...
}
//to make it easier, try for each:
for (ClassName ele : c) {
//do something
}//this works for all classes that impl Collection
//can reduce words with lambda expression:
iterator.forEachRemaining(ele -> System.out.println(ele));
列表接口List
//List is sequential and can be accessed by index
集合接口Set
//Set can store no duplicate elements judged by equals()