Home > Back-end >  Remove numbers from a list that begins with a specif digit
Remove numbers from a list that begins with a specif digit

Time:02-25

For example, i have this list

List<Integer> list = new ArrayList<>();

        list.add(64);
        list.add(5);
        list.add(10);
        list.add(66);
        list.add(7);
        list.add(68);

How i remove only the numbers that begins with "6"

CodePudding user response:

for example, you want to delete the numbers starting with 6 you can convert each int to a list while traversing the list and check if the first character of the string is "6". if it is 6 remove that number from the list

    for(int i: list)
    {
    String s= Integer.toString(i);
    if(s.charAt(0)=='6')
    {
    list.remove(new Integer(i));
    }
    }

CodePudding user response:

List result = list.stream().filter(a -> !a.toString().startsWith("6")).collect(Collectors.toList());

result.forEach(System.out::println);

  • Related