Java 8 注意:可以从列表中获取不同的int[<;int[]>;

Java 8 注意:可以从列表中获取不同的int[<;int[]>;,java-8,Java 8,我正在尝试使用unique int[]获取不同的列表,但仍然会得到重复的列表 List<Integer> set1 = Arrays.asList(2, 3, 4, 5, 10, 4, 5, 6, 4); List<Integer> count = new ArrayList<>(); List<int[]> combination = set1.stream().flatMap(i -> set1.stream().

我正在尝试使用unique int[]获取不同的列表,但仍然会得到重复的列表

List<Integer> set1 = Arrays.asList(2, 3, 4, 5, 10, 4, 5, 6, 4);
List<Integer> count = new ArrayList<>();
List<int[]> combination = set1.stream().flatMap(i -> set1.stream().
            flatMap(j -> set1.stream().map(k -> new int[] { i, j, k }))) 
                                      .distinct().collect(Collectors.toList());
List set1=Arrays.asList(2,3,4,5,10,4,5,6,4);
列表计数=新的ArrayList();
列表组合=set1.stream().flatMap(i->set1.stream()。
flatMap(j->set1.stream().map(k->newint[]{i,j,k})))
.distinct().collect(collector.toList());

我从int[]的组合列表中获取所需元素的逻辑是数组中的数字应该是连续的,差为1

例:{2,3,4}

但是在我最后的列表中,{2,3,4}出现了3次,这是显而易见的。我想筛选3次{2,3,4}到1的出现次数。所以我写了下面的逻辑。仍然有许多元素是连续形式的{4,5,6},我希望它们也只被计数一次

 List<int[]> combination1 = combination.stream().distinct().collect(Collectors.toList());
        combination1.stream().filter(i -> Collections.frequency(combination, i) == 1).forEach(s -> {
            if (s[1] - s[0] == 1 && s[2] - s[1] == 1 && s[2] - s[0] == 2) {
                count.add(1);
            } else {
                count.add(0);
            }
        });
        System.out.println(count.stream().mapToInt(Integer::intValue).sum());
List combination1=composition.stream().distinct().collect(Collectors.toList());
combination1.stream().filter(i->Collections.frequency(组合,i)==1.forEach(s->{
如果(s[1]-s[0]==1&&s[2]-s[1]==1&&s[2]-s[0]==2){
计数。添加(1);
}否则{
计数。添加(0);
}
});
System.out.println(count.stream().mapToInt(Integer::intValue.sum());
我得到的独特元素为15,但它应该是3

我最后的
列表
应该只包含:{2,3,4},{3,4,5},{4,5,6} 因此计数应该是三


以下是
distinct()
文档的第一行:

返回由此流的不同元素(根据
Object.equals(Object)
)组成的流

现在,因为您在
流上运行它
int[].equals
必须在两个int数组具有相同内容时返回
true
。但事实并非如此。您可以在
distinct()
之前使用一个列表,然后使用映射(此处使用
Integer[]
,但是
int[]
需要不同的映射)来解决此问题:

List composition=set1.stream()
.flatMap(i->set1.stream()
.flatMap(j->set1.stream().map(k->array.asList(i,j,k)))
.distinct()
.map(l->l.toArray(新整数[0]))
.collect(Collectors.toList());

尽管@ernest_k提到了解决方案的基本问题,但我认为您可以用简单的方法来解决

首先从
set1
元素创建映射

Map<Integer,Integer> map = set1.stream()
                      .collect(Collectors
                                .toMap(Function.identity(),Function.identity(),(v1,v2)->v1));
Map Map=set1.stream()
.收藏(收藏家)
.toMap(Function.identity(),Function.identity(),(v1,v2)->v1));
{2=2,3=3,4=4,5=5,6=6,10=10}

然后通过循环映射值,可以创建连续数组

List<Integer[]> combination = map.values()
            .stream()
            .filter(item -> map.containsKey(item + 1) && map.containsKey(item + 2))
            .map(item -> new Integer[] {item, item + 1, item + 2})
            .collect(Collectors.toList());
列表组合=map.values()
.stream()
.filter(项目->映射.containsKey(项目+1)和映射.containsKey(项目+2))
.map(项->新整数[]{item,item+1,item+2})
.collect(Collectors.toList());

正如@Holger所评论的,使用集合会更简单

Set<Integer> set = new HashSet<>(set1);
List<Integer[]> combination = set.stream()
            .filter(item -> set.contains(item + 1) && set.contains(item + 2))
            .map(item -> new Integer[] {item, item + 1, item + 2})
            .collect(Collectors.toList());
Set Set=newhashset(set1);
列表组合=set.stream()
.filter(项目->设置包含(项目+1)和设置包含(项目+2))
.map(项->新整数[]{item,item+1,item+2})
.collect(Collectors.toList());
如中所述,
int[]
数组不具有
distinct()
所需的相等性。但是,可以包装
int[]
数组并提供基于值的相等性。因此,您可以使用sequence
map(IntBuffer::wrap).distinct().map(IntBuffer::array)
获得不同的数组,以包装数组,消除重复项并展开数组

然而,这个问题是一个完全不必要的步骤的一部分,因为你没有专注于你想要解决的实际问题

要获得连续值的所有元组,只需使用

List<Integer> set1 = Arrays.asList(2, 3, 4, 5, 10, 4, 5, 6, 4);

int[] ascending = set1.stream().mapToInt(Integer::intValue).sorted().distinct().toArray();

List<int[]> combination = IntStream.rangeClosed(0, ascending.length - 3)
    .mapToObj(ix -> Arrays.copyOfRange(ascending, ix, ix + 3))
    .filter(a -> a[0] + 1 == a[1] && a[1] + 1 == a[2])
    .collect(Collectors.toList());

combination.forEach(a -> System.out.println(Arrays.toString(a)));

我得到了我想要的。谢谢:)还有一个是为了解决OP的实际任务。请注意,这里不需要
映射
,也可以使用
集合
,这简化了整个过程。这是可以做到的。表达式
Arrays.asList(item,item+1,item+2).toArray(Integer[]::new)
是一个洛可可式的解决方案,用于执行简单的
newinteger[]{item,item+1,item+2}
List<Integer> set1 = Arrays.asList(2, 3, 4, 5, 10, 4, 5, 6, 4);

int[] ascending = set1.stream().mapToInt(Integer::intValue).sorted().distinct().toArray();

List<int[]> combination = IntStream.rangeClosed(0, ascending.length - 3)
    .mapToObj(ix -> Arrays.copyOfRange(ascending, ix, ix + 3))
    .filter(a -> a[0] + 1 == a[1] && a[1] + 1 == a[2])
    .collect(Collectors.toList());

combination.forEach(a -> System.out.println(Arrays.toString(a)));