I have two lists of String
s and two lists of objects. I need to copy the list of strings to the correspondence list of objects with specific values.
public List<Name> retrieveCitiesAndCountries(){
List<String> list1 = new ArrayList<String>(); //list of cities
List<String> list2 = new ArrayList<String>(); //list of countries
list1 = retrieveListOfCities();
list2 = retrieveListOfCountries();
List<Name> namesList = new ArrayList<Names>();
return namesList;
}
Name class
class Name{
String name;
String attrib;
..
}
I need to add both lists to namesList
and add the respective attribs. For example, all cities should have City
as their attribs and all countries should have Country
as their attribs.
for(int i=0;i<list1.size();i++){
Name name = new Name();
name.setName(list1.get(i));
name.setAttrib("City");
namesList.add(name);
}
for(int i=0;i<list2.size();i++){
Name name = new Name();
name.setName(list2.get(i));
name.setAttrib("Country");
namesList.add(name);
}
I am wondering if there is any better way to do it.
I use namesList to return in JSON format.
@RequestMapping(value = "/names")
public @ResponseBody List<Name> retrieveCitiesAndCountries(){
List<Name> namesList = retrievalServ.retrieveCitiesAndCountries();
return namesList;
}
list1
andlist2
happen to be method arguments, for example), and how isnamesList
used finally? – h.j.k. Oct 9 '15 at 4:02Name
, if feasible, and how it is used? Is it meant to be used generically (in layman terms) to represent either city or country names? How about landmarks? Other geographical features? – h.j.k. Oct 9 '15 at 4:06