简体   繁体   English

如何检查List是否只包含来自给定范围JAVA的元素?

[英]How to Check if a List contains elements only from a given range JAVA?

What is the efficient way to test whether a list has values which are only from a given range? 测试列表是否具有仅来自给定范围的值的有效方法是什么?

Eg. List = 1,6,0,4556 
Range = 0 - 10
so here isValid(list) = false // 4556 is not in the range

Eg. List = 188,8,0,-90 
Range = 0 - 10
so here isValid(list) = false // -90 and 188 are not in the range

Eg. List = 1 ,8,0 
Range = 0 - 10
so here isValid(list) = true 

Using Java 8 primitive IntStream: 使用Java 8原语IntStream:

IntPredicate contains = value -> 0 <= value && value <= 10;
Assert.assertFalse(
        IntStream.of(1, 6, 0, 4556).allMatch(contains));
Assert.assertFalse(
        IntStream.of(188, 8, 0, -90).allMatch(contains));
Assert.assertTrue(
        IntStream.of(1, 8, 0).allMatch(contains));

Using Eclipse Collections primitive IntList: 使用Eclipse Collections原语IntList:

IntPredicate contains = IntInterval.zeroTo(10)::contains;
Assert.assertFalse(
        IntLists.mutable.with(1, 6, 0, 4556).allSatisfy(contains));
Assert.assertFalse(
        IntLists.mutable.with(188, 8, 0, -90).allSatisfy(contains));
Assert.assertTrue(
        IntLists.mutable.with(1, 8, 0).allSatisfy(contains));

In both cases here the int values will not be boxed as Integers, which may make it more efficient. 在这两种情况下,int值都不会被装箱为Integers,这可能会提高效率。

Note: I am a committer for Eclipse Collections. 注意:我是Eclipse Collections的提交者。

I originally mentioned Guava's RangeSet , but I'm not sure if it's applicable to List s with arbitrary elements. 我最初提到了Guava的RangeSet ,但我不确定它是否适用于具有任意元素的List

Anyway, you can use the following with Java 8: 无论如何,您可以在Java 8中使用以下内容:

public static void main(String[] args) {
    List<Integer> list = Arrays.asList(1, 6, 0, 4556);

    System.out.println(inRange(list, 0, 10));
}

private static boolean inRange(List<Integer> list, int min, int max) {
    return list.stream().allMatch(i -> i >= min && i <= max);
}

>> false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查字符串是否包含与Java中给定范围不同的字母 - How to check if a string contains a letter different then a given range in Java 如何检查列表是否包含Java中给定顺序的子列表 - How to check if a list contains a sublist in a given order in Java 如何使用Java 8中的lambda和流检查整数类型的所有元素是否在给定范围内? - How to check if all elements of type integer is within given range using lambdas and streams in Java 8? 如何检查List是否按顺序包含多个元素 - How to check if List contains several elements in order 给定列表时,如何检查元素是否相等 - When given a list, how to check if elements are equal, or not 如何检查字符串是否仅包含 Java 中的数字 - How to check if a string contains only digits in Java 如何检查字符串是否包含特定范围内的数字,例如在Java中从11到26 - How to check if the string contains numbers from a specific range, for eg from 11 to 26 in java 使用 java stream API 检查列表元素是否是整数的连续范围 - Check if list elements are a continuous range of integers using java stream API java程序如何检查数组是否包含给定的两个不同值? - How to java Program to Check if an array contains a given two different values? 列表<Map<String, String> &gt; 包含来自 String[] 的所有元素。 如何检查? - List<Map<String, String>> contains all elements from String[]. How to check it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM