• Post Reply Bookmark Topic Watch Topic
  • New Topic

to compare array of objects in ArrayList(its working)  RSS feed

 
veena bijur
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks A lot to all it worked:

import java.util.*;
class persons
{
public String name;
public String city;
persons(String n,String c)
{
name = n;
city = c;
}
}
public class Assignment01
{
public static void main(String args[])
{
persons p1=new persons("a","xx");
persons p2=new persons("b","yy");
persons p3=new persons("c","zz");
persons p4=new persons("a","xx");
persons p5=new persons("a","yy");
persons p6=new persons("a","zz");
persons p7=new persons("a","xx");

ArrayList<persons> per=new ArrayList<persons>();

per.add(p1);
per.add(p2);
per.add(p3);
per.add(p4);
per.add(p5);
per.add(p6);
per.add(p7);


Iterator<persons> itr1 = per.iterator(); // iterator for persons

Set<String> set = new HashSet<String>();

persons element ;

while(itr1.hasNext())
{
element = itr1.next();
String names =element.city;
set.add(names);
}


Iterator<String> itr2 = set.iterator(); // iterator for set



System.out.println("Name City_name");


while(itr2.hasNext())
{
int count=0;
String a = itr2.next();

Iterator<persons> itr3 = per.iterator(); // iterator for persons
while(itr3.hasNext())
{
element = itr3.next();
String names = element.city;
if(a.compareTo(names) == 0)
{
count=count+1;
}
//System.out.println(names);

}
System.out.println(a + " " +count);
}

}

}

 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good to hear

But I think you pressed the wrong button and created a new thread instead of just a new post in your old thread. That doesn't matter much though.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!