java - How to iterate over a custom type and find specific one? -
i have arraylist of class olive:
arraylist<olive> olives = new arraylist<olive>(); olives.add(new kalamata()); olives.add(new ligurian()); olives.add(new kalamata()); olives.add(new ligurian());
inside kalamata , ligurian classes, have set names:
this.name = "ligurian"; this.name = "kalamata";
how can iterate on olive type arraylist? want find name variable = "ligurian" , return ligurian.getorigin()
use streams api:
olive olive = olives.stream() .filter(o -> o.name.equals("ligurian")) .findany().get(); string origin = olive.getorigin();
if you're using java 7 can use loop, shame ok:
olive found; for(olive olive : olives) { if(olive.name.equals("ligurian")) { found = olive; break; } } string origin = found.getorigin();
Comments
Post a Comment