Home > Mobile >  Seach the Object From the array in Java
Seach the Object From the array in Java

Time:11-05

I am trying to search the object from the ArrayList

For Example in the Below Code the Object of Product class is going to stored in the ArrayList Now I want to Find the Product using its Product Name (By Contain Method)

import java.util.*;

class Product {
    String name;
    int price;
    int id;
    Product(int i, String name, int price) {
        this.id=i;
        this.name = name;
        this.price = price;
    }
}
public class Test{
    public static void main(String[] args) {
        ArrayList<Product> al = new ArrayList<Product>();
        al.add(new Product(1, "Samsung", 10000));
        al.add(new Product(2, "Apple", 20000));
        al.add(new Product(3, "Nokia", 30000));
        al.add(new Product(4, "Sony", 40000));
        al.add(new Product(5, "LG", 50000));
        for (Product p : al) {
            System.out.println(p.id   " "   p.name   " "   p.price);
        }
        System.out.println("Enter the name of the product to search:");
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();
        if (al.contains(name)) {
            System.out.println("Product found");
        } else {
            System.out.println("Product not found");
        }
    }
}

Please Can you Help Me out.

I am Trying to Search the Array of the Object using contain Method.

CodePudding user response:

kuriboh's explanation about why the contains method does not work in your sample is clear and I believe there is nothing to add to explain why your code is not working as you expected.

That being said, I know people recommended to loop through all the elementes in your list to compare by name and find the entry in the list that matches your criteria search. Another way to perform the product search by name is using lambda expressions.

//Search using lambda expression, comparing product name
Optional<Product> productFound = al.stream().filter(prod -> name.equalsIgnoreCase(prod.name)).findFirst();
if (productFound.isPresent()) {
    System.out.println("Product found");
    System.out.println(productFound.get());
} else {
    System.out.println("Product not found");
}

Running example

import java.util.ArrayList;
import java.util.Optional;
import java.util.Scanner;

public class Products {
    public static void main(String[] args) {
        ArrayList<Product> al = new ArrayList<Product>();
        al.add(new Product(1, "Samsung", 10000));
        al.add(new Product(2, "Apple", 20000));
        al.add(new Product(3, "Nokia", 30000));
        al.add(new Product(4, "Sony", 40000));
        al.add(new Product(5, "LG", 50000));
        for (Product p : al) {
            System.out.println(p.id   " "   p.name   " "   p.price);
        }
        System.out.println("Enter the name of the product to search:");
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();
        //Search using lambda expression, comparing product name
        Optional<Product> productFound = al.stream().filter(prod -> name.equalsIgnoreCase(prod.name)).findFirst();
        if (productFound.isPresent()) {
            System.out.println("Product found");
            System.out.println(productFound.get());
        } else {
            System.out.println("Product not found");
        }
        sc.close();
    }
}

class Product {
    String name;
    int price;
    int id;
    Product(int i, String name, int price) {
        this.id = i;
        this.name = name;
        this.price = price;
    }
    @Override
    public String toString() {
        return String.format("Product [name=%s, price=%s, id=%s]", name, price, id);
    }

}

Console output

1 Samsung 10000
2 Apple 20000
3 Nokia 30000
4 Sony 40000
5 LG 50000
Enter the name of the product to search:
Apple
Product found
Product [name=Apple, price=20000, id=2]

I hope this is helpful.

Happy Coding!

Karl

CodePudding user response:

containsReturns true if this list contains the specified element. While element in the list is Product and name is String, it always returns false in your case.

Try to implement your logic with a for loop on the whole list and check whether there is a product whose name is the desired name.

Reference: https://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html#contains(java.lang.Object)

CodePudding user response:

The contains() method checks whether a string contains a sequence of characters.

You cannot search in product object directly by contains() method, so first you must be get name variable of product object and then search what you want:

    for (Product p : al) {
        System.out.println(p.id   " "   p.name   " "   p.price);
        if (p.name.contains(name)) {
            System.out.println("Product found");
        } else {
            System.out.println("Product not found");
        }
    }
  • Related