Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloading constructor with a generic type in java

Here is the code:

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

/**
 * Created by IDEA on 15/11/14.
 */
public class Matrix<T> {
    private final int nrow;
    private final int ncol;

    Matrix(List<List<T>> data) {
        nrow = data.size();
        ncol = data.get(0).size();
    }

    Matrix(List<T[]> data) {
        nrow = data.size();
        ncol = data.get(0).length;
    }
}

The IDE asks me to delete the first constructor, why?

like image 663
qed Avatar asked Dec 01 '22 15:12

qed


1 Answers

The IDE asks me to rename or delete the first constructor, why?

It's because of the way generics work in Java. Due to type erasure, you're effectively declaring two constructors like this:

Matrix(List data)

That's what the JVM sees, with some added metadata about the type arguments involved.

This isn't just for constructors - you can't overload methods based on generic type arguments, either.

See the Java Generics FAQ entry on type erasure for the gruesome details.

It's odd that your constructor only needs the row and column count, but here's a version using static factory methods:

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

public class Matrix<T> {
    private final int nrow;
    private final int ncol;

    private Matrix(int nrow, int ncol) {
        this.nrow = nrow;
        this.ncol = ncol;
    }

    public static <T> Matrix<T> fromArrays(List<T[]> data) {
        return new Matrix<T>(data.size(), data.get(0).length);
    }

    public static <T> Matrix<T> fromLists(List<? extends List<T>> data) {
        return new Matrix<T>(data.size(), data.get(0).size());
    }
}

And a demo of using this:

public class Test {
    public static void main(String[] args) {
        // We're only checking the compilation for the moment :)
        List<List<String>> lists = null;
        List<String[]> arrays = null;

        Matrix<String> x = Matrix.fromArrays(arrays);
        Matrix<String> y = Matrix.fromLists(lists);
    }
}
like image 142
Jon Skeet Avatar answered Dec 17 '22 20:12

Jon Skeet