Jump to content

ArrayList, Distinct Objects

- - - - -

  • Please log in to reply
10 replies to this topic

#1
SterAllures

SterAllures

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 203 posts
Hey CC'ers,

I've just finished an assignment and I wondered about somenthing.

I've created a method that takes an BankAccount ArrayList, substracts the Customer Objects out of it and put that in a new (Customer) ArrayList. After that returns that ArrayList.

The values in the Customer ArrayList have to be distinct.

I've created the following, which works:
public static ArrayList<Customer> extractCustomer(ArrayList<BankAccount> bankAccounts)
    {
        ArrayList<Customer> customerList = new ArrayList<Customer>();
        
        for(BankAccount ba : bankAccounts)
        {
            if(!customerList.contains(ba.customer))
            {
                customerList.add(ba.customer);
            }
            else
            {
                //Not sure why I have to specifically remove it 
                //but if I leave it clear it doesn't work
                customerList.remove(ba.customer);
            }
        }
        return customerList;
    }



I can check for duplicate values with contains().
What I don't understand why I can't do this:

if(!customerList.contains(ba.customer))
{
    customerList.add(ba.customer);
}
else
{
                
}
And just leave else blank because if the if isn't correct it can't add the value to the list anyway?


and I have to clearly state I don't what that value with remove()
Like I do here:
if(!customerList.contains(ba.customer))
{
    customerList.add(ba.customer);
}
else
{
    //Not sure why I have to specifically remove it 
    //but if I leave it clear it doesn't work
    customerList.remove(ba.customer);
}



I print the values with the following loop
for(Customer c : extractCustomer(oldBankAccounts))
{
    System.out.println(c.firstName);
}

4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Why not use a collection that doesn't allow duplicates? Ex.: A HashSet or a TreeSet.

#3
SterAllures

SterAllures

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 203 posts
I know there are better ways to approach this but this is an exercise to learn a bit about ArrayLists, so I have to do it this way, just wanted to know why I have to use remove().
This is by the way an exercise for KnowledgeBlackBelt.
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#4
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
            if(!customerList.contains(ba.customer))

            {

                customerList.add(ba.customer);

            }

            else

            {

                //Not sure why I have to specifically remove it 

                //but if I leave it clear it doesn't work

                customerList.remove(ba.customer);

            }
This seems illogical to me. This is how I'm reading what you have:
If you don't have an item in the list, then you want to add it. Otherwise, remove it. Why would you want to remove anything from your customerList?

The following seems to work and reproduces the results you want:

import java.util.ArrayList;

import java.util.Arrays;


public class ArrayListTest {

	public static void main(String[] args) {

		ArrayList<String> list = new ArrayList<>( Arrays.asList( new String[]{"a", "e", "i", "o", "u", "a", "e", "i", "o", "u"}) );

		

		ArrayList<String> otherList = new ArrayList<>();

		

		for(String s : list) {

			if(!otherList.contains(s))

				otherList.add(s);

		}

		

		

		System.out.println(list);

		System.out.println(otherList);

	}

}

prints:
[a, e, i, o, u, a, e, i, o, u]

[a, e, i, o, u]



#5
SterAllures

SterAllures

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 203 posts
That's what I thought would happen. but even if I do it exactly as you did I still get a multiple Object.

only when I use remove() it gives me distinct objects
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#6
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Do you think it has something to do with the way .contains() is comparing objects?
What type of object does BankAccount.customer return? A Customer Object? a String Object?

---------- Post added at 04:58 PM ---------- Previous post was at 04:53 PM ----------

Dur. I just looked at your code again. I see it returns a Customer Object. Are you overriding the equals() method in your Customer class?

#7
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
I assume, you have overridden the .equals(..) method from the Customer class, right?

#8
SterAllures

SterAllures

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 203 posts
Nope nothing at all

Customer.java:
public class Customer
{
    public String firstName;
    public String lastName;
}


(I know you should use getters and setters, but mind this is an exercise and it's suppose to be this way ;))
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#9
SterAllures

SterAllures

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 203 posts
Added my project file.
Hope I'm not doing somenthing wrong but without the remove() I get back 2 Johns

Attached Files


4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#10
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Check out the information for the contains method of ArrayList:

The definition states that it will use the equals method to compare the 2 objects. So, in your case, you'll need to override the equals method.
Without overriding the equals method, the contains method will use the inherited equals method of the Customer class.

#11
SterAllures

SterAllures

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 203 posts
Oke, then I'll override the equals() method in the main class I think.

Thanks for the help!
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users