Home > Blockchain >  Searching an ArrayList for a matching parameter
Searching an ArrayList for a matching parameter

Time:11-08

I have two classes Museum and Painting. Painting class is working as expected, but I am having issues with the Museum class. The purpose of the method is to return an ArrayList of paintings the museum has, which matches the parameter string.

When I try to compile the code, I am getting lots of error messages and clearly I am not compiling the code correctly.

For example, if there is a Painting by Picasso - it should just return all those paintings and nothing else.

I think I may have missed a step - potentially by creating a local variable to store it in first, but I'm at a bit of a brick wall. I also wonder if String is correct when the ArrayList uses the Painting object.

Does anyone know what I'm missing here?

public class Museum  {
    //creating the fields
    private ArrayList<Painting> paintings;
    private String name;

    /**
     * Create a Museum Class 
     */
    public Museum(String aMuseum) {
        paintings = new ArrayList<>();
        name = aMuseum;
    }

    public String listMatches(String searchString)
    {
        if(filename.equals(searchString)) {
            return filename;
        }
    }
}

CodePudding user response:

Searching paintings by artist should return a sublist of paintings (it may be empty if no painting is found):

public List<Painting> listMatches(String artist) {
    List<Painting> matches = new ArrayList<>();
    for (Painting painting : paintings) {
        if (artist.equals(painting.getArtist())) {
            matches.add(painting);
        }
    }

    return matches;
}

Stream API may be used for this (more concise and functional style):

public List<Painting> listMatches(String artist) {
    return paintings.stream()
        .filter(p -> artist.equals(p.getArtist()))
        .collect(Collectors.toList());
}

CodePudding user response:

Okay I'm assuming here in your Painting class, you have an attribute which is Author and a getter for it.

I've changed the listMatches method to have a for-each loop. This loop will go through every element in your Paintings arraylist, and saving in a local variable the name of its author. If the current painting's author matches the one you are looking for, it will print the title.

I changed the return type of listMatches from String to void, because you are not returning anything, just printing.

Instead of just printing the names you could save the paintings somewhere (another ArrayList for example) if you want to use them. Remember to change the return type, because in that case you will be returning something.

Remember to write the name of the author exactly as you have it in the paintings or else it might not find it.

I havent had a chance to try it, but it should work.


public class Museum  {
//creating the fields
private ArrayList<Painting> paintings;
private String name;

/**
 * Create a Museum Class 
 */
public Museum(String aMuseum) {
    paintings = new ArrayList<>();
    name = aMuseum;
}

public void listMatches(String searchString)
{
  String this_painting_author = new String();
  for (Painting painting : paintings){
    this_painting_author = painting.getAuthor();
    if(this_painting_author.equals(searchString)) {
      System.out.println(painting.getTitle());
    }
  }
}
}
  • Related