What I'm trying to achieve - have a list that is not shared by all the objects i.e. have a unique list for each objects created from a class, something similar to the below code which obviously results in an error because the ArrayList needs to be static.
public class Foo{
public ArrayList<String> chain = new ArrayList<>();
public addElement(String input){
this.chain.add(input);
}
}
public printList(){
for(String s : this.chain){
System.out.println(s);
}
}
public static void main(){
Foo x = new Foo();
Foo y = new Foo();
x.addElement("abc");
x.addElement("pqr");
y.addElement("lmn");
y.addElement("rty");
x.printList(); //print abc pqr
y.printList(); //print lmn rty
}
Is there a way to achieve the above outputs?
public addElement(String input)because it does not define a return type in its signature. Writepublic void addElement(String input)instead...Map<String, List<Map<String,Integer>>> m = new HashMap<String, ArrayList<HashMap<String, Integer> becomes = new HashMap<>