How to compare Objects attributes in an ArrayList?

后端 未结 4 890
故里飘歌
故里飘歌 2020-12-22 09:57

I am fairly new to Java and I have exhausted all of my current resources to find an answer. I am wondering if it possible to access an Objects first property to see if it ma

相关标签:
4条回答
  • 2020-12-22 10:37

    You have already got the Product out of the List<Product> in the following statement:

    System.out.println(products.get(i));
    

    Now, that you have got Product, now to get it's id, you can just call it's getId() method:

    if (product.get(i).getId() == productId) {
        // Return this product.
    }
    

    I would also suggest you to use enhanced for-loop instead of the traditional loop like this:

    for (Product product: products) {
        // Now you have the product. Just get the Id
        if (product.getId() == productId) {
            return product;
        }
    }
    

    Also, you should change the type of productId from Integer to int. You don't need a wrapper type there.

    0 讨论(0)
  • Have you considered using a HashMap (or LinkedHashMap) instead of an Array. This way you can use the productId as the key and the product as the value?

    This will let you get the object without having to loop through the entire array.

    0 讨论(0)
  • 2020-12-22 10:54

    According to your commented line //I was trying: if (Product.getId() == productId) I think where you were falling over is using Product (capital P). What you needed was:

    if (products.get(i).getId() == productId)

    Also, you weren't returning the product you found...

    The problem with that form is that a) you have to find the product in the list twice (once in the condition and then again when printing the result - a third time if you returned the product) and b) it will throw a null pointer exception if the product you are looking for is not in the list.

    This is a nicer, more compact way of doing it:

    @Override
    public Product getProduct(int productId)
    {
      for(Product product : products)
      {
        if (productId == product.getId())
        {
          System.out.println(product.toString());
          return product;
        } 
      }
      return null;
    }
    
    0 讨论(0)
  • 2020-12-22 10:59

    For comparing the ArrayList Objects make override equal function in your CustomObject Class Like Employee.

    ArrayList<Employee> list;
    Employee emp; 
    //suppose you have some number of items in that list and emp object contains some value.
    int size=list.size();  
    for(int i=0;i<size;i++) {
        if(list.get(i).equals(emp)) {
            //todo perform operation on the basis of result.
        }
    }
    

    And suppose this is your Employee class and in that class you need to override the equal function.

    class Employee{
        int age;
        String name;
    
        public int getAge() {
            return this.age;
        }
    
        public String getName() {
            return this.name;
        }
    
        public void setAge(int emp_age) {
            this.age=emp_age;
        }
    
        public void setName(String emp_name) {
            this.name=emp_name;
        }
    
        @Override
        public boolean equal(Employee emp) {
            boolean isEqual=false;
            if(emp!=null && emp instanceof Employee) {
                isEqual=(this.age==emp.age);
            }
            return isEqual;
        }
    
    
    
    }
    

    I hope this solution will help you to check for equal values and compare the values.

    Thanks

    0 讨论(0)
提交回复
热议问题