0

I am trying to return all items with the same category name within a list I made myself.

But it is returning only one category item when there are more items with the same category name.

What can I do to return all of them?

I did this method in my item class :

public static String findCategory(List<item>items ,String cat ) { 
    
    for (int i =0 ; i < items.size() ; i++) {
        
        if(items.get(i).getcategory().equals(cat)) 
        {
            return items.get(i).toString();
        }
        
    }
    return "Invalid Category!" ;
}

I am calling this method in my main as :

System.out.println("Enter category name : ");
String categ = input.next() ;
System.out.println(item.findCategory(itemDatabase, categ)) ;
1
  • 1
    Why don't you collect the items to a list inside the method and return that list? Rather than simply returning the first encountered match? Commented Mar 12, 2021 at 6:26

2 Answers 2

2

I am trying to return all items with the same category name within a list

Using Stream it's pretty easy:

public static List<item> findCategory(List<item> items, String cat) { 
    return items.stream()
      .filter(item -> item.getcategory().equals(cat))
      .collect(Collectors.toList());
}
Sign up to request clarification or add additional context in comments.

3 Comments

How can i use this to return only specific attributes such as only the item name and the price?
You use the map Operator. Here are some examples how Streams work: baeldung.com/java-8-streams-introduction
I don't see a good example on the link about my problem. Can you tell me where and how I must put the map Operator?
2

It is important to realize that java exits the function after the First return call which is why you encountered the problem where it only returns one category.

You need to, within the function, create a new list and store the items with the same category name within said list. Only once that list is full, you can return it.

Be sure to adjust the function header to accommodate the new return type

public static List<item> findCategory(List<item> items, String cat)

So to put what I said into code:

Create the new list before the for loop:

List<item> out = new ArrayList<item>();

And within your for loop simply append to the list if you have a match:

out.add( items.get(i).toString() );

Then at the end of the function return this list

return out;

Edit: Here is how it looks put together:

public static List<item> findCategory(List<item>items ,String cat ) { 

    List<item> out = new ArrayList<item>();

    for (int i =0 ; i < items.size() ; i++) {
    
        if(items.get(i).getcategory().equals(cat)) 
        {
            out.add( items.get(i).toString() );
        }
    }

    return out;
    
}

4 Comments

It might be more helpful, if you put the new code for creating and populating the list to be returned in the context of the OP example. It might reduce the number of questions you get about where to put each line in the code :)
How can i use this to return only specific attributes such as only the item name and the price?
Can you please add the getcategory() function you made and an example of the list you created to the question?
I'm trying to understand your question, are you asking on how to return a list of elements based on parameters such as (1) must match category a and b or (2) must match category a or b?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.