I can't get this method to modify an ArrayList then return the modified ArrayList.
public static ArrayList<Integer> sortAndRemoveDuplicates(ArrayList<Integer> list) {
list = new ArrayList<Integer>(new HashSet<Integer>(list));
Collections.sort(list);
//System.out.println(list);
return list;
}
When I uncomment the System call I can see that the ArrayList is being modified and I know that my method works but when I run the method through main the ArrayList remains unaffected. Why is this? What am I doing wrong?
Why isn't this modified version working?
public static ArrayList<Integer> sortAndRemoveDuplicates(ArrayList<Integer> list) {
ArrayList<Integer> newList = new ArrayList<Integer>(new HashSet<Integer>(list));
Collections.sort(newList);
System.out.println(newList);
return newList;
}