1.字符串判空

String a = "";
String b = " ";
String d = "\r\n";
String c = "A";
System.out.println("a: " + a.isBlank() + " " + a.isEmpty());
 //a: true true
System.out.println("b: " + b.isBlank() + " " + b.isEmpty());
 //b: true false
System.out.println("d: " + d.isBlank() + " " + d.isEmpty());
 //d: true false
System.out.println("c: " + c.isBlank() + " " + c.isEmpty()); //c: false false

2.列表判空

        List<UserE> userEList = null;
        System.out.println(CollectionUtils.isEmpty(userEList));
 //true
        List<UserE> userEList2 = new ArrayList<>();
        System.out.println(CollectionUtils.isEmpty(userEList2));
 //true
        userEList2.add(new UserE());
        System.out.println(CollectionUtils.isEmpty(userEList2)); //false

3.返回空数组或空集 替代 null

public List<User> getUserList() {
    return Collections.emptyList();
}
public Map<String,String> getUser() {
    return Collections.emptyMap();
}

4.Map迭代

        Map<String, String> map = new HashMap<>();
        map.put("1111", "2222");
        for( Map.Entry<String, String> entry : map.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            System.out.println("key:" + key + ", value:" + value);
        }
//key:1111, value:2222

        Set<String> keys = map.keySet();
        System.out.println(keys);
//[1111]

5.ArrayList 遍历

        List<UserE> userEList2 = new ArrayList<>();
        userEList2.add(new UserE());

        if (userEList2 instanceof RandomAccess) {
            System.out.println("-----------RandomAccess----------------");
            for(int i=0, size = userEList2.size(); i < size; i++) {
                System.out.println(userEList2.get(i));
            }
        } else {
            System.out.println("-----------Iterator----------------");
            Iterator<UserE> userEIterator = userEList2.iterator();
            while (userEIterator.hasNext()) {
                System.out.println(userEIterator.next());
            }
        }
//-----------RandomAccess----------------
//xxx.UserE@4eec7777

(50)

Related Post