我刚刚创建了以下简约测试用例:
package testcase;
public class Main
{
public static void main( String[] args )
throws Throwable
{
if ( args.length == 0 )
Main.class.getMethod( "main", String[].class ).invoke( null, new String[] { "test" } );
}
}
Run Code Online (Sandbox Code Playgroud)
它应该只运行,没有输出,也没有例外.该main方法应该使用反射来调用自身.但是我得到以下异常:
Exception in thread "main" java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at testcase.Main.main(Main.java:10)
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚,为什么......
LocalDateTime是抽象类.所以我写不出来:
LocalDateTime value = new LocalDateTime(); //error
Run Code Online (Sandbox Code Playgroud)
如果我想得到它的实例,我必须写:
LocalDateTime value = LocalDateTime.now(); //not error
Run Code Online (Sandbox Code Playgroud)
我有一个问题,为什么LocalDateTime可以返回实例?这是一个抽象类.
我看到了概述,但我找不到它......
如何转换这段代码:
private static Map<CustomObj<? extends CustomOtherObj>, CustomEnumeration> map;
if (map == null) {
map = Arrays.stream(CustomEnumeration.values())
.collect(Collectors.toMap(x -> x.source, x -> x));
}
Run Code Online (Sandbox Code Playgroud)
进入Java 7等价?(Arrays.stream.collect(Collectors....)部分.
基本上,上面的代码将一些元素放在地图中.
为什么这段代码不能为我编译?
我尝试使用stream和toMap选项将List转换为Map
List<CountryToPaymentMethodsDisplayRules>
paymentMethodDisplayRulesByCountryList =
gatway.getCountryPaymentMethodsDisplayRulesByCountry();
Map<PaymentMethod, CountryToPaymentMethodsDisplayRules>
countryToPaymentMethodsDisplayRulesMap = paymentMethodDisplayRulesByCountryList
.stream()
.collect(Collectors.toMap(type -> type.getCountryToPaymentMethodsDisplayRules().getPaymentMethod(),
type -> type));
public interface PaymentMethod extends Serializable {
}
public enum PaymentMethodType implements PaymentMethod, Serializable {
}
public interface CountryToPaymentMethodsDisplayRules {
public PaymentMethod getPaymentMethod();
}
public class CountryToPaymentMethodsDisplayRulesEntity implements CountryToPaymentMethodsDisplayRules, PersistentEntity<Long>, Serializable {
@Type(type = "com.plimus.core.payment.PaymentMethodTypeUserType")
@Column(name = "PAYMENT_TYPE")
private PaymentMethod paymentMethod;
}
Run Code Online (Sandbox Code Playgroud)
这有什么不对?
我试过类似的东西
Thread
.getAllStackTraces()
.keySet()
.stream()
.map(
$->{
System.out.println
($.getStackTrace());
return $;
});
Run Code Online (Sandbox Code Playgroud)
我觉得这不是最好的方法.还有其他方法吗? 最简单的方法是什么?使用Stream API.(需要打印整个堆栈跟踪)
例如,在显微镜下查看代码时,第一个代码块是否比第二个代码块使用更多的计算时间?
代码块1:
method1() {
do something;
call method2;
}
method2() {
do something simple like assign a variable value;
}
Run Code Online (Sandbox Code Playgroud)
代码块2:
method1(){
do something;
do something simple like assign a variable value;
}
Run Code Online (Sandbox Code Playgroud)
非常简单,但是我想看看这里是否有人知道考虑这一点的价值是什么(例如,优化程序以获得最短的计算时间).
作为一个例子,我了解到电路中的门具有有限的"Delta延迟",在该延迟中可以加起来看到在考虑设计中的时钟速度时电路将具有的最大延迟.Java有类似Delta延迟的东西吗?
希望有人能解决这个问题.,
对象结构我有一个类似的对象结构
主要对象是学生,学生得到一些信件
public class LetterRange {
private Date letterStartDate;
private Date letterEndDate;
public Date getLetterStartDate() {
return letterStartDate;
}
public void setLetterStartDate(Date letterStartDate) {
this.letterStartDate = letterStartDate;
}
public Date getLetterEndDate() {
return letterEndDate;
}
public void setLetterEndDate(Date letterEndDate) {
this.letterEndDate = letterEndDate;
}
}
public class Letters {
private String letterName;
private Set<LetterRange> letterRangeSet;
public String getLetterName() {
return letterName;
}
public void setLetterName(String letterName) {
this.letterName = letterName;
}
public Set<LetterRange> getLetterRangeSet() {
return letterRangeSet;
}
public …Run Code Online (Sandbox Code Playgroud) 输入= M+a?i*n S=t
期望值= M a i n s t
每个角色从比较输入与字符的下面的列表,如果发现有空格替换它和输出中应expectedValue。
Java-8提供了对数组求和的流,如下所示,流是否支持product?如果蒸汽不支持,还有其他方法来计算产品而无需手动编写循环吗?
int[] a = {1, 2, 3, 4};
int sum = IntStream.of(a).sum();
Run Code Online (Sandbox Code Playgroud) java ×10
java-8 ×10
java-stream ×2
lambda ×2
abstract ×1
algorithm ×1
collections ×1
collectors ×1
exception ×1
invocation ×1
java-7 ×1
java-time ×1
optimization ×1
reflection ×1