Returns a sequential ordered stream whose elements are the specified values.
flatMap(Function super T,? extends Stream extends R%gt;> mapper)
Returns a stream of objects after applying the mapping function on each element and then flattens the result.
toArray()
Returns an array containing the elements of this stream.
插图:
Input : a[] = {1, 2, 3}
b[] = {4, 5, 6}
Output : {1, 2, 3, 4, 5, 6}
输出说明: Stream.of(a, b) 获取数组并将它们通过管道传输到单个流中。然后 flatMap() 方法在对 Stream.of() 的每个元素应用映射函数后返回一个对象流,然后将结果展平。最后, toArray() 将流元素转换为数组并返回形成的数组。
例子:
Java
// Java program to merge two arrays of
// same type into an Object array.
import java.util.stream.Stream;
import java.util.Arrays;
import java.io.*;
class GFG {
public static Object[] concatenate(T[] a, T[] b)
{
// Function to merge two arrays of
// same type
return Stream.of(a, b)
.flatMap(Stream::of)
.toArray();
// Arrays::stream can also be used in place
// of Stream::of in the flatMap() above.
}
public static void main (String[] args)
{
Integer[] a = new Integer[]{1,2,3};
Integer[] b = new Integer[]{4,5,6};
Object[] c = concatenate(a,b);
System.out.println("Merged object array : "
+ Arrays.toString(c));
}
}
Java
// Java program to merge two arrays of
// same type into an Object array.
import java.util.stream.Stream;
import java.util.Arrays;
import java.io.*;
class GFG {
public static Object[] concatenate(T[] a, T[] b)
{
// Function to merge two arrays of
// same type
return Stream.concat(Arrays.stream(a),
Arrays.stream(b))
.toArray();
}
public static void main (String[] args)
{
Integer[] a = new Integer[]{1,2,3};
Integer[] b = new Integer[]{4,5,6};
Object[] c = concatenate(a,b);
System.out.println("Merged object array : "
+ Arrays.toString(c));
}
}
Java
// Java program to merge two arrays of
// same type into an Object array.
import java.util.stream.Stream;
import java.util.Arrays;
import java.io.*;
class GFG {
// Function to merge two arrays of same type
public static Object[] concatenate(T[] a, T[] b)
{
// Create an empty Object array of the combined
// size of the array a and array b
Object[] n=new Object[a.length + b.length];
// Copy the array a into n
System.arraycopy(a, 0, n, 0, a.length);
// Copy the array b into n
System.arraycopy(b, 0, n, a.length, b.length);
return n;
}
public static void main (String[] args)
{
Integer[] a = new Integer[]{1,2,3};
Integer[] b = new Integer[]{4,5,6};
Object[] c = concatenate(a,b);
System.out.println("Merged object array : "
+ Arrays.toString(c));
}
}
Java
// Java program to merge two arrays of
// same type into an Object array.
import java.util.stream.*;
import java.util.Arrays;
import java.io.*;
class GFG {
// Function to merge two arrays of same type
public static Object[] concatenate(T[] a, T[] b)
{
// Create an empty List of type Object
List
Java
// Java program to merge two arrays of
// same type into an Object array.
import java.util.*;
import java.io.*;
class GFG {
// Function to merge two arrays of same type
public static List
// Java program to merge two arrays of
// same type into an Object array.
import java.util.stream.Stream;
import java.util.Arrays;
import java.io.*;
class GFG {
public static Object[] concatenate(T[] a, T[] b)
{
// Function to merge two arrays of
// same type
return Stream.concat(Arrays.stream(a),
Arrays.stream(b))
.toArray();
}
public static void main (String[] args)
{
Integer[] a = new Integer[]{1,2,3};
Integer[] b = new Integer[]{4,5,6};
Object[] c = concatenate(a,b);
System.out.println("Merged object array : "
+ Arrays.toString(c));
}
}
输出 :
Merged object array : [1, 2, 3, 4, 5, 6]
方法三:使用System类的arraycopy()方法
System 类的 arraycopy() 方法已存在于Java.lang 包中,将源数组从特定开始位置复制到目标数组。要复制的参数数量由 len 参数决定。
public static void arraycopy(Object source_arr, int sourcePos,
Object dest_arr, int destPos, int len)
插图:
Input : a[] = {1, 2, 3}
b[] = {4, 5, 6}
Output : {1, 2, 3, 4, 5, 6}
例子:
Java
// Java program to merge two arrays of
// same type into an Object array.
import java.util.stream.Stream;
import java.util.Arrays;
import java.io.*;
class GFG {
// Function to merge two arrays of same type
public static Object[] concatenate(T[] a, T[] b)
{
// Create an empty Object array of the combined
// size of the array a and array b
Object[] n=new Object[a.length + b.length];
// Copy the array a into n
System.arraycopy(a, 0, n, 0, a.length);
// Copy the array b into n
System.arraycopy(b, 0, n, a.length, b.length);
return n;
}
public static void main (String[] args)
{
Integer[] a = new Integer[]{1,2,3};
Integer[] b = new Integer[]{4,5,6};
Object[] c = concatenate(a,b);
System.out.println("Merged object array : "
+ Arrays.toString(c));
}
}
// Java program to merge two arrays of
// same type into an Object array.
import java.util.stream.*;
import java.util.Arrays;
import java.io.*;
class GFG {
// Function to merge two arrays of same type
public static Object[] concatenate(T[] a, T[] b)
{
// Create an empty List of type Object
List n = new ArrayList<>();
// Add arrays to list
Stream.of(a, b)
.flatMap(Stream::of)
.forEach(n::add);
// Convert list to array and return
return n.toArray();
}
public static void main (String[] args)
{
Integer[] a = new Integer[]{1,2,3};
Integer[] b = new Integer[]{4,5,6};
Object[] c = concatenate(a,b);
System.out.println("Merged object array : "
+ Arrays.toString(c));
}
}
输出:
Merged object array : [1, 2, 3, 4, 5, 6]
方法 5:使用 Collections.addAll() 为Java 7 使用Java集合
插图:
Input : a[] = {1, 2, 3}
b[] = {4, 5, 6}
Output : {1, 2, 3, 4, 5, 6}
例子:
Java
// Java program to merge two arrays of
// same type into an Object array.
import java.util.*;
import java.io.*;
class GFG {
// Function to merge two arrays of same type
public static List concatenate(T[] a, T[] b)
{
// Create an empty List of type Object
List n = new ArrayList<>();
// Add the array a into n
Collections.addAll(n, a);
// Add the array b into n
Collections.addAll(n, b);
return n;
}
public static void main (String[] args)
{
Integer[] a = new Integer[]{1,2,3};
Integer[] b = new Integer[]{4,5,6};
List c = concatenate(a,b);
System.out.println("Merged object array : "
+ c);
}
}