Reputation: 83
I need to iterate through and ArrayList of objects and look for a variable that has the same value for multiple objects. As the example below, I am looking through an ArrayList, that has a sub-class . All I want to do is find out if a house or condo share the same listing number for this example code. I tried using a double loop, and an enhanced loop with a method (sameListingNum) and am having trouble working it out.
Thank you
// main client class
public static void main(String[] args)
{
ArrayList<House> listings = new ArrayList();
listings.add(new House(0001, 200000.00));
listings.add(new House(0201, 200000.00));
listings.add(new House(0001, 200000.00));
listings.add(new House(0401, 200000.00));
listings.add(new House(0031, 200000.00));
listings.add(new Condo(0401, 200000.00, 4));
listings.add(new Condo(0001, 120000.00, 3));
listings.add(new Condo(0301, 220000.00, 2));
listings.add(new Condo(0001, 130000.00, 3));
listings.add(new Condo(0201, 130000.00, 3));
for(House currentHouse: listings)
System.out.println(currentHouse);
for(int i=0; i<listings.size()-1; i++)
{
for(int j=i+1; j<listings.size(); j++)
{
}
}
// House Class
public class House
{
public int listingNum;
public double price;
public House()
{
listingNum = 0;
price = 0.00;
}
public House(int newListingNum, double newPrice)
{
listingNum = newListingNum;
price = newPrice;
}
public int getListingNum()
{
return listingNum;
}
public double getPrice()
{
return listingNum;
}
public String toString()
{
return ("Listing number: "+listingNum+", Price: "+price);
}
public boolean sameListingNum(Object other)
{
if(!(other instanceof House))
return false;
else {
House objHouse = (House)other;
if(listingNum - objHouse.getListingNum() == 0)
{
System.out.println("Same listing numbers: "
+listingNum+", "+objHouse.getListingNum());
return true;
}
else
return false;
}
}
}
// Condo Class
public class Condo extends House
{
public int connectedUnits;
public Condo()
{
super();
connectedUnits = 0;
}
public Condo(int newListingNum, double newPrice, int newConUnits)
{
super(newListingNum, newPrice);
connectedUnits = newConUnits;
}
public double getPrice()
{
return price;
}
public int getListingNum()
{
return listingNum;
}
public int getConnectedUnits()
{
return connectedUnits;
}
public String toString()
{
return super.toString()+", Number of connected unints: "+connectedUnits;
}
public boolean sameListingNum(Object other)
{
if(!(other instanceof House))
return false;
else {
House objHouse = (House)other;
if(listingNum - objHouse.getListingNum() == 0)
{
System.out.println("Same listing numbers: "
+listingNum+", "+objHouse.getListingNum());
return true;
}
else
return false;
}
}
Upvotes: 0
Views: 233
Reputation: 1084
Both of the other 2 answers will work alternativly you can implement comparable on the House... eg.
public class House implements Comparable<House>
@Override
public int compareTo(final House o) {
return listingNum - o.listingNum;
}
then inside your main method. Sort the Collection and check to see if the previous row has the same Id all the time.
Collections.sort(listings);
int previousListing = Integer.MIN_VALUE;
for (House currentHouse : listings) {
if (currentHouse.getListingNum() == previousListing){
System.out.println("Duplicate for " + currentHouse.getListingNum());
}
previousListing = currentHouse.getListingNum();
}
take your pick.
Upvotes: 1
Reputation: 3207
You can group it using a Map of List, like Map> groups.
Then you loop over you list and for each house/condo you put it in the group of the same listing number. At the end you will have a map where for each entry there are all houses/condos with same listing number.
Here a sample:
Map<Integer, List<House>> groups = new HashMap<Integer, List<House>>();
for (House house:listings) {
List<House> group = groups.get(house.getListingNum());
if (group == null) {
group = new ArrayList<House>();
groups.put(house.getListingNum(), group);
}
group.add(house);
}
for (Entry<Integer, List<House>> entry:groups.entrySet()) System.out.println("Listing Number "+entry.getKey()+" Houses/condos: "+entry.getValue());
Upvotes: 1
Reputation: 186
Try this:
foreach(House h in listings)
{
if(listings.Exists(p => p.sameListingNum(h)))
{
//do something
}
}
Upvotes: 0