3D-kreativ
3D-kreativ

Reputation: 9301

Get value inside from a objects method in arraylist in Java?

I'm trying to select and get values from customer objects. What I want to do is to enter a personal number like "702312" and search after the customer objects that has a data member personal number that are equal to "702312". And when I found it I want to get the rest of the values or change it's content. This is some code that creates the customer objects from the class Customer and then it's stored in a arraylist.

// create a new customer object and send personal number, name and account number to constructor
Customer customer = new Customer(personalNumber, name, newAccountNumber);

// create an arraylist to store customer objects
ArrayList<Customer> customerList = new ArrayList<Customer>();

// add the new customer object to arraylist that holds all customer objects
customerList.add(customer);

I have tried to reach the values like this, but it's not working, so I'm looking for some help?

 // search for customer
 for (Customer customer : customerList) {
if(customer.getAccountOwnerPersonalNumber() == "702312"){
    System.out.println("OK!!!");
    }
 }

And instead of:

 if(customer.getAccountOwnerPersonalNumber() == "702312")...

I have tried this:

 if(customer.personalNumber == "702312")...

Finally I have also tested it like this:

for(int i=0;i<customerList.size();i++){
    if(customerList.get(i).getAccountOwnerName() == "702312");
    System.out.println("OK");
    break;
}

I'm not sure if I'm doing right!? Help is preciated! Thanks!

Upvotes: 0

Views: 6208

Answers (6)

Shivan Dragon
Shivan Dragon

Reputation: 15219

I would suggest you use java.util.Collections binarySearch(...) method. It's of order O(log n)(in this case, since ArrayList implements RandomAccess) which is tons better than the O(n) order you get from doing your for loop.

You can make a separate comparator for the desired Customer fields(s) (in this case "personalNumber"):

private static class CustomerPersonalNumberComparator implements Comparator<Customer> {

        @Override
        public int compare(Customer o1, Customer o2) {
            return o1.getPersonaNumber().compareTo(o2.getPersonaNumber());
        }

    }

Also, don't forget to sort your list according to your desired search criteria (the Comparator) before using binary search on it:

Collections.sort(customerList,new CustomerPersonalNumberComparator());
int desiredIndex = Collections.binarySearch(customerList, new Customer("658",null,null), new CustomerPersonalNumberComparator());
Customer desiredCustomer = desiredIndex == -1 ?  null : customerList.get(desiredIndex);

And don't forget that the binary search returns the position inside our array for the desired element or -1 if nothing was found. So you must use that returned position to retrieve the desired instance from the list.

Upvotes: 0

Jakub Matczak
Jakub Matczak

Reputation: 15656

You're trying to compare customer.personalNumber to "702312" which is a String, so i conclude customer.personalNumber is a String too. If I'm right, try String.equals() method instead of == operator. This is because this operator compares objects instead of value, which is what you want.

if(customer.getAccountOwnerPersonalNumber().equals("70312");)
  ...

Upvotes: 0

Shiv Kumar Ganesh
Shiv Kumar Ganesh

Reputation: 3825

If the data that you query for is a String then in that case you have to use equals()

So the entire things will turn out to be

if(customer.getAccountOwnerPersonalNumber().equals("702312")) { System.out.println("OK!!!"); }

Upvotes: 0

erimerturk
erimerturk

Reputation: 4288

customer.getAccountOwnerPersonalNumber() == "702312" this is wrong you cant compare them like that because it will compare their object type. you should do like this

if(customer.getAccountOwnerPersonalNumber().equals("702312"))

or this is better

if("702312".equals(customer.getAccountOwnerPersonalNumber()))

Upvotes: 1

Dave
Dave

Reputation: 556

You want .equals():

if(customerList.get(i).getAccountOwnerName().equals("702312"))

Upvotes: 1

BalusC
BalusC

Reputation: 1108632

You need to use the equals() method to compare objects like Strings by their internal value (otherwise they will only be compared by reference):

if (customer.getAccountOwnerPersonalNumber().equals("702312")) {
    System.out.println("OK!!!");
}

or, better, if it can potentially return null:

if ("702312".equals(customer.getAccountOwnerPersonalNumber())) {
    System.out.println("OK!!!");
}

or, if applicable, just make it a primitive like int, so that == will work the way you intended:

private int accountOwnerPersonalNumber;

with

if (customer.getAccountOwnerPersonalNumber() == 702312) {
    System.out.println("OK!!!");
}

Upvotes: 5

Related Questions