在Java中如何加入数组? 3 种方式:Apache Commons ArrayUtils、Java 8 Streams 和简单 API
已发表: 2022-01-20
在本教程中,我们将介绍加入 Java 数组的不同方式。
如果您有以下任何问题,那么您来对地方了:
- 如何在 Java 中连接两个数组?
- 如何在 Java 中合并两个数组?
- 在 Java 中组合数组的 3 种方法
- 在 Java 中使用分隔符加入基元数组
以下是在 Java 中加入数组的三种方法。
- 使用 Apache Common 实用程序加入数组
- 使用 Java8 Stream 操作加入数组
- 使用标准 Java API 加入数组
让我们开始吧:
创建 java 类CrunchifyJoinArrays3Ways.java
。
将以下 Java 代码放入文件中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
package crunchify . com . tutorial ; import org . apache . commons . lang3 . ArrayUtils ; import java . lang . reflect . Array ; import java . util . Arrays ; import java . util . stream . IntStream ; import java . util . stream . Stream ; /** * @author Crunchify.com * <p> * In Java how to join Arrays? 3 ways: Apache Commons ArrayUtils, Java 8 Streams and Simple APIs. * Version: 1.0.3 */ public class CrunchifyJoinArrays3Ways { public static void main ( String [ ] args ) { crunchifyPrint ( "Original String Array1: [google, twitter]" ) ; crunchifyPrint ( "Original String Array2: [apple, microsoft]" ) ; crunchifyPrint ( "Original Integer Array1: [111, 444]" ) ; crunchifyPrint ( "Original Integer Array2: [222, 555]\n" ) ; // Join Array using Apache Common utility joinArrayUsingApacheCommon ( ) ; // Join Array using Java8 Stream operation joinArrayUsingJava8Stream ( ) ; // Join Array using Standard Java APIs joinArrayUsingJavaAPI ( ) ; } // Method-1: Join Array using Apache Common utility private static void joinArrayUsingApacheCommon ( ) { String [ ] company1 = new String [ ] { "google" , "twitter" } ; String [ ] company2 = new String [ ] { "apple" , "microsoft" } ; // from org.apache.commons.lang3 maven dependency String [ ] crunchifyResult = ArrayUtils . addAll ( company1 , company2 ) ; crunchifyPrint ( "From Method-1: addAll() - String ==> " + Arrays . toString ( crunchifyResult ) ) ; int [ ] crunchifyArray1 = new int [ ] { 111 , 444 } ; int [ ] crunchifyArray2 = new int [ ] { 222 , 555 } ; int [ ] crunchifyResult2 = ArrayUtils . addAll ( crunchifyArray1 , crunchifyArray2 ) ; crunchifyPrint ( "From Method-1: addAll() - Integer ==> " + Arrays . toString ( crunchifyResult2 ) ) ; } // Simple Java Print Method private static void crunchifyPrint ( String result ) { System . out . println ( result ) ; } // Method-2: Join Array using Java8 Stream operation private static void joinArrayUsingJava8Stream ( ) { String [ ] company1 = new String [ ] { "google" , "twitter" } ; String [ ] company2 = new String [ ] { "apple" , "microsoft" } ; // Stream.of() - returns a sequential ordered stream whose elements are the specified values. // A sequence of elements supporting sequential and parallel aggregate operations. The following example illustrates an aggregate operation using Stream and IntStream: // // int sum = widgets.stream() // .filter(w -> w.getColor() == RED) // .mapToInt(w -> w.getWeight()) // .sum(); String [ ] result = Stream . of ( company1 , company2 ) . flatMap ( Stream : : of ) . toArray ( String [ ] : : new ) ; crunchifyPrint ( "\nFrom Method-2: Stream.of() ==> " + Arrays . toString ( result ) ) ; int [ ] crunchifyArray1 = new int [ ] { 111 , 444 } ; int [ ] crunchifyArray2 = new int [ ] { 222 , 555 } ; // Arrays.stream() - returns a sequential IntStream with the specified array as its source. int [ ] crunchifyResult2 = IntStream . concat ( Arrays . stream ( crunchifyArray1 ) , Arrays . stream ( crunchifyArray2 ) ) . toArray ( ) ; crunchifyPrint ( "From Method-2: IntStream.concat() ==> " + Arrays . toString ( crunchifyResult2 ) ) ; } // Method-3: Join Array using Standard Java APIs private static void joinArrayUsingJavaAPI ( ) { String [ ] company1 = new String [ ] { "google" , "twitter" } ; String [ ] company2 = new String [ ] { "apple" , "microsoft" } ; String [ ] crunchifyResult = crunchifyJoinGenericArray ( company1 , company2 ) ; crunchifyPrint ( "\nFrom Method-3: crunchifyJoinArrayusingGeneric() ==> " + Arrays . toString ( crunchifyResult ) ) ; int [ ] crunchifyArray1 = new int [ ] { 111 , 444 } ; int [ ] crunchifyArray2 = new int [ ] { 222 , 555 } ; int [ ] crunchifyResult2 = crunchifyJoinIntegerArray ( crunchifyArray1 , crunchifyArray2 ) ; // Arrays.toString() Returns a string representation of the contents of the specified array. // The string representation consists of a list of the array's elements, enclosed in square brackets ("[]"). // Adjacent elements are separated by the characters ", " (a comma followed by a space). // Elements are converted to strings as by String.valueOf(int). Returns "null" if a is null. crunchifyPrint ( "From Method-3: joinArray() ==> " + Arrays . toString ( crunchifyResult2 ) ) ; } @ SafeVarargs private static < T > T [ ] crunchifyJoinGenericArray ( T [ ] . . . crunchifyArrays ) { int crunchify = 0 ; for ( T [ ] crunchifyArray : crunchifyArrays ) { crunchify += crunchifyArray . length ; } //T[] result = new T[crunchify]; final T [ ] crunchifyResult = ( T [ ] ) Array . newInstance ( crunchifyArrays [ 0 ] . getClass ( ) . getComponentType ( ) , crunchify ) ; int crunchifyOffset = 0 ; for ( T [ ] crunchifyArray : crunchifyArrays ) { // Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array. // A subsequence of array components are copied from the source array referenced by src to the destination array referenced by dest. // The number of components copied is equal to the length argument. // The components at positions srcPos through srcPos+length-1 in the source array are copied into positions destPos through destPos+length-1, respectively, of the destination array. System . arraycopy ( crunchifyArray , 0 , crunchifyResult , crunchifyOffset , crunchifyArray . length ) ; crunchifyOffset += crunchifyArray . length ; } return crunchifyResult ; } private static int [ ] crunchifyJoinIntegerArray ( int [ ] . . . crunchifyArrays ) { int crunchify = 0 ; for ( int [ ] crunchifyArray : crunchifyArrays ) { crunchify += crunchifyArray . length ; } final int [ ] crunchifyResult = new int [ crunchify ] ; int crunchifyOffset = 0 ; for ( int [ ] crunchifyArray : crunchifyArrays ) { System . arraycopy ( crunchifyArray , 0 , crunchifyResult , crunchifyOffset , crunchifyArray . length ) ; crunchifyOffset += crunchifyArray . length ; } return crunchifyResult ; } } |
让我们看一下Java API 的一些细节。

java.lang.System.arraycopy():

java.util.Arrays.toString():

java.util.stream():


只需将程序作为 Java 程序运行,您应该会看到与以下相同的结果。
IntelliJ IDEA 控制台输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
/ Library / Java / JavaVirtualMachines / jdk - 15.jdk / Contents / Home / bin / java - javaagent : / Applications / IntelliJ IDEA . app / Contents / lib / idea_rt . jar = 50249 : / Applications / IntelliJ IDEA . app / Contents / bin - Dfile . encoding = UTF - 8 - classpath / Users / app / crunchify / github / CrunchifyTutorials / target / classes : / Users / app / crunchify / github / CrunchifyTutorials / WebContent / WEB - INF / lib / zxing - 2.1.jar / Users / app / . m2 / repository / commons - collections / commons - collections / 3.2.1 / commons - collections - 3.2.1.jar / spring - context - support - 5.1.3.RELEASE.jar crunchify . com . tutorial . CrunchifyJoinArrays3Ways Original String Array1 : [ google , twitter ] Original String Array2 : [ apple , microsoft ] Original Integer Array1 : [ 111 , 444 ] Original Integer Array2 : [ 222 , 555 ] From Method - 1 : addAll ( ) - String == > [ google , twitter , apple , microsoft ] From Method - 1 : addAll ( ) - Integer == > [ 111 , 444 , 222 , 555 ] From Method - 2 : Stream . of ( ) == > [ google , twitter , apple , microsoft ] From Method - 2 : IntStream . concat ( ) == > [ 111 , 444 , 222 , 555 ] From Method - 3 : crunchifyJoinArrayusingGeneric ( ) == > [ google , twitter , apple , microsoft ] From Method - 3 : joinArray ( ) == > [ 111 , 444 , 222 , 555 ] Process finished with exit code 0 |
如果您有任何其他方式来合并 Java 数组或遇到任何异常,请告诉我。