Java 把数组合并成一个新的对象数组

Java 把数组合并成一个新的对象数组

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

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

将数组合并成一个新对象的不同方法

  1. 使用Java 8中的Stream API,使用Stream.of()、flatMap()和toArray()方法
  2. 使用concat()方法流类
  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
方式 执行的动作
of(T… values) 返回一个连续的有序流,其元素是指定的值。
flatMap(Function<? super T,? extends Stream<? extends R%gt;> mapper) 在对每个元素应用映射函数后返回一个对象流,然后对结果进行平移。
toArray() 返回一个包含该流元素的数组。

示例

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

输出

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

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

方法一 执行的操作
concat(Stream<? extends T> a, Stream<? extends T> b) 创建一个懒惰地连接的流,其元素是第一个流的所有元素,然后是第二个流的所有元素。

示例

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

Output : {1, 2, 3, 4, 5, 6}

输出解析: Stream.concat()创建一个合并的流,其中的元素按照参数中的顺序排列。这里Stream.concat()创建了一个合并的流,其元素是由数组’a’转换而来的所有流的元素,然后是由数组’b’转换而来的所有流的元素。然后,串联的流被转换为数组并返回。

例子

// 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 <T> 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]

方法3: 使用系统类的arraycopy()方法

在java.lang包中,System类的arraycopy()方法将一个源数组从一个特定的开始位置复制到目标数组的指定位置。被复制的参数数量由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 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 <T> 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]

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

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

示例: 在Java 8流中使用Java集合

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

Output : {1, 2, 3, 4, 5, 6}

例子

// 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 <T> Object[] concatenate(T[] a, T[] b)
    {
         
        // Create an empty List of type Object
        List<Object> 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: 使用Java 7的集合使用Collections.addAll()

示例

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

Output : {1, 2, 3, 4, 5, 6}

例子

// 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 <T> List<Object> concatenate(T[] a, T[] b)
    {
        // Create an empty List of type Object
        List<Object> 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<Object> c = concatenate(a,b);
     
        System.out.println("Merged object array : "
                        + c);
    }
}

输出

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程