在Java平嵌套集合
流是一系列支持各种方法的对象,这些方法可以通过流水线来产生所需的结果。 Stream 用于按照流水线方法计算元素,而不改变对象的原始值。而且,扁平化意味着将两个或多个集合合并为一个。考虑下图,我们有一个包含 3 个数组的数组,但在展平效果之后,我们将有一个结果数组,其中包含三个数组中的所有元素。
插图:
Input : arr1[] = {{1,2,3,4},{5,6,7},{8,9}};
Processing : Flatening
Output : arr1[] = {1,2,3,4,5,6,7,8,9};
Stream flatMap()方法用于将集合流扁平化为对象流。这些对象是从原始流中的所有集合组合而成的。 flatMap()方法是对流元素的一对多转换,然后将结果元素展平为新的流。基本上Stream.flatMap()方法有助于将Stream
示例 1:使用 flatMap() 方法展平具有相同类型的两个数组的流
Java
// Java Program to flatten a stream of same type two arrays
// using flatMap() method
// Importing input output classes
import java.io.*;
// Importing Arrays and Stream classes
// from java.util package
import java.util.Arrays;
import java.util.stream.Stream;
// Main class
class GFG {
// Method 1
// To flatten a stream of two arrays of the same type
public static Stream flatten(T[] a, T[] b)
{
// Stream.flatMap() method coverts
// Stream> to the Stream
Stream stream
= Stream.of(a, b).flatMap(Arrays::stream);
// Returns the desired stream
return stream;
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Input array of strings
// Array 1 has uppercase characters
String[] a = { "A", "B", "C" };
// Array 2 has lowercase characters
String[] b = { "i", "J", "K" };
// Calling the above method in the main() method
String[] s = flatten(a, b).toArray(String[] ::new);
// Return string representation of contents
// of integer array
System.out.println(Arrays.toString(s));
}
}
Java
// Java Program to Flatten a stream of Two Lists
// of the same type
// Importing required libraries
import java.io.*;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
// Main class
public class GFG {
// Method 1
// Flatten a stream of two lists of the same type
public static Stream flatten(List a,
List b)
{
// Stream.flatMap() method coverts
// Stream> to the Stream
// using flatMap(x -> x.stream())
Stream stream
= Stream.of(a, b).flatMap(List::stream);
// Return the desired stream
return stream;
}
// Method 2
// Main drier method
public static void main(String[] args)
{
// Input Lists
// List 1
List a = Arrays.asList("Ma", "Rs", "Xy");
// List 2
List b = Arrays.asList("Jw", "Pi", "Br");
// Calling the method 1 and storing it in a single
// list
List s
= flatten(a, b).collect(Collectors.toList());
// Print all the elements in above List object
System.out.println(s);
}
}
Java
// Java Program to Flatten a map containing a list of items
// as values using flatMap() method
// Importing input output classes
import java.io.*;
// Importing desired classes from java.util package
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
// Main class
public class GFG {
// Method 1
// To flatten a map containing a list of items as values
public static Stream
flatten(Collection > values)
{
// Stream.flatMap() method coverts
// Stream> to the Stream
Stream stream
= values.stream().flatMap(x -> x.stream());
// Return the desired stream
return stream;
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Creating an object of Map class
// Declaring object of integer and string type
Map > map = new HashMap<>();
// Adding elements to the above Map object
// Custom input entries
map.put(1, Arrays.asList("1", "2", "3"));
map.put(2, Arrays.asList("4", "5", "6"));
// Creating a List class object holding all elements
// after flatenning
List s = flatten(map.values())
.collect(Collectors.toList());
// Print and display the above List object
System.out.println(s);
}
}
输出
[A, B, C, i, J, K]
示例 2:展平两个相同类型列表的流
Java
// Java Program to Flatten a stream of Two Lists
// of the same type
// Importing required libraries
import java.io.*;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
// Main class
public class GFG {
// Method 1
// Flatten a stream of two lists of the same type
public static Stream flatten(List a,
List b)
{
// Stream.flatMap() method coverts
// Stream> to the Stream
// using flatMap(x -> x.stream())
Stream stream
= Stream.of(a, b).flatMap(List::stream);
// Return the desired stream
return stream;
}
// Method 2
// Main drier method
public static void main(String[] args)
{
// Input Lists
// List 1
List a = Arrays.asList("Ma", "Rs", "Xy");
// List 2
List b = Arrays.asList("Jw", "Pi", "Br");
// Calling the method 1 and storing it in a single
// list
List s
= flatten(a, b).collect(Collectors.toList());
// Print all the elements in above List object
System.out.println(s);
}
}
输出
[Ma, Rs, Xy, Jw, Pi, Br]
示例 3:使用 flatMap() 方法将包含项目列表作为值的地图展平
Java
// Java Program to Flatten a map containing a list of items
// as values using flatMap() method
// Importing input output classes
import java.io.*;
// Importing desired classes from java.util package
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
// Main class
public class GFG {
// Method 1
// To flatten a map containing a list of items as values
public static Stream
flatten(Collection > values)
{
// Stream.flatMap() method coverts
// Stream> to the Stream
Stream stream
= values.stream().flatMap(x -> x.stream());
// Return the desired stream
return stream;
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Creating an object of Map class
// Declaring object of integer and string type
Map > map = new HashMap<>();
// Adding elements to the above Map object
// Custom input entries
map.put(1, Arrays.asList("1", "2", "3"));
map.put(2, Arrays.asList("4", "5", "6"));
// Creating a List class object holding all elements
// after flatenning
List s = flatten(map.values())
.collect(Collectors.toList());
// Print and display the above List object
System.out.println(s);
}
}
输出
[1, 2, 3, 4, 5, 6]