Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,880 questions

51,806 answers

573 users

How to filter a Collection based on predicate in Java

1 Answer

0 votes
import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.function.Predicate; 

public class MyClass {
    public static void main(String args[]) {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);

        Predicate<Integer> isOdd = n -> n % 2 != 0;

        List<Integer> oddNumbersList = list.stream()
                                        .filter(isOdd)
                                        .collect(Collectors.toList());

        System.out.println(oddNumbersList); 
    }
}
  
  
  
/*
run:
     
[1, 3, 5, 7, 9, 11]
     
*/

 



answered Nov 13, 2023 by avibootz

Related questions

1 answer 105 views
1 answer 106 views
1 answer 144 views
2 answers 173 views
173 views asked Jun 24, 2021 by avibootz
2 answers 166 views
...