1

What is Java 8 equivalent method for where extension method in C#?

How can I filter element of a Hashtable in Java 8 using lambda expression

nZeus
  • 2,475
  • 22
  • 21
Shashank Kumar
  • 1,210
  • 10
  • 18

2 Answers2

11

You need to use the stream filter() function, which takes a Predicate object defining your filtering criteria.

Here's a useful tutorial. Note the usage:

List<User> olderUsers = users. 
                        stream().
                        filter(u -> u.age > 30).
                        collect(Collectors.toList());

and the subsequent collection into a collection type of your choice.

Note that I'd likely avoid Hashtable in preference to HashMap. See this useful SO entry.

Community
  • 1
  • 1
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
3

Actually he's asking about hashtable, but there is not much a difference there.

        Hashtable<String, Integer> someHashTable;
        someHashTable.entrySet().stream().filter((entry)-> entry.getValue()>3).forEach(System.out::println);

Here's the tutorial I've used to understand java8 new features including streams http://winterbe.com/posts/2014/03/16/java-8-tutorial/ best one I've found so far. Enjoy :)

vach
  • 10,571
  • 12
  • 68
  • 106