So, based on your description, I think the filter you want is this:
segments.stream()
.filter(m -> ((Long) m.get("ct")) != 1L)
.collect(toList());
Or being explicit about the type of the predicate:
.filter((Map<String, Object> m) ->
((Long) m.get("ct")) != 1L)
On your edit: you have erroneous data in the Map, or you've misunderstood the way it's represented. It appears that segments is actually a List<Object[]>.
I don't really have enough information to help you fix it.
To try debugging, you could do something like:
segments.stream()
.map(Arrays::toString)
.forEach(System.out::println);
This will tell you what's actually in it if you don't have documentation to refer to.
Answer from Radiodef on Stack OverflowSo, based on your description, I think the filter you want is this:
segments.stream()
.filter(m -> ((Long) m.get("ct")) != 1L)
.collect(toList());
Or being explicit about the type of the predicate:
.filter((Map<String, Object> m) ->
((Long) m.get("ct")) != 1L)
On your edit: you have erroneous data in the Map, or you've misunderstood the way it's represented. It appears that segments is actually a List<Object[]>.
I don't really have enough information to help you fix it.
To try debugging, you could do something like:
segments.stream()
.map(Arrays::toString)
.forEach(System.out::println);
This will tell you what's actually in it if you don't have documentation to refer to.
This works for me...
List<Map<String, Object>> segments = new ArrayList<Map<String, Object>>();
List<Map<String, Object>> segmentsWithMoreVersions = segments.stream()
.filter((Map<String, Object> s) -> {
return (Long) s.get("versions") > 1;
})
.collect(Collectors.toList());
Java 8 Filter Map<String,List<Employee>> - Stack Overflow
Using Java stream to filter a list and create a map - Code Review Stack Exchange
java - Map to List after filtering on Map's key using Java8 stream - Stack Overflow
collections - Java 8 Streams API to filter Map entries - Stack Overflow
To filter out entries where a list contains an employee who is not of the "M" gender:
Map<String, List<Employee>> r2 = map.entrySet().stream()
.filter(i -> i.getValue().stream().allMatch(e-> "M".equals(e.gender)))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
To filter out employees who are not of the "M" gender:
Map<String, List<Employee>> r1 = map.entrySet().stream()
.filter(i -> !i.getValue().isEmpty())
.collect(Collectors.toMap(Map.Entry::getKey,
i -> i.getValue().stream()
.filter(e -> "M".equals(e.gender)).collect(Collectors.toList())));
To filter out entries where a list doesn't contain any "M" employee.
Map<String, List<Employee>> r3 = map.entrySet().stream()
.filter(i -> i.getValue().stream().anyMatch(e -> "M".equals(e.gender)))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
Let's have 2 entries in the map:
"1" -> ["M", "M", "M"]
"2" -> ["M", "F", "M"]
The results for them will be:
r1 = {1=[M, M, M], 2=[M, M]}
r2 = {1=[M, M, M]}
r3 = {1=[M, M, M], 2=[M, F, M]}
In Java 8 you can convert a Map.entrySet() into a stream, follow by a filter() and collect() it. Example taken from here.
Map<Integer, String> map = new HashMap<>();
map.put(1, "linode.com");
map.put(2, "heroku.com");
//Map -> Stream -> Filter -> String
String result = map.entrySet().stream()
.filter(x -> "something".equals(x.getValue()))
.map(x->x.getValue())
.collect(Collectors.joining());
//Map -> Stream -> Filter -> MAP
Map<Integer, String> collect = map.entrySet().stream()
.filter(x -> x.getKey() == 2)
.collect(Collectors.toMap(x -> x.getKey(), x -> x.getValue()));
// or like this
Map<Integer, String> collect = map.entrySet().stream()
.filter(x -> x.getKey() == 3)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
And for your case it would look like this, because you also need to find out if there is a match in a List of object of class Employee.
Map<String, List<Employee>> collect = map.entrySet().stream()
.filter(x -> x.getValue().stream()
.anyMatch(employee -> employee.Gender.equals("M")))
.collect(Collectors.toMap(x -> x.getKey(), x -> x.getValue()));
Your sniplet wil produce a List<List<String>> not List<String>.
You are missing flatMap , that will convert stream of lists into a single stream, so basically flattens your stream:
List<String> wordsForGivenAlphabet = words.entrySet().stream()
.filter(x-> x.getKey().equalsIgnoreCase(inputAlphabet))
.map(Map.Entry::getValue)
.flatMap(List::stream)
.collect(Collectors.toList());
You can also add distinct(), if you don't want values to repeat.
Federico is right in his comment, if all you want is to get the values of a certain key (inside a List) why don't you simply do a get (assuming all your keys are uppercase letters already) ?
List<String> values = words.get(inputAlphabet.toUpperCase());
If on the other hand this is just to understand how stream operations work, there is one more way to do it (via java-9 Collectors.flatMapping)
List<String> words2 = words.entrySet().stream()
.collect(Collectors.filtering(x -> x.getKey().equalsIgnoreCase(inputAlphabet),
Collectors.flatMapping(x -> x.getValue().stream(),
Collectors.toList())));
You don't need the forEach method. You can map each entry of the Map to an int, and sum those integers :
int sum = inputMap
.entrySet()
.stream()
.filter(entry -> entry.getKey().field().equals(fieldName))
.mapToInt(entry -> Math.min(entry.getValue().size(), 5))
.sum();
With Eclipse Collections, the following will work using MutableMap and IntList.
MutableMap<Term, IntList> inputMap =
Maps.mutable.of(term1, IntLists.mutable.of(1, 2, 3),
term2, IntLists.mutable.of(4, 5, 6, 7));
long sum = inputMap
.select((term, intList) -> term.field().equals(fieldName))
.sumOfInt(intList -> Math.min(intList.size(), 5));
Note: I am a committer for Eclipse Collections.
I found the below approach to be working well for me. Any better solutions are welcome to post a reply and say why its better. Currently, this code returns a map keys with an empty list if no match is found, (If you have a solution which can eliminate keys from the result and return null when nothing matches that will be good too).
map.entrySet()
.stream()
.collect(Collectors.toMap(Map.Entry::getKey,
entry -> entry.getValue()
.stream().filter(predicate)
.collect(Collectors.toList()))
)
If you don't care about you're inital map then you can use following snippet, which iterates over the values stored in the map and then checks if an item in the list needs to be removed when it doesn't fullfil the predicate:
map.values().forEach(list -> list.removeIf(predicate.negate()));
What i have to mention though is, that this works only for mutable lists. For imutable ones an unchecked UnsupportedOperationException is thrown at runtime.
Doc: Collection.removeIf(Predicate)