Contains in Java, how to fetch a text in an ArrayList?

9

In C # there is the method contains of System.Linq that searches for text in a list, did the tests in Java and did not find a similar form even knowing that in Java 8 has expression, is there any way to do this with expression in Java?

Example:

ArrayList<String> strListas = new ArrayList<>();
strListas.add("Paulo");
strListas.add("Adriano");
strListas.add("Paula");

strListas.Contains("Pa");

Result:

Paulo e Paula
    
asked by anonymous 25.01.2017 / 14:23

3 answers

16
List<String> list = new ArrayList<>();
list.add("Paulo");
list.add("Adriano");
list.add("Paula");

List<String> resultado = list.stream()
                            .filter(s -> s.contains("Pa"))
                            .collect(Collectors.toList());
resultado.forEach(System.out::println);

Output:

  

Paulo

     

Paula

    
25.01.2017 / 14:31
9

I think you'll have to go through the list and make a loop to search.

for (String valor: strListas){
 valor.contains('Pa')
}
    
25.01.2017 / 14:27
7

If you need a case with case insensitive, use Pattern :

ArrayList<String> strList = new ArrayList<>();

strList.add("Paula"); // será encontrado
strList.add("Paulo"); // será encontrado
strList.add("paula"); // será encontrado
strList.add("Pedro");
strList.add("Pedro pa"); // será encontrado

Pattern p = Pattern.compile("Pa", Pattern.CASE_INSENSITIVE);

for (String nome : strList) {
    Matcher m = p.matcher(nome);
    if (m.find()) {
        System.out.println("Nome encontrado: " + nome);
    }
}
    
25.01.2017 / 14:57