我有一段Java代码,它从Optional#map的输入参数中包含的集合中删除一个元素
boolean ret = methodReturnsOptioanl()
.map(project -> project.getDocIds().remove(docId))
.orElse(false);
Run Code Online (Sandbox Code Playgroud)
其中project.getDocIds()返回一组字符串id并保证不为空。
我已经测试过并且可以工作;如果Optional为空或者集合中不存在docId,ret为false。
但是,Optional#map 可以执行此操作并更改成员集的状态并返回 Set#remove 操作的布尔结果吗?
我四处搜寻,找不到任何明确的答案。
从 Java8 迁移到 Java11 后,我收到错误“包 com.sun.jndi.ldap 不可见”。但我需要这个包用于 LdapCtxFactory 类。包是否被移动或者我应该为我的 Ldap 连接使用另一个类?
此致
假设您有一个Optional<T>变量可以采用 3 个值之一:
检索值T或null其他值的最佳方法是什么?
这是一种方法:
Optional<String> op = ...
String str = op != null ? op.orElse(null) : null;
Run Code Online (Sandbox Code Playgroud)
这是另一种方法:
String str = Optional.ofNullable(op).flatMap(innerOpt -> innerOpt).orElse(null);
Run Code Online (Sandbox Code Playgroud)
各自的优点和缺点是什么?性能更高吗?又是一种 scala 式的吗?是否更具可读性/可维护性?
我听说返回是不好的做法null。
在这种情况下,除了退货还有哪些选择null?
public RollingStock getHeadPoint() {
if (!train.isEmpty()) {
return train.get(0);
} else {
return null;
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用 Maven 插件 avro-maven-plugin (1.9.2) 从 AVRO-schema-file (avsc) 生成 Java 类。我定义一个日期字段如下:
{
"name": "inceptionDate",
"type": "int",
"logicalType": "date"
}
Run Code Online (Sandbox Code Playgroud)
我面临的问题是,它生成一个int而不是Date或LocalDate。
private int inceptionDate;
Run Code Online (Sandbox Code Playgroud)
pom.xml配置定义如下:
<plugin>
<groupId>org.apache.avro</groupId>
<artifactId>avro-maven-plugin</artifactId>
<version>1.9.2</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>schema</goal>
</goals>
<configuration>
<dateTimeLogicalTypeImplementation>JSR310</dateTimeLogicalTypeImplementation>
<sourceDirectory>${project.basedir}/src/main/resources/schema/</sourceDirectory>
<outputDirectory>${project.build.directory}/generated-sources/main/java/</outputDirectory>
<stringType>String</stringType>
<fieldVisibility>PRIVATE</fieldVisibility>
</configuration>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
有什么想法吗,有什么问题吗?
当我的交易对手可以是时,我如何null使用 Java 8 处理以下代码中的检查null。counterParty我只想在它有值时设置,如果它为空则不设置。
public static Iterable<? extends Trade> buildTrade (final List<Trade> trade) {
return () -> trade.stream()
.map(trade -> Trade.newBuilder()
.setType(trade.type())
.setUnit(trade.unit())
.setCounterParty(trade.counterParty())
.build())
.iterator();
}
Run Code Online (Sandbox Code Playgroud) 我需要在 java 中检查字符串是否仅由 Unicode 值 [\u0030-\u0039] 或 [\u0660-\u0669] 组成。做到这一点最有效的方法是什么?
我正在尝试使用stream()并放入地图来迭代列表,其中键是 steam 元素本身,值是 AtomicBoolean,true。
List<String> streamDetails = Arrays.asList("One","Two");
toReplay = streamDetails.stream().collect(Collectors.toMap(x -> x.toString(), new AtomicBoolean(true)));
Run Code Online (Sandbox Code Playgroud)
我在编译时收到以下错误。
Type mismatch: cannot convert from String to K
The method toMap(Function<? super T,? extends K>, Function<? super T,? extends U>) in the type Collectors is not applicable for the arguments ((<no type> x) -> {},
AtomicBoolean)
Run Code Online (Sandbox Code Playgroud)
我可能做错了什么,我应该用什么x -> x.toString()来代替?
我正在尝试创建一个类似于 的自定义浮动添加Collectors.summingDouble()。
但我面临两个问题,我不知道如何解决它。
BiConsumer- 第 #27 行 - void 方法不能返回值Collectors.ofBiConsumer<R,T>- 第 32 行 - Collector 类型中的 (Supplier, , , Collector.Characteristics...)方法BinaryOperator<R>不适用于参数 ( Supplier<Float[]>, BiConsumer<Float,Employee>, BinaryOperator<Float>) 这里需要做什么来解决这个问题?public class CustomCollector {
public static void main(String[] args) {
Employee e1=new Employee(1,"Tom",10.2f);
Employee e2=new Employee(1,"Jack",10.4f);
Employee e3=new Employee(1,"Harry",10.4f);
ArrayList<Employee> lstEmployee=new ArrayList<Employee>();
lstEmployee.add(e1);lstEmployee.add(e2);lstEmployee.add(e3);
/* Implementation 1
* double totalSal=lstEmployee.stream().collect(Collectors.summingDouble(e->e.getSal()));
System.out.println(totalSal);
*/
//Implementation 2
Function<Employee,Float> fun=(e)->e.getSal();
BiConsumer<Float,Employee> consumer=(val,e)->val+e.getSal();
BinaryOperator<Float> operator=(val1,val2)->val1+val2;
Supplier<Float[]> supplier=() -> new Float[2]; …Run Code Online (Sandbox Code Playgroud) public void setQueryTimeout(int queryTimeout)我想知道我们需要传入的值的类型是什么jdbcTemplate。文件说
> Set the query timeout for statements that this JdbcTemplate executes.
> <p>Default is -1, indicating to use the JDBC driver's default (i.e. to
> not pass a specific query timeout setting on the driver). <p>Note: Any
> timeout specified here will be overridden by the remaining transaction
> timeout when executing within a transaction that has a timeout
> specified at the transaction level. @see
> java.sql.Statement#setQueryTimeout
Run Code Online (Sandbox Code Playgroud)
想知道queryTimeout是否是milliseconds, …
java ×10
java-8 ×10
java-stream ×3
collections ×2
option-type ×2
apache-kafka ×1
avro ×1
avro-tools ×1
collectors ×1
java-11 ×1
jdbc ×1
jdbctemplate ×1
ldap ×1
monads ×1
package ×1
regex ×1
scala ×1
unicode ×1