[Java] From two Lists to one array list

Introduction

I verified it because I need to do the title to make a stacked bar graph. At first, I implemented it with a fixed class, but when I tried to use it for general purposes, I stumbled, so don't forget it.

environment

java8

Source

From a list of two Integers to one list of Integer arrays The two lists are assumed to be the same size.

Integer version


private List<Integer[]> convertTwoListToArray1(List<Integer> list1, List<Integer> list2) {
    List<Integer[]> list = new ArrayList<>();
    for (int i = 0; i < list1.size(); i++) {
        list.add(new Integer[] { list1.get(i), list2.get(i) });
    }
    return list;
}

Click here for the generic version that I tried to generalize

Generics version


@SuppressWarnings("unchecked")
private <T> List<T[]> convertTwoListToArray2(List<T> list1, List<T> list2) {
    List<T[]> list = new ArrayList<>();
    for (int i = 0; i < list1.size(); i++) {
        Object[] tmpArray = new Object[] { list1.get(i), list2.get(i) };
        list.add((T[]) tmpArray);
    }
    return list;
}

Finally

I stumbled because I couldn't create an array of generics, but I solved it by casting an array of Objects.

that's all

Recommended Posts

[Java] From two Lists to one array list
[Java] Conversion from array to List
Changes from Java 8 to Java 11
Sum from Java_1 to 100
About Java Array List
From Java to Ruby !!
[Java] List type / Array type conversion
Migration from Cobol to JAVA
New features from Java7 to Java8
Connect from Java to PostgreSQL
[Java] Convert ArrayList to array
From Ineffective Java to Effective Java
How to initialize Java array
Get to the abbreviations from 5 examples of iterating Java lists
For Java beginners: List, Map, Iterator / Array ... How to convert?
[Java] Convert 1-to-N List to Map
Java to be involved from today
From Java to VB.NET-Writing Contrast Memo-
Java, interface to start from beginner
[Java] How to use List [ArrayList]
The road from JavaScript to Java
How to make a Java array
Java array / list / stream mutual conversion list
[Java] Convert array to ArrayList * Caution
[Android, Java] Method to find the elapsed date from two dates
[Java] Shallow copy and deep copy when converting an array to List
Cast an array of Strings to a List of Integers in Java
Effective Java Item 25 Select a list from an array First half
[Java] array
Java array
Java array
java (array)
[Java] How to add data to List (add, addAll)
Java array
[Java] Array
Convert from java UTC time to JST time
Connect from Java to MySQL using Eclipse
Change List <Optional <T >> to Optional <List <T >> in Java
Java array
From installing Eclipse to executing Java (PHP)
Post to Slack from Play Framework 2.8 (Java)
Java: How to send values from Servlet to Servlet
[Java] Flow from source code to execution
Introduction to monitoring from Java Touching Prometheus
Precautions when migrating from VB6.0 to JAVA
[Java] Generate a narrowed list from multiple lists using the Stream API
Memo for migration from java to kotlin
java array
[Java] Array
Type conversion from java BigDecimal type to String type
[Java] How to convert one element of a String type array to an Int type
[Java] Program example to get the maximum and minimum values from an array
Upsert from Java SDK to Azure Cosmos DB
Two ways to start a thread in Java + @
Run R from Java I want to run rJava
Connect to Aurora (MySQL) from a Java application
To become a VB.net programmer from a Java shop
Migrate from Java to Server Side Kotlin + Spring-boot
How to get Class from Element in Java
Create Scala Seq from Java, make Scala Seq a Java List
[Java] Get multiple values from one return value