Search / filter for a string or char in a string, list, and array in Java

This post covers the following topics on search and filter:
– Search for a sub string or char in a string
– Search for a string in a list
– Filter a string in a list
– Filter a sub string in a list
– Search for a string in an array

Example 1: Search for a sub string or char in a string

String.contains() matches the exact input string, or a sub string, or a character.

private void searchSring()
{
    String str = “abc3xy1cz”;
    System.out.printf( “Original string: %s %n”, str );
    System.out.printf( “Contains c3: %s %n”, str.contains( “c3” ) );
    System.out.printf( “Contains A: %s %n”, str.contains( “A” ) );
    System.out.printf( “Contains Z: %s %n”, str.contains( “Z” ) );
    System.out.printf( “Contains Z: %s %n”, str.toLowerCase().contains( “Z”.toLowerCase() ) );
}

Output:

Original string: abc3xy1cz 
Contains c3: true 
Contains A: false 
Contains Z: false 
Contains Z: true 

Example 2: Search for a string in a list

List.contains() searches for the exact input string.

private void searchList()
{
    List<String> originalList = new ArrayList<String>();
    originalList.add( “abc” );
    originalList.add( “a7c” );
    originalList.add( “xyZ” );

    System.out.printf( “Original list: %s %n”, originalList );
    System.out.printf( “Contains a: %s %n”, originalList.contains( “a” ) );
    System.out.printf( “Contains abc: %s %n”, originalList.contains( “abc” ) );
}

Output:

Original list: [abc, a7c, xyZ] 
Contains a: false 
Contains abc: true 

Example 3: Filter a string in a list

Filter out elements in a list which exactly matches the input string.

private void filterList()
{
    List<String> originalList = new ArrayList<String>();
    originalList.add( “abc” );
    originalList.add( “ABC” );
    originalList.add( “a7c” );
    originalList.add( “xyZ” );

    List<String> filteredList = new ArrayList<String>();
    String[] originalArray = { “abc”, “x” };

    for ( String str : originalArray )
    {
        if ( originalList.contains( str ) ) 
        {
            filteredList.add( str );
        }
    }

    System.out.printf( “Original list: %s, Filtered list: %s %n”, originalList, filteredList );
}

Output:

Original list: [abc, ABC, a7c, xyZ], Filtered list: [abc] 

Example 4: Filter a sub string in a list

Filter out elements in a list which matches the input sub string.

private void filterListSubString()
{
    List<String> originalList = new ArrayList<String>();
    originalList.add( “abc” );
    originalList.add( “ABC” );
    originalList.add( “a7c” );
    originalList.add( “xyZ” );

    List<String> filteredList = originalList.stream()
        .filter( str -> str.contains( “a” ) )
        .collect( Collectors.toList() );

    System.out.printf( “Original list: %s, Filtered list: %s %n”, originalList, filteredList );
}

Output:

Original list: [abc, ABC, a7c, xyZ], Filtered list: [abc, a7c] 

Example 5: Search for a string in an array

Methods contains(), or stream anyMatch() can be used to check if a string exactly matches with the elements in an array.

private void searchArray()
{
    String[] originalArray = { “aaa”, “bbb”, “ccc” }; 
    System.out.printf( “Original array: %s %n”, Arrays.asList( originalArray ) );|
    System.out.printf( “Contains a: %s %n”, Arrays.asList( originalArray ).contains( “a” ) );
    System.out.printf( “Contains aaa: %s %n”, Arrays.asList( originalArray ).contains( “aaa” ) );
    System.out.printf( “AnyMatch a: %s %n”, Arrays.stream( originalArray ).anyMatch( “a” :: equals ) );
    System.out.printf( “AnyMatch aaa: %s %n”, Arrays.stream( originalArray ).anyMatch( “aaa” :: equals ) );
    System.out.printf( “AnyMatch AAA: %s %n”, Arrays.stream( originalArray ).anyMatch( “AAA” :: equalsIgnoreCase ) );
}

Output:

Original array: [aaa, bbb, ccc] 
Contains a: false 
Contains aaa: true 
AnyMatch a: false 
AnyMatch aaa: true 
AnyMatch AAA: true

Leave a Comment

Your email address will not be published. Required fields are marked *