Converting Array To List and List To Array in Java

JAVA - Photo by Sahil Pandita on Unsplash

Learning Java is not that easy especially because Java is statically typed language, it requires you to add a type of variable you created. Array is collection of objects, for example String[], Boolean[], or YourOwnObject[].

List  also just a collection of object, but it is wrapped inside ArrayList() class, this class has lot of methods you can use to work with your list, for example dynamic size, adds, removes, sorting, etc. So it is a recommended why to use List over Arrays. But if you somehow have to create Array first than you want to convert that array to List because it  is the right way, you can do something like this example below:

1. Convert String[] to ArrayList<String>

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TestClass {
    public static void main(String[] args) {
        String[] strings = new String[]{"Hello", "World"};
        List stringList = new ArrayList<>(Arrays.asList(strings));
        System.out.println(stringList);
    }
}

2. Convert YourObject[] to ArrayList<YourObject>

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TestClass {
    public static void main(String[] args) {
        MyObject obj1 = new MyObject(1,"Hello");
        MyObject obj2 = new MyObject(2,"World");

        MyObject[] strings = new MyObject[]{obj1, obj2};
        List stringList = new ArrayList<>(Arrays.asList(strings));
        System.out.println(stringList);
    }

    public static class MyObject {
        private Integer id;
        private String label;

        MyObject(Integer id, String label){
            this.id=id;
            this.label=label;
        }

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getLabel() {
            return label;
        }

        public void setLabel(String label) {
            this.label = label;
        }
    }
}

3. Convert ArrayList<YourObject> to YourObject[]

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TestClass {
    public static void main(String[] args) {

        List stringList = new ArrayList(){{
            add(new MyObject(1,"hello"));
            add(new MyObject(2,"world"));
        }};

        MyObject[] myObjects = stringList.toArray(new MyObject[]{});
        for (MyObject obj : myObjects){
            System.out.println(obj.getId() + " => " + obj.getLabel());
        }
    }

    public static class MyObject {
        private Integer id;
        private String label;

        MyObject(Integer id, String label){
            this.id=id;
            this.label=label;
        }

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getLabel() {
            return label;
        }

        public void setLabel(String label) {
            this.label = label;
        }
    }
}

Yes working with Java the language is more complicated than most dynamic language like Python or Javascript. But again dynamic language can turn your codes to become un-maintainable in the future. Also lack of supported tools, like Integrated Development Environment (IDE), IDE can hardly works on dynamic languages.

Popular posts from this blog

ERROR 1348 Column Password Is Not Updatable When Updating MySQL Root Password

How To Create Spring Boot Project Using Netbeans

Spring Kafka - how to use ReplyingKafkaTemplate send and reply synchronously

Upload and Download Rest API using Spring Boot Reactive WebFlux

What Is My Localhost IP, For Windows User?