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.
This can be done using loop
but I can't use loop. Using java stream
there may be a solution.
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?
Put all the numbers in a Set and just match the size of array and set. If both are equal then all your numbers in array are unique.
Overview. Creating a multidimensional ArrayList often comes up during programming. In many cases, there is a need to create a two-dimensional ArrayList or a three-dimensional ArrayList. In this tutorial, we'll discuss how to create a multidimensional ArrayList in Java.
An ArrayList is just like a 1D Array except it's length is unbounded and you can add as many elements as you need. In order to create a 1D or 2D Array you need to specify the type of object that you are going to be storing in the array when you create it.
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. It will also work for non uniform matrices, since it will just skip the rows that has no nth column.
Unique count of any column will the the size of resulting map of collect method; 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
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.
update
To get the count of rows that start with consecutive list 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. Here we are just mapping the 3rd column (by using index 2) and then applying .distinct()
on the resulting stream.
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
G
You can use the same logic to find count of 'C' in column 3 using the same logic.
list.stream().map(a -> a.get(2)).filter(i -> i.contains("C")).count();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With