0

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.

1
  • You will have to implement equals() and hashcode(). Commented Aug 30, 2014 at 8:48

2 Answers 2

1

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())
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I am really grateful to you guy because your solution works fine like the other ones
1

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
            }
            }

1 Comment

I am really grateful to you guy because your solution works fine like the other ones

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.