List
将拼接的字符串转为集合
String vehicleIds = “1,2,3,4”;
List<Integer> ids = Arrays.stream(vehicleIds.split(",")).map(Integer::parseInt).collect(Collectors.toList());
Collections.synchronizedList()构造一个线程安全的对象
List<Integer> success = Collections.synchronizedList(new ArrayList<>());
toHexString()方法
长类toHexString()方法 (Long class toHexString() method)
toHexString() method is available in java.lang package.
toHexString()方法在java.lang包中可用。
toHexString() method is used to represent a hexadecimal string of the given parameter [value] of long type as an unsigned long in base 16.
toHexString()方法用于将long类型给定参数[value]的十六进制字符串表示为以16为基数的无符号long。
toHexString() method is a static method, it is accessible with the class name too and if we try to access the method with the class object then also we will not get an error.
toHexString()方法是一个静态方法,也可以使用类名进行访问,并且如果我们尝试使用类对象访问该方法,那么我们也不会收到错误。
toHexString() method does not throw an exception at the time of conversion from long integer to Hexadecimal string.
从长整数转换为十六进制字符串时, toHexString()方法不会引发异常。
StringUtils中常用方法leftPad(),rightPad(),center()
左侧填充:
leftPad():
StringUtils.leftPad(String str,int size);
StringUtils.leftPad(String str,int size,String padStr);
右侧填充:
rightPad():
StringUtils.rightPad(String str,int size);
StringUtils.rightPad(String str,int size,String padStr);
中间填充:
center():
StringUtils.center(String str, int size);
StringUtils.center(String str, int size, String padString);
例:
StringUtils.center("abcdef", 20);
返回结果(左右各7个空格):abcdef
StringUtils.center("abcdef", 20,"*_");
返回结果:*_*_*_abcdef*_*_*_*_
StringUtils.leftPad("abc", 10, "*");
返回结果:*******abc
StringUtils的isNotBlank方法
当Str为空白或者null时,isNotBlank返回false
当Str的length>0时,isNotBlank返回true
CollectionUtils.isEmpty
例1: 判断集合是否为空:
CollectionUtils.isEmpty(null): true
CollectionUtils.isEmpty(new ArrayList()): true
CollectionUtils.isEmpty({a,b}): false
例2: 判断集合是否不为空:
CollectionUtils.isNotEmpty(null): false
CollectionUtils.isNotEmpty(new ArrayList()): false
CollectionUtils.isNotEmpty({a,b}): true
emptyList()方法的使用
通过java.util.Collections.emptyList()方法的相关源码可以得知它实际上就是返回了一个空的List,但是这个List和我们平时常用的那个List是不一样的。这个方法返回的List是Collections类的一个静态内部类,它继承AbstractList后并没有实现add()、remove()等方法,因此这个返回值List并不能增加删除元素。
既然这个List不能进行增删操作,那么它有何意义呢?
这个方法主要目的就是返回一个不可变的列表,使用这个方法作为返回值就不需要再创建一个新对象,可以减少内存开销。并且返回一个size为0的List,调用者不需要校验返回值是否为null,所以建议使用这个方法返回可能为空的List。