I am trying to copy an ArrayList, but it seems to be just referencing it and not really copying it.
My code:
List<Column> columns = columnData(referencedField.table); List<Field> newPath = new ArrayList<>(startPath); System.out.println("Equals? " + (newPath.equals(startPath))); startPath is an ArrayList<Field> being passed into the function.
Field:
public class Field { public String table; public String column; public Field(final String table, final String column) { this.table = table; this.column = column; } @Override public String toString() { return "(" + table + ", " + column + ")"; } @Override public int hashCode() { int hash = 3; hash = 67 * hash + Objects.hashCode(this.table); hash = 67 * hash + Objects.hashCode(this.column); return hash; } @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Field other = (Field) obj; if (!Objects.equals(this.table, other.table)) { return false; } if (!Objects.equals(this.column, other.column)) { return false; } return true; } } Now I have read about cloning it, but I am not experienced in it. I am actually wondering if a clone will really create a list of new objects such that they are not being equal anymore, as with same contents the elements would be equal.
So to rephrase, what I want is: To make sure that startPath and newPath are not equal and thus not referenced to eachother, as I am using the Lists a recursive function that should add the lists to a map.