我需要检查 FirstOrDefault() 是否不为空,如果不为空,则获取一些属性。我最好的选择是:
var results = Database.Set<Pizzas>().Select(x => new PizzaViewModel
{
Base = Database.Set<Bases>().FirstOrDefault(y => y.Id == x.Base.Id) != null ? Database.Set<Bases>().FirstOrDefault(y => y.Id == x.Base.Id).Name : null
}).ToList();
Run Code Online (Sandbox Code Playgroud)
有更好的方法吗?
我想按键对地图对象进行分组。我尝试使用此代码,但出现编译错误:
Non-static method cannot be referenced from a static context
Run Code Online (Sandbox Code Playgroud)
我的代码:
Map<String, List<A>> getAMap() {
return Arrays.stream(SomeArray.values())
.map(map -> createObject())
.collect(Collectors.groupingBy(Map.Entry::getKey,
Collectors.mapping(Map.Entry::getValue, Collectors.toList())));
}
private Map<String, A> createObject()
final A a = new A(some attributes);
Map<String, A> map = new LinkedHashMap<>();
map.put(some key, a);
.... // add another values.
return map;
}
Run Code Online (Sandbox Code Playgroud)
我需要类似的东西
{
"a", {a1, a2, a3},
"b", {a4, a5, a6},
}
Run Code Online (Sandbox Code Playgroud) 为什么这个函数返回浮点数?
func = lambda x: 1.
x = np.linspace(0,1,10)
func(x).shape
Run Code Online (Sandbox Code Playgroud)
结果是
func = lambda x: 1.
x = np.linspace(0,1,10)
func(x).shape
Run Code Online (Sandbox Code Playgroud)
我期望以下行为:
func = lambda x: 1. + 0*x
x = np.linspace(0,1,10)
func(x)
Run Code Online (Sandbox Code Playgroud)
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
Run Code Online (Sandbox Code Playgroud)
如何在不干扰 lambda 函数的情况下获得预期结果(即无需编写func = lambda x: 1. + 0*x)?
背后的想法是用户将此函数传递给另一个函数以在网格上进一步评估。我不能指望用户将常量函数设置为1 + 0*x。我应该怎么办?
我有一个使用参数作为 lambda 表达式的工作方法
private BiConsumer<List<String>, Properties> handleList(Properties p) {
return (list, prop) -> executeSubList(list, prop);
}
Run Code Online (Sandbox Code Playgroud)
因为p我收到了来自 SonarLint 的误报警告
Unused method parameters should be removed (squid:S1172)
Run Code Online (Sandbox Code Playgroud)
如果我更改prop为,p则会出现编译错误
Lambda 表达式的参数 p 无法重新声明封闭范围内定义的另一个局部变量
使用方法参数作为 lambda 参数时是否存在真正的问题或者是否存在误报检查?
我有一个包含动态创建的列的数据表:
Name | Role | Comm w1 | Role | Comm w2 | .... | Total Comm |
Run Code Online (Sandbox Code Playgroud)
我想获取包含Comm.
预期的结果应该是:
2
4
...
Run Code Online (Sandbox Code Playgroud) int val = integerList.stream().collect(
Collectors.reducing(0, a1 -> a1 * 5, (a1, a2) -> a1 + a2));
Run Code Online (Sandbox Code Playgroud)
上面的代码进行了归约操作。转换整数流和聚合函数以返回 Integer 。我无法理解以下代码和归约操作的内部实现。Java 如何执行以下有状态功能?谢谢!
java.util.stream.Collectors:reducing method
public static <T, U>
Collector<T, ?, U> reducing(U identity,
Function<? super T, ? extends U> mapper,
BinaryOperator<U> op) {
return new CollectorImpl<>(
boxSupplier(identity),
(a, t) -> { a[0] = op.apply(a[0], mapper.apply(t)); },
(a, b) -> { a[0] = op.apply(a[0], b[0]); return a; },
a -> a[0], CH_NOID);
}
Run Code Online (Sandbox Code Playgroud)
可能是,我会更好地澄清我的问题。上面的实现是如何获取数据流的。a[0],b[0] 是指数据流吗?我相信以上是为供应商和累加器提供功能实现。我想通过代码了解缩减过程是如何工作的。
我有两个清单。我想创建一个映射里面会有真正的匹配部件及虚假的唯一一个在Java 8
EG。
输入 -
列表1 = [A,B,C,D]
列表2 = [B,C,Y,Z]
输出 -
地图:
A,假
B,真
C真
D,假
我的代码:
Map<String,Boolean> map = new HashMap<>();
for(String var1 : list1) {
boolean value;
if (CollectionUtils.isNotEmpty(list2)) {
Optional<String> valueOptional = list2.stream()
.filter(e1 -> e1.equalsIgnoreCase(var1))
.findAny();
value = valueOptional.isPresent();
map.put(var1, value);
}
}
Run Code Online (Sandbox Code Playgroud) lambda表达式可以替代吗?使用当前的代码,我无法访问变量maxCnt,对其进行修改并发送回使用。
int maxCnt = 0;
incoming.stream().forEach(a -> {
Integer cnt = wordFrequency.get(a);
if (cnt != null) {
if (cnt > maxCnt) {
maxCnt = cnt;
}
}
});
Run Code Online (Sandbox Code Playgroud) 在查看 lambda 表达式时,我发现了匿名内部类和 lambda 表达式的以下行为。这背后的原因可能是什么?
Human h = new Human() {
int a = 2;
@Override
public void sing() {
System.out.println(++a);
}
};
h.sing();
h.sing();
Run Code Online (Sandbox Code Playgroud)
输出/输出
3
4
Run Code Online (Sandbox Code Playgroud)
而对于 lambda,我得到以下结果:
Human h = () -> {
int a = 2;
System.out.println(++a);
};
h.sing();
h.sing();
}
Run Code Online (Sandbox Code Playgroud)
输出/输出
3
3
Run Code Online (Sandbox Code Playgroud) 我有一个Map<String,Object>包含元素值的List<String>
现在我需要过滤后者并检索包含@.
我做得最好的是:
Map<String, Object> map = new HashMap<>();
map.put("alias", List.of("Mxxx", "fstarred@mymail.org"));
final Object obj = map.get("alias");
final List lst = (List) obj;
final Object output= lst.stream()
.filter(o -> ((String) o).contains("@"))
.findFirst()
.orElse(null);
Run Code Online (Sandbox Code Playgroud)
但是看起来很冗长,主要需要:
对于上面发出的,我试过了 .map(String.class::cast)
lst.stream()filter(o -> ((String) o).contains("@"))这些方法都没有奏效。
任何提示?
lambda ×10
java ×7
java-8 ×3
c# ×2
collectors ×2
java-stream ×2
collections ×1
datatables ×1
grouping ×1
linq ×1
numpy ×1
python ×1
reducing ×1
sonarlint ×1