📜  在Java中将数组合并成一个新的对象数组

📅  最后修改于: 2022-05-13 01:55:25.480000             🧑  作者: Mango

在Java中将数组合并成一个新的对象数组

给定两个相同类型的数组,需要将它们合并成一个新的对象数组。任务是将两个相同类型的数组合并为一个对象数组,使得数组元素在新合并的数组中保持其原始顺序,并且第一个数组的元素在合并的对象数组中位于第二个数组的元素之前。

这种合并可以在Java中的许多方法中完成,例如 Java8、System.arraycopy() 和Java集合。

将数组合并为新对象的不同方法

  1. 在Java 8 中使用 Stream API 并使用 Stream.of()、flatMap() 和 toArray() 方法
  2. 使用 concat() 方法 Stream 类
  3. 使用 System 类的 arraycopyOf() 方法
  4. 为Java 8 及更高版本使用Java集合
  5. 为Java 7 使用Java集合

让我们详细讨论这些方法,如下所示:

方法 1:使用 Java8 中的 Stream API 并使用 Stream.of()、flatMap() 和 toArray() 方法

Stream的类层次结构如下:

java.lang.Object
  ↳  java.util.stream
MethodAction Performed
of(T… values)Returns a sequential ordered stream whose elements are the specified values.
flatMap(Function 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 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));
    }
}

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);
    }
}

输出:

Merged object array : [1, 2, 3, 4, 5, 6]

方法二:使用 Stream.concat()、Arrays.stream() 和 toArray() 方法

MethodAction Performed
concat(Stream a, Stream b)Creates a lazily concatenated stream whose elements are all the elements of the first stream followed by all the elements of the second stream.

插图:

Input : a[] = {1, 2, 3}
        b[] = {4, 5, 6}
Output : {1, 2, 3, 4, 5, 6}

输出说明: Stream.concat() 创建一个合并流,其中的元素按照它们在参数中的顺序排列。这里 Stream.concat() 创建了一个连接流,其元素是从数组“a”转换而来的流的所有元素,然后是从数组“b”转换而来的流的所有元素。然后将连接的流转换为数组并返回。

例子:

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));
    }
}

输出 :

Merged object array : [1, 2, 3, 4, 5, 6]

方法三:使用System类的arraycopy()方法

System 类的 arraycopy() 方法已存在于Java.lang 包中,将源数组从特定开始位置复制到目标数组。要复制的参数数量由 len 参数决定。

source_Position 到 source_Position + length – 1 的组件从destination_Position 到destination_Position + length – 1 复制到目标数组。

语法:类声明

public final class System extends Object

语法:方法声明

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));
    }
}

输出:

Merged object array : [1, 2, 3, 4, 5, 6]

方法 4:在Java 8 中使用Java集合

集合是一组表示为单个单元的单个对象。 Java提供了一个集合框架,它定义了几个类和接口,以将一组对象表示为一个单元。

插图:为Java 8 流使用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.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);
    }
}

输出:

Merged object array : [1, 2, 3, 4, 5, 6]