ArrayList<String> list = new ArrayList<String>();
list = (ArrayList<String>) Files.readAllLines(FilePath);
In the above code, if I remove the explicit casting in second line, the compiler gives an error. Why is this explicit cast required considering the fact that Files.readAllLines(FilePath) returns a List<String>, and ArrayList<String> implements List<String> ?
List<String> listinstead.Files.readAllLines(FilePath)returns aList<String>, but the implementation of the list might not beArrayList. Your cast is therefore unsafe. You should just doList<String> list = Files.readAllLines(filePath);.ArrayListis aList, but not everyListis anArrayList.