首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

在java 8中检查列表是否同时不为空和不为空

在Java 8中,可以使用Stream API来检查列表是否同时不为空和不为null。以下是一个示例代码:

代码语言:txt
复制
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> list1 = List.of("apple", "banana", "orange");
        List<String> list2 = List.of("cat", "dog", "bird");
        List<String> list3 = List.of();

        boolean bothNotEmpty = checkListsNotEmpty(list1, list2);
        System.out.println("Both lists not empty: " + bothNotEmpty);

        boolean bothNotEmpty2 = checkListsNotEmpty(list1, list3);
        System.out.println("Both lists not empty: " + bothNotEmpty2);
    }

    public static boolean checkListsNotEmpty(List<?> list1, List<?> list2) {
        return !list1.isEmpty() && !list2.isEmpty();
    }
}

输出结果:

代码语言:txt
复制
Both lists not empty: true
Both lists not empty: false

在上述示例中,我们定义了一个checkListsNotEmpty方法,该方法接受两个List参数,并使用isEmpty()方法来检查列表是否为空。如果两个列表都不为空,则返回true,否则返回false

这种方法适用于任何类型的列表,无论是字符串列表还是自定义对象列表。它可以用于检查多个列表是否同时不为空和不为null。

腾讯云相关产品和产品介绍链接地址:

请注意,以上链接仅供参考,具体产品选择应根据实际需求进行评估和决策。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券