Java – How to join Arrays

In this article, we will show you a few ways to join a Java Array.

Apache Commons Lang – ArrayUtils
Java API
Java 8 Stream
1. Apache Commons Lang – ArrayUtils
The simplest way is add the Apache Commons Lang library, and use ArrayUtils. addAll to join arrays. This method supports both primitive and object type arrays.

JoinArray.java
package com.mkyong.example.array;

import org.apache.commons.lang3.ArrayUtils;

import java.util.Arrays;

public class JoinArray {

public static void main(String[] args) {

String[] s1 = new String[]{"a", "b", "c"};
String[] s2 = new String[]{"d", "e", "f"};

String[] result = ArrayUtils.addAll(s1, s2);

System.out.println(Arrays.toString(result));

int [] int1 = new int[]{1,2,3};
int[] int2 = new int[]{4,5,6};

int[] result2 = ArrayUtils.addAll(int1, int2);

System.out.println(Arrays.toString(result2));

}

}

Output

[a, b, c, d, e, f]
[1, 2, 3, 4, 5, 6]
For Maven user.

pom.xml
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>

2. Java API
Pure Java API example, supports both primitive and generic types.

JoinArray.java
package com.mkyong.example.array;

import java.lang.reflect.Array;
import java.util.Arrays;

public class JoinArray {

public static void main(String[] args) {

String[] s1 = new String[]{"a", "b", "c"};
String[] s2 = new String[]{"d", "e", "f"};
String[] s3 = new String[]{"g", "h", "i"};

String[] result = joinArrayGeneric(s1, s2, s3);

System.out.println(Arrays.toString(result));

int[] int1 = new int[]{1, 2, 3};
int[] int2 = new int[]{4, 5, 6};
int[] int3 = new int[]{7, 8, 9};

int[] result2 = joinArray(int1, int2, int3);

System.out.println(Arrays.toString(result2));

}

static <T> T[] joinArrayGeneric(T[]... arrays) {
int length = 0;
for (T[] array : arrays) {
length += array.length;
}

//T[] result = new T[length];
final T[] result = (T[]) Array.newInstance(arrays[0].getClass().getComponentType(), length);

int offset = 0;
for (T[] array : arrays) {
System.arraycopy(array, 0, result, offset, array.length);
offset += array.length;
}

return result;
}

static int[] joinArray(int[]... arrays) {
int length = 0;
for (int[] array : arrays) {
length += array.length;
}

final int[] result = new int[length];

int offset = 0;
for (int[] array : arrays) {
System.arraycopy(array, 0, result, offset, array.length);
offset += array.length;
}

return result;
}

//create other overloaded primitive type - long, double...
//static long[] joinArray(long[]... arrays)
}

Output

[a, b, c, d, e, f, g, h, i]
[1, 2, 3, 4, 5, 6, 7, 8, 9]

3. Java 8 Stream
Java 8 Stream example to join arrays.

JoinArray.java
package com.mkyong.example.array;

import java.util.Arrays;

import java.util.stream.IntStream;
import java.util.stream.Stream;

public class JoinArray {

public static void main(String[] args) {

String[] s1 = new String[]{"a", "b", "c"};
String[] s2 = new String[]{"d", "e", "f"};
String[] s3 = new String[]{"g", "h", "i"};

//join object type array
String[] result = Stream.of(s1, s2, s3).flatMap(Stream::of).toArray(String[]::new);
System.out.println(Arrays.toString(result));

int [] int1 = new int[]{1,2,3};
int[] int2 = new int[]{4,5,6};
int[] int3 = new int[]{7,8,9};

//join 2 primitive type array
int[] result2 = IntStream.concat(Arrays.stream(int1), Arrays.stream(int2)).toArray();

//join 3 primitive type array, any better idea?
int[] result3 = IntStream.concat(Arrays.stream(int1),
IntStream.concat(Arrays.stream(int2), Arrays.stream(int3))).toArray();

System.out.println(Arrays.toString(result2));

System.out.println(Arrays.toString(result3));

}

}
Output

[a, b, c, d, e, f, g, h, i]
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Any better Java 8 Stream example? Do comment below.

from:http://www.mkyong.com/java/java-how-to-join-arrays/

Java – How to join Arrays的更多相关文章

  1. Java 7 Fork/Join 框架

    在 Java7引入的诸多新特性中,Fork/Join 框架无疑是重要的一项.JSR166旨在标准化一个实质上可扩展的框架,以将并行计算的通用工具类组织成一个类似java.util中Collection ...

  2. Java 7 Fork/Join 并行计算框架概览

    应用程序并行计算遇到的问题 当硬件处理能力不能按摩尔定律垂直发展的时候,选择了水平发展.多核处理器已广泛应用,未来处理器的核心数将进一步发布,甚至达到上百上千的数量.而现在 很多的应用程序在运行在多核 ...

  3. Java使用Fork/Join框架来并行执行任务

    现代的计算机已经向多CPU方向发展,即使是普通的PC,甚至现在的智能手机.多核处理器已被广泛应用.在未来,处理器的核心数将会发展的越来越多. 虽然硬件上的多核CPU已经十分成熟,但是很多应用程序并未这 ...

  4. java源码分析:Arrays.sort

    仔细分析java的Arrays.sort(version 1.71, 04/21/06)后发现,java对primitive(int,float等原型数据)数组采用快速排序,对Object对象数组采用 ...

  5. Java中Stringbuffer、Arrays、Interger、Character类的特性

    1:StringBuffer(掌握) (1)用字符串做拼接,比较耗时并且也耗内存,而这种拼接操作又是比较常见的,为了解决这个问题,Java就提供了 一个字符串缓冲区类.StringBuffer供我们使 ...

  6. Java _ JDK _ Arrays, LinkedList, ArrayList, Vector 及Stack

    (最近在看JDK源码,只是拿着它的继承图在看,但很多东西不记录仍然印象不深,所以开始记录JDK阅读系列.) (一)Arrays Arrays比较特殊,直接继承自Arrays ->List(Int ...

  7. Java Concurrency - Fork/Join Framework

    Normally, when you implement a simple, concurrent Java application, you implement some Runnable obje ...

  8. Java并发——Fork/Join框架

    为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/ShiJiaqi. http://www.cnblogs.com/shijiaqi1066/p/4631466. ...

  9. Java线程--interrupt join yield setDaemon常用方法的使用

    概念: 操作系统可以有多个进程,一个线程可以有一个或多个线程.进程与进程之间不共享内存,都在各自的空间中运行.而线程不仅可以共享内存,还可以用有一个自己的内存空间,叫做线程栈. 线程又称轻量级进程.J ...

随机推荐

  1. 变址values(, %edi, 4)和间址4(%edi)

    <汇编语言程序设计>Richard Blum著:5.2.4 在内存和寄存器之间传送数据   使用变址的内存位置: 可以在一个命令中指定把多个值存放到内存中: values:     .in ...

  2. 关于Storm 中Topology的并发度的理解

    来自:https://storm.apache.org/documentation/Understanding-the-parallelism-of-a-Storm-topology.html htt ...

  3. PC版收音机—龙卷风收音机

      龙卷风收音机-龙卷风   文章来源:刘俊涛的博客 欢迎关注,有问题一起学习欢迎留言.评论

  4. js 数组 转为树形结构

    题目: source = [{ id: 1, pid: 0, name: 'body' }, { id: 2, pid: 1, name: 'title' }, { id: 3, pid: 2, na ...

  5. Echarts饼图更改颜色、显示数据且换行

    var option = {   title : {       text: '数据来源',       x:'center'   },   tooltip : {       trigger: 'i ...

  6. vi编辑器的常用命令

    游标控制 h 游标向左移 j 游标向下移 k 游标向上移 l (or spacebar) 游标向右移 w 向前移动一个单词 b 向后移动一个单词 e 向前移动一个单词,且游标指向单词的末尾 ( 移到当 ...

  7. 6、java5线程池之固定大小线程池newFixedThreadPool

    JDK文档说明: 创建一个可重用固定线程数的线程池,以共享的无界队列方式来运行这些线程.在任意点,在大多数 nThreads 线程会处于处理任务的活动状态.如果在所有线程处于活动状态时提交附加任务,则 ...

  8. 转 安装php时报gd相关的错误(gd_ctx.c)

    在安装php时,报如下错误 In file included from /kk/php-5.4.0/ext/gd/gd.c:103: /kk/php-5.4.0/ext/gd/gd_ctx.c: In ...

  9. mysql Substr与char_length函数的应用

    update lee set name = SUBSTR(name, CHAR_LENGTH('lee')+1) where name like "lee%" char_lengt ...

  10. Linux chattr 命令

    不让用户修改.删除文件等,使用 chattr保护 chattr命令的用法:chattr [ -RV ] [ -v version ] [ mode ] files… 最关键的是在[mode]部分,[m ...