In java8, how to set the global value in the lambdas foreach block?

You could, of course, “make the outer value mutable” via a trick: public void test() { String[] x = new String[1]; List<String> list = Arrays.asList(“a”, “b”, “c”, “d”); list.forEach(n -> { if (n.equals(“d”)) x[0] = “match the value”; }); } Get ready for a beating by the functional purist on the team, though. Much nicer, … Read more

java: Arrays.sort() with lambda expression

The cleanest way would be: Arrays.sort(months, Comparator.comparingInt(String::length)); or, with a static import: Arrays.sort(months, comparingInt(String::length)); However, this would work too but is more verbose: Arrays.sort(months, (String a, String b) -> a.length() – b.length()); Or shorter: Arrays.sort(months, (a, b) -> a.length() – b.length()); Finally your last one: Arrays.sort(months, (String a, String b) -> { return Integer.signum(a.length() – … Read more

Do Java8 lambdas maintain a reference to their enclosing instance like anonymous classes?

Lambda expressions and method references capture a reference to this only if required, i.e. when this is referenced directly or an instance (non-static) member is accessed. Of course, if your lambda expression captures the value of a local variable and that value contains a reference to this it implies referencing this as well…

Construct LambdaExpression for nested property from string

Do you mean: static LambdaExpression CreateExpression(Type type, string propertyName) { var param = Expression.Parameter(type, “x”); Expression body = param; foreach (var member in propertyName.Split(‘.’)) { body = Expression.PropertyOrField(body, member); } return Expression.Lambda(body, param); } For example: class Foo { public Bar myBar { get; set; } } class Bar { public string name { get; … Read more

How do static variables in lambda function objects work?

tl;dr version at the bottom. §5.1.2 [expr.prim.lambda] p1 lambda-expression: lambda-introducer lambda-declaratoropt compound-statement p3 The type of the lambda-expression (which is also the type of the closure object) is a unique, unnamed nonunion class type — called the closure type — whose properties are described below. This class type is not an aggregate (8.5.1). The closure … Read more

Can using a lambda in header files violate the ODR?

This boils down to whether or not a lambda’s type differs across translation units. If it does, it may affect template argument deduction and potentially cause different functions to be called – in what are meant to be consistent definitions. That would violate the ODR (see below). However, that isn’t intended. In fact, this problem … Read more

Java 8 stream map to list of keys sorted by values

You say you want to sort by value, but you don’t have that in your code. Pass a lambda (or method reference) to sorted to tell it how you want to sort. And you want to get the keys; use map to transform entries to keys. List<Type> types = countByType.entrySet().stream() .sorted(Comparator.comparing(Map.Entry::getValue)) .map(Map.Entry::getKey) .collect(Collectors.toList());