0

I have the following 3 classes:

Order.java

public class Order<T> implements Comparator<File> {

    T[] args;

    public Order(T[] args) {
        this.args = args;
    }

    @Override
    public int compare(File o1, File o2) {
        return 0;
    }

}

FileList.java:

public class FileList extends LinkedList<File> {

    public FileList() {
        this.add(new File("example.txt"));
    }

    public void printByOrder(Order order) {
        this.stream().sorted(order)
                .forEach(file -> System.out.println(file.getName()));
    }

}

And finally, Test.java:

public class Test {

    public static void main(String[] args) {
        FileList fileList = new FileList();
        Integer[] arguments = {1, 2, 3};
        Order<Integer> order = new Order<>(arguments);
        fileList.printByOrder(order);
    }

}

I get an error in the following line:

this.stream().sorted(order)
            .forEach(file -> System.out.println(file.getName()));

Stating:

Error:(12, 57) java: cannot find symbol
symbol:   method getName()
location: variable file of type java.lang.Object

I understand it is a problem with Order having raw type. For example, changing printByOrder to:

public void printByOrder(Order<Integer> order) {
        this.stream().sorted(order)
                .forEach(file -> System.out.println(file.getName()));
}

fixes the error, but I would like to be able to send an Order object with any type variable to this method.

What is the correct way of doing it?

0 Answers0