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

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -