简体   繁体   English

如何在java中找到2D ArrayList列的唯一值?

[英]How to find unique value of a column of a 2D ArrayList in java?

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

public class Delete
{
    public static void main(String[] args)
    {
         List<List<String>> list = new ArrayList<>();
         list.add(List.of("A","B","C","R"));
         list.add(List.of("E","F","G","F"));
         list.add(List.of("A","B","C","D"));
         System.out.println(list.stream().distinct().count());
         Map<String, Long> countMapOfColumn = list.stream()
                                                  .filter(innerList -> innerList.size() >= 3)
                                                  .map(innerList -> innerList.get(3 - 1))
                         .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
        System.out.println(countMapOfColumn.keySet());
    }
}

I want to find out the unique element of column 3 which are "C","G" and there are 2 unique element in column 3. 我想找出第3列中"C","G"唯一元素,第3列中有2唯一元素。

This can be done using loop but I can't use loop. 这可以使用loop完成,但我不能使用循环。 Using java stream there may be a solution. 使用java stream可能有一个解决方案。

In addition, how can I get count of rows having "A","B" in column 1,2 at a time and in general for N columns? 另外,如何在第1,2列中一次获得具有"A","B"的行数,一般来说是N列?

For your given datastructure; 对于您给定的数据结构;

Map<String, Long> countMapOfColumn = list.stream()
        .filter(innerList -> innerList.size() >= column)
        .map(innerList -> innerList.get(column - 1))
        .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

where column = 3 , will get you a map with {"C", "G"} as keyset, and values will give you the count of each character in the given column. 其中column = 3 ,将为您提供一个带有{"C", "G"}的映射作为键集,值将为您提供给定列中每个字符的计数。 It will also work for non uniform matrices, since it will just skip the rows that has no nth column. 它也适用于非均匀矩阵,因为它只会跳过没有第n列的行。

Unique count of any column will the the size of resulting map of collect method; 任何列的唯一计数将是collect方法的结果映射的大小; countMapOfColumn.size()

To get individual char counts, use the resulting map with keys as input, for example to get count of C in column = 3 , use countMapOfColumn.get("C") , which will return 2 要获取单个字符计数,请使用带有键的结果映射作为输入,例如,要获取column = 3C计数,请使用countMapOfColumn.get("C") ,它将返回2

But I'd rather use a Guava's Table , you can directly select any given row or column without any hassle, then it is only, getting nth column, and filter our duplicates. 但是我宁愿使用Guava的Table ,你可以直接选择任何给定的行或列,没有任何麻烦,然后它只是,获得第n列,并过滤我们的重复。

update 更新

To get the count of rows that start with consecutive list list = {"A", "B"} ; 获取以连续列表开头的行数list = {"A", "B"} ;

List<String> startingList = Arrays.asList("A", "B");
Long count = list.stream()
        .filter(innerList -> innerList.size() >= startingList.size() 
                && innerList.subList(0, startingList.size()).equals(startingList))
        .count();

Your question doesn't compile. 你的问题没有编译。 Assuming you have defined the list as follows, you can easily find the distinct value in column 3 by using the following. 假设您已按如下方式定义列表,则可以使用以下内容轻松找到第3列中的不同值。 Here we are just mapping the 3rd column (by using index 2) and then applying .distinct() on the resulting stream. 这里我们只是映射第3列(使用索引2),然后在结果流上应用.distinct()

List<List<String>> list = Arrays.asList(Arrays.asList("A", "B", "C"), 
                                        Arrays.asList("E", "F", "G"),
                                        Arrays.asList("A", "B", "C"));

List<String> distinct = list.stream().map(a -> a.get(2))
                            .distinct().collect(Collectors.toList());

When you print out the elements it will output : 当您打印出它将输出的元素时:

C C

G G

You can use the same logic to find count of 'C' in column 3 using the same logic. 您可以使用相同的逻辑使用相同的逻辑在第3列中查找“C”的计数。

list.stream().map(a -> a.get(2)).filter(i -> i.contains("C")).count();

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM