Jmocke
Jmocke

Reputation: 271

Query an Array list of object

i created a class with the following definition

public class RegionsWithConstituency {

String reg_name;
String const_name;

public void setRegionName(String reg_name)
{
    this.reg_name = reg_name;
}
public String getRegionName()
{
    return reg_name;
}

public void setConstituencyName(String const_name)
{
     this.const_name = const_name;
}

public String getConstituencyName()
{
    return const_name;
}

@Override
public String toString()
{
    return const_name;
}

}

i populated this class as follow:

  ArrayList<RegionsWithConstituency> region = new ArrayList<RegionsWithConstituency>();

 for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);

            RegionsWithConstituency reg = new RegionsWithConstituency();
            reg.setRegionName(jsonObject.optString("Region"));
            reg.setConstituencyName(jsonObject.optString("Constituency"));
            region.add(reg);

          } 

i want to find the constituency name based on region name and save the result into a ArrayList of String. an example will be something like that

for (int i = 0; i < region.length(); i++) {

   if (regionList.Contains(region)){
   listofString.add(region.getConstituencyName())
  }
}

i am a c# background , i know how to achieve this one in c# but not in java, so i need a help about that , feel free to ask any question if u don't get my problem properly.

Upvotes: 0

Views: 8751

Answers (2)

Ker p pag
Ker p pag

Reputation: 1588

if you want to compare the objects inside the list or just use the contains method. in your RegionsWithConstituency object you should override the equals and hashcode method.

@Override
public boolean equals(Object obj) {
    // TODO Auto-generated method stub
    if (this == obj)
        return true;

    if (obj == null || (this.getClass() != obj.getClass())) {
        return false;
    }

    RegionsWithConstituency reg = (RegionsWithConstituency) obj;
    return this.const_name.equals(reg.getConstituencyName())
            && this.reg_name.equals(reg.getRegionName());
}

@Override
public int hashCode() {
    // TODO Auto-generated method stub
    return const_name != null ? const_name.hashCode() : 0;
}

and check if it contain the object

for (int i = 0; i < region.length(); i++) {

   if (regionList.Contains(region)){
   listofString.add(region.get(i).getConstituencyName())
  }
}

Upvotes: 1

Fallak Asad
Fallak Asad

Reputation: 368

You need to make a get function for region and do as follows . Here is the Working code

Iterator<RegionsWithConstituency> it=region.iterator();
            while(it.hasNext()){

            RegionsWithConstituency obj = it.next();

            if(obj.getRegionName().equals("Region"))
            {
            String Constituency=obj.getConstituencyName();

            listofString.add(Constituency);

            //do whatever u wanted to do with it now
            }
            }

Upvotes: 1

Related Questions