// 空对象模式示例interfaceAnimal{voidmakeSound();}classDogimplementsAnimal{publicvoidmakeSound(){System.out.println("Woof");}}classNullAnimalimplementsAnimal{publicvoidmakeSound(){// do nothing}}// 用法publicclassNullObjectPatternExample{publicstaticvoidmain(String[]args){Animalanimal=getAnimal("cat"...
public Stream<String> collectionToStream(Collection<String> collection) { return Optional.ofNullable(collection) .map(Collection::stream) .orElseGet(Stream::empty); } Optional.ofNullable(collection)creates anOptionalobject from the passed-in collection. An emptyOptionalobject is created if the collection...
String tsb=JSON.toJSONString(b); a=JSON.to(Dog.class, tsb); b=JSON.to(Dog.class, tsa); }publicstaticvoidmain(String[] args)throwsIllegalAccessException, IllegalArgumentException, InvocationTargetException { TestPassObjectParam test=newTestPassObjectParam();//演示基本类型和对象类型传递//a.1 ...
publicvoidadd(Ee){i.add(typeCheck(e));}EtypeCheck(Object o){if(o!=null&&!type.isInstance(o))thrownewClassCastException(badElementMsg(o));return(E)o;} 这一组的函数可以在开发中多用,尽量避免因为不小心或者因为多人合作的原因出现一些异常。 2.4 emptyxxx 返回空的集合 这个操作有点骚,就是直接...
其中的set(Object obj, Object value)方法是Field类本身的方法,用于设置字段的值,而get(Object obj)则是获取字段的值,当然关于Field类还有其他常用的方法如下:
* List<String> s = Collections.emptyList(); * </pre> * * @implNote * Implementations of this method need not create a separate <tt>List</tt> * object for each call. Using this method is likely to have comparable * cost to using the like-named field. (Unlike this method, the fie...
you should strive to use theOptionaltype instead of null. The Optional type was introduced in Java 8 and is a generic container for any object. It was introduced to give developers a way to indicate that a method might return nothing, which you previously couldn’t (at least not without ...
public class Hello implements java.io.Serializable { String name; private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException { Runtime.getRuntime().exec(name); } } 该类在反序列化的时候会执行命令,我们构造一个序列化的对象,name为恶意命令,那么在反序列化的时候...
下面是 String 类支持的方法,更多详细,参看Java String API文档。 基本上涉及到的String的操作,JDK都已经设置好了。所以,这个String类的修饰符中有一个final,表示这个类就是一个比较完美的类。 publicfinalclassString{privatefinalbyte[]value;} 声明方法的类 java.lang.Object ...
Iterator可用来遍历Set和List集合,但是ListIterator只能用来遍历List。Iterator对集合只能是前向遍历,ListIterator既可以前向也可以后向。ListIterator实现了Iterator接口,并包含其他的功能,比如:增加元素,替换元素,获取前一个和后一个元素的索引,等等。22.快速失败(fail-fast)和安全失败(fail-safe)的区别是什么?I...