Search String in an ArrayList?

Discussion in 'Spigot Plugin Development' started by Snowmite, Apr 15, 2017.

  1. Question: Hello! I have a question, how would I search a string in an ArrayList? So if a player says /search [Keyword], it will look inside a custom ArrayList for the keywords. How would I do this?
     
  2. Choco

    Moderator

    Code (Java):
    String value = yourList.stream().filter(s -> s.equalsIgnoreCase("theSearchArgument")).findFirst().orElse(null);
    Java 8 Stream API

    Or if you just want to know if it's there,
    Code (Java):
    yourList.contains("String");
     
    • Like Like x 1
    • Informative Informative x 1
  3. Do you want to know whether the list contains the specified element or at which (for example 1st, 4th, etc.) index is it or do you want to get another value based on what the keyword is?
     
  4. Damn i need to read Java 8 new things, it has a lot of improvements i think.
    If i am not wrong, you could just get the args between 0 and 1 and check if it equalsignorecase to what you want.
     
  5. ArrayLists have a .contains(Object) method that loops throught all content and check for .equals()