• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

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

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

}

}

 
Sheriff
Posts: 21500
96
Chrome Eclipse IDE Java Spring Ubuntu VI Editor 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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!