Home > Software design >  Remove specific elements with Streams
Remove specific elements with Streams

Time:10-29

I have these two methods where I want to remove specific elements.

The first one should return the following sequence of numbers: 4 5 7 8 10 11 13 14 ... The sequence contains no terms divisible by three. The sequence always starts at "4". But in my case it always just prints the paramter from the method.

The second one should delete all spaces from the passed string s1 and outputs the result. (“Hello world, how are you” becomes “Helloworld,howareyou?”). But it in my case it doesn't delete the blanks.

static void printFolgenOhne3(int anz) {
        List<Integer> item = new ArrayList<>();
        List<Integer> remove;
        item.add(anz);

        remove = item.stream()
                .filter(i -> anz % 3 == 0)
                .collect(Collectors.toList());
        item.removeAll(remove);
        item.forEach(System.out::println);
    }

static void deleteBlanks(String s1) {
        List<String> elements = new ArrayList<>();
        elements.add(s1);
        List<String> deleted = elements
                .stream()
                .filter(x -> !x.isBlank())
                .collect(Collectors.toList());
        System.out.println(deleted);
    }

CodePudding user response:

Note that in the first method, you can do the following to simplify removal.

List<Integer> list = new ArrayList<>(List.of(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15));
list.removeIf(element -> element % 3 == 0);
System.out.println(list);

prints:

[1, 2, 4, 5, 7, 8, 10, 11, 13, 14]

CodePudding user response:

Your methods should have some input parameters to represent sequences and return appropriate results instead of just printing them.

  • Sequence as List<Integer>:
    remove divisibles by 3;
    (optionally) ensure that the first number is 4;
    existing sequence not affected, new sequence is created and returned
public static List<Integer> removeDivisiblesBy3(List<Integer> seq) {
    return seq.stream()
        .dropWhile(x -> x != 4)  // optionally ensure to start from 4
        .filter(x -> x % 3 != 0) // keep NOT divisible by 3
        .collect(Collectors.toList());
}
  • Sequence as variable array of ints:
public static int[] removeDivisiblesBy3(int ... arr) {
    return IntStream.of(arr)
        .dropWhile(x -> x != 4)  // optionally ensure to start from 4
        .filter(x -> x % 3 != 0) // keep NOT divisible by 3
        .toArray();
}
  • Input sequence as String, output result - the string without spaces
    As mentioned in the comments it is enough to use String::replaceAll method to remove all unneeded whitespace characters from the input string:
public static String deleteBlanks(String str) {
    return str.replaceAll("\\s ", ""); // delete all whitespace characters
}

Tests:

System.out.println(removeDivisiblesBy3(Arrays.asList(1, 4, 6, 7, 9, 11, 12, 19, 22)));
System.out.println(Arrays.toString(removeDivisiblesBy3(1, 4, 6, 7, 9, 11, 12, 19, 22)));
System.out.println(deleteBlanks("Hello world, how are you?"));

Output:

[4, 7, 11, 19, 22]
[4, 7, 11, 19, 22]
Helloworld,howareyou?
  • Related