create a nested map using hashmap in java to accomplish the nested array but not working -
i want create nested array in php. told not available in java , tell use map , hash map. have tried following accomplish issue
list<webelement> address = driver.findelements(by.xpath(addressxpath)); list<webelement> addressprimarylist = driver.findelements(by.xpath(addressprimaryxpath)); list<webelement> addresssecondrylist = driver.findelements(by.xpath(addresssecondryxpath)); list<webelement> addresslocationlist = driver.findelements(by.xpath(addresslocationxpath)); map<string, string> addresses = new hashmap<string, string>(); map<string, string> addressesparent = new hashmap<string, string>(); int totaladd = address.size(); system.out.println("total addresses " + totaladd); (int = 0; < totaladd; i++) { addressesparent.put("'" + + "'", addresses.put("addressprimary", addressprimarylist.get(i).gettext())); addressesparent.put("'" + + "'", addresses.put("addresssecondry", addresssecondrylist.get(i).gettext())); addressesparent.put("'" + + "'", addresses.put("addresslocation", addresslocationlist.get(i).gettext())); } // addresses.get("addressprimary"); // returns "addressprimary value" system.out.println(addresses.tostring()); system.out.println(addressesparent.tostring());
it gives me following results
{addresssecondry=apt b, addresslocation=merrick, ny 11566-3559, addressprimary=1964 stuyvesant ave} {'0'=null, '1'=merrick, ny 11566-4523}
but want results thing in php
note: following php code example want similar
$addresses[0]['primary'] = $primaryaddress; $addresses[0]['secondy'] = $secondryaddress; $addresses[0]['location'] = $locationaddress; $addresses[1]['primary'] = $primaryaddress; $addresses[1]['secondy'] = $secondryaddress; $addresses[1]['location'] = $locationaddress;
i think making wrong approach. should encapsulate data in object , create list (or array) of these objects. like:
public class address { private string addressprimary, addresssecondry, addresslocation; public void setaddressprimary(string ap) { addressprimary = ap; } public string getaddressprimary() { return addressprimary; } public void setaddresssecondry(string as) { addresssecondry = as; } public string getaddresssecondry() { return addresssecondry; } public void setaddresslocation(string al) { addresslocation = al; } public string getaddresslocation() { return addresslocation; } }
merged code like:
list<webelement> address = driver.findelements(by.xpath(addressxpath)); list<webelement> addressprimarylist = driver.findelements(by.xpath(addressprimaryxpath)); list<webelement> addresssecondrylist = driver.findelements(by.xpath(addresssecondryxpath)); list<webelement> addresslocationlist = driver.findelements(by.xpath(addresslocationxpath)); int totaladd = address.size(); system.out.println("total addresses " + totaladd); list<address> addresses = new arraylist<>(); (int = 0; < totaladd; i++) { address address = new address(); address.setaddressprimary(addressprimarylist.get(i).gettext()); address.setaddresssecondry(addresssecondrylist.get(i).gettext()); address.setaddresslocation(addresslocationlist.get(i).gettext()); addresses.add(address); }
you access data following:
addresses.get(0).getaddressprimary(); // ^ ^ // | | // returns address object // | // returns addressprimary property
you work maps think bad style , wrong approach:
list<webelement> address = driver.findelements(by.xpath(addressxpath)); list<webelement> addressprimarylist = driver.findelements(by.xpath(addressprimaryxpath)); list<webelement> addresssecondrylist = driver.findelements(by.xpath(addresssecondryxpath)); list<webelement> addresslocationlist = driver.findelements(by.xpath(addresslocationxpath)); int totaladd = address.size(); system.out.println("total addresses " + totaladd); list<map<string, string> addresses = new arraylist<>(); (int = 0; < totaladd; i++) { map<string, string> address = new hashmap<>(); address.put("addressprimary", addressprimarylist.get(i).gettext()); address.put("addresssecondry", addresssecondrylist.get(i).gettext()); address.put("addresslocation", addresslocationlist.get(i).gettext()); addresses.add(address); }
then access data with:
addresses.get(0).get("addressprimary");
i don't recommend this.
Comments
Post a Comment