-3

I am trying to search for a keyword in the ArrayList, and If keyword is found, It prints how many "Keyword" there is in the ArrayList. But I get the print "No keywords found" no matter what keyword It is because the search method doesn't work for a reason.

Here's the code:

public ArrayList<Media> search(String keywordi)
{
    keyword = keywordi;
    System.out.println(keyword);
    return (ArrayList<Media>) storage.stream()
            .filter(m -> storage.contains(keyword))
            .collect(Collectors.toList());
}

And

ArrayList<Media> result = (ArrayList<Media>) katalog.search("Tasty");

    if(result.size()==0)
    {
        System.out.println("No keywords found.");

    }
    else
    {
        System.out.println(result.size()+" keywords found.");
    }
Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
logiacer
  • 31
  • 1
  • 6

3 Answers3

1

I guess the filter invocation should be

filter(m -> m.contains(keyword))

but your Media class does not have a contain method. So maybe it should be

filter(m -> m.beskrivning.contains(keyword))
whistling_marmot
  • 3,561
  • 3
  • 25
  • 39
0

This will helpful to your question :

    List<Media> list = Arrays.asList(new Media("T"), new Media("Keyword"), new Media("Keyword"),
            new Media("T"), new Media("Keyword"), new Media("Keyword"),
            new Media("F"), new Media("T"), new Media("Keyword"));
    String looking_for = "Keyword";
    long count
            = list.stream()
            .filter(i -> i.getWord().equals(looking_for)).count();
    System.out.println(count > 0 ? count + " keywords found" : "No keywords found");

Assuming your Media class like below :

class Media {

        private String word;

        public String getWord() {
            return word;
        }

        public void setWord(String word) {
            this.word = word;
        }

        public Media(String word) {
            this.word = word;
        }

    }
Madushan Perera
  • 2,568
  • 2
  • 17
  • 36
0

The method you probably want is either

public ArrayList<Media> search(String keywordi) {
    return storage.stream()
            .filter(media -> media.gettitel() != null ? media.gettitel().contains(keywordi) : false)
            .collect(Collectors.toCollection(ArrayList::new));
}

or

public List<Media> search(String keywordi) {
    return storage.stream() // => Stream<Media>
            .filter(media -> media.gettitel() != null ? media.gettitel().contains(keywordi) : false)
            .collect(Collectors.toList());
}

because ArrayList is just a detail that doesn't matter. The generic List interface is usually preferable.

The filter is applied to every element from the Stream<Media> you get by calling the stream() method. It's a boolean function that decides whether the element is supposed to be present in the next step or not. If a simple expression isn't enough you can as well define a whole code block to analyze the Media element you get:

public List<Media> search(String keywordi) {
    return storage.stream()
            .filter(media -> {
                if (media.gettitel() != null && media.gettitel().contains(keywordi))
                    return true;
                if (media.getMediatyp() != null && media.getMediatyp().contains(keywordi))
                    return true;
                return false;
            })
            .collect(Collectors.toList());
}
zapl
  • 63,179
  • 10
  • 123
  • 154
  • @logiacer try again, I've updated the code. In case `gettitel()` can return `null`, you'll have to catch that case. See http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it – zapl Dec 06 '15 at 22:14
  • I get the "No keywords found." because there is no data in the gettitel. It is stored in ArrayList storage, or correct me if I'm wrong? – logiacer Dec 06 '15 at 22:17
  • @logiacer I have not seen `Tasty` appear anywhere in your code so I don't know where it should find that. Is it something that can be found somewhere? – zapl Dec 06 '15 at 22:18
  • I'm searching for John at the moment, tasty was only for test haha. ArrayList result = (ArrayList) katalog.search("John"); – logiacer Dec 06 '15 at 22:20
  • @logiacer it works for me, but I've replaced the DVD and so on classes with `Media` directly. ( see http://ideone.com/3MOyDK) Are you maybe not passing the titel in the constructors of `Bok` down to `Media`? – zapl Dec 06 '15 at 22:29
  • @logiacer yes, you don't actually extend `Media`, you ignore it :) You should set Media's titel field etc as well (call all the `set..` methods in the constructor of `Bok` or call `super(mtitel, mutgivningsår, mpris, mbeskrivning)`with proper values), otherwise that base class is useless. – zapl Dec 06 '15 at 22:38
  • Do you mean calling the bok "setförfattare" and "setantalSidor" to Media's constructor? or the opposite? I'm new at Java, sorry for being noob. – logiacer Dec 06 '15 at 22:52
  • @logiacer the opposite. The `Bok` sits on top and needs to call down to the `Media`. – zapl Dec 06 '15 at 22:53
  • How do I call one constructor from another in Java? – logiacer Dec 06 '15 at 23:10
  • @logiacer http://ideone.com/3MOyDK and https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html and https://docs.oracle.com/javase/tutorial/java/IandI/super.html – zapl Dec 06 '15 at 23:18