I am trying to add an object of class Document to an arraylist in a loop. the problem is that every time the object changes, the prevous contents of the list changes too
here's my Document class:
public class Document {
public Map<String, Double> tokens;
public String category;
}
and I have the list dataset:
List<Document> dataset = new ArrayList<Document>();
I read all the tokens of a document from a file to a map named counts. then do as follows
doc = new Document();
doc.tokens = counts;
doc.category=sampleCategory;
dataset.add(doc);
counts.clear();
but every time i read a new doc, the contents of dataset changes to the values of new doc. So how can I add the doc by value, not by reference?