How do I convert java.sql.Array to List<MyCustomEnum>?

ilhan :

I have rs.getArray("lang"); which is java.sql.Array and the lang field is character varying[]. I want to convert it to List<MyEnumLanguage>. As an example I have {fr_FR,en_US} and I have used the following code to convert that my IDE didn't show any errors

List<MyEnumLanguage> myEnumLanguageList = (List<MyEnumLanguage>) rs.getArray("lang");

but it throws an exception org.postgresql.jdbc.PgArray cannot be cast to java.util.List.

My MyEnumLanguage is like:

public enum MyEnumLanguage {
    en_US {
        public String getCode() { return "en_US" }
    de_DE {
        public String getCode() { return "de_DE" }

    private MyEnumLanguage() {
antonis :

You cannot cast an array to List. Your IDE does not show any arror because casting happens on runtime.

Instead, you should use Arrays.asList(array) method, which returns a list containing all the elements of the array. Note that if you want to map the elements of the array to another type, you could easily do that with streams. Example:

List<MyEnumLanguage> myEnumLanguageList = Arrays.asList(rs.getArray("lang"))
                .map(arrayElement -> convertToMyEnumLanguage(arrayElement))

where convertToMyEnumLanguage() takes an element of your array and returns the corresponding MyEnumLanguage.

Take a look at this post:


Initially I missread the question. You have to first convert your PgArray to a normal java array, before you can use it in Arrays.asList().

This can be done using PgArray.getArray() method, and then cast the returned object to an array of the pgArray's containing type.


Improved example:

First of all you should define your enum like this:

public enum MyEnumLanguage {


    private final String code;

    private MyEnumLanguage(String code) {
        this.code = code;

    public String getCode() {
        return code;

    public static MyEnumLanguage getEnumByCode(String code) {
        if(code == null || code.isEmpty()) {
            return null;
        for(MyEnumLanguage e : values()) {
            if(e.getCode().equals(code)) {
                return e;

        return null;

Then to map your pgArray to a List:

Array pgArray = rs.getArray("lang");
String[] langJavaArray = (String[]) pgArray.getArray(); //this returns an Object, so we cast it to a String array

List<MyEnumLanguage> myEnumLanguageList =

Please note that the mapping function does not check for nulls. So if a wrong codes are passed, your list will contain nulls. If this is not the desired result, you have to perform the appropriate checks and handle null cases in your map function.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at


Login to comment


How do i convert a json array object of an object into a java 8 optional list of that object

How can I convert an array to a list without using Arrays.asList() method or Java List Interface

How do I convert an array (i.e. list) column to Vector

How do I convert a Java 8 IntStream to a List?

How do I convert an array of strings into append-able list?

How do I convert a C# List<string[]> to a Javascript array?

How do I convert a Python list into a C array by using ctypes?

How do I convert a java.sql.Date object into a GregorianCalendar?

How do I convert a Java byte array into a Scala byte array?

How do I convert an object array to an object list

How do I convert a string to a list of chars?

How do I convert an array list to hash separated string?

How do I convert user input into a list?

How to convert an observable list to an array list? Java

how do I convert an object to an array list in JS

How do I convert a list/dictionary into a Dataframe?

How do I generate a random derangement of a list/array in java?

How do I convert this to an array?

How do I convert a list of integers into bytes?

How do I convert List<List<Object>> to a multi-dimensional array?

How do i convert a Set into 2D array in java?

How do I convert a named list to an array of objects

How do I convert a long into a char[] array in java?

How do I convert list into dictionary in Python?

How do I convert an array or a list into a hashref?

How do I convert a list into a matrix in KDB?

How I convert string response into Array List

I need to convert a javascript array to a sql list

How i convert array to adjacency list of tree?