我正在尝试将应用程序从JDK7升级到JDK8,它使用了sun.security.*包中的以下类
sun.security.x509.X509CertImpl
sun.security.pkcs11.SunPKCS11
sun.security.util.DerOutputStream
sun.security.util.DerValue
sun.security.util.ObjectIdentifier
sun.security.pkcs.PKCS10
sun.security.x509.X500Name
sun.security.pkcs11.SunPKCS11
sun.security.pkcs11.wrapper.CK_TOKEN_INFO
sun.security.pkcs.PKCS10
Run Code Online (Sandbox Code Playgroud)
这些类的使用会在所有情况下生成警告,除非sun.security.pkcs.PKCS10导致编译错误,因为此类不再存在.它似乎已经转移到另一个包sun.security.pkcs10.PKCS10.
虽然我可以简单地更改此包名并忽略其他sun.security类生成的警告,但我知道您不应该在sun.security包中使用类.如何使用JDK8公共API中的等效类替换这些类?
我已经使用Java 8几个月了,我已经开始使用Lambda表达式,这在某些情况下非常方便.但是,我常常遇到一些问题,要对使用Lambda的代码进行单元测试.
以下面的伪代码为例:
private Bar bar;
public void method(int foo){
bar.useLambda(baz -> baz.setFoo(foo));
}
Run Code Online (Sandbox Code Playgroud)
一种方法是仅仅验证条形码上的呼叫
verify(bar).useLambda(Matchers.<Consumer<Baz>>.any());
Run Code Online (Sandbox Code Playgroud)
但是,通过这样做,我不测试Lambda的代码.
另请注意,我无法使用方法替换Lambda并使用方法引用:
bar.useLambda(This::setFooOnBaz);
Run Code Online (Sandbox Code Playgroud)
因为我不会在那个方法上有foo.或者至少这是我的想法.
你以前遇到过这个问题吗?如何测试或重构我的代码以正确测试?
编辑
因为我编码的是单元测试,我不想实例化bar,而我将使用mock.所以我将无法验证baz.setFoo通话.
我在Java 8中编写了一些使用时间算术的代码.我意识到我可以在不同的方面实施.让我们看看下面的简单代码.当然它是相同的结果,但我混淆了哪种方式主要应用或最有效地在Java 8中进行算术运算?
LocalTime time = LocalTime.now();
// 1st way
LocalTime plusOp = time.plus(Duration.ofMinutes(10L));
// 2nd way
LocalTime plusOp2 = time.plus(10L, ChronoUnit.MINUTES);
System.out.println(plusOp);
System.out.println(plusOp2);
// 3. way simply
time.plusMinutes(10L);
Run Code Online (Sandbox Code Playgroud)
提前致谢.
import java.util.function.*;
class Test {
void test(int foo, Consumer<Integer> bar) { }
void test(long foo, Consumer<Long> bar) { }
void test(float foo, Consumer<Float> bar) { }
void test(double foo, Consumer<Double> bar) { }
}
Run Code Online (Sandbox Code Playgroud)
当我编译这个时,javac -Xlint Test.java我会得到一些警告:
Test.java:4: warning: [overloads] test(int,Consumer<Integer>) in Test is potentially ambiguous with test(long,Consumer<Long>) in Test
void test(int foo, Consumer<Integer> bar) { }
^
Test.java:6: warning: [overloads] test(float,Consumer<Float>) in Test is potentially ambiguous with test(double,Consumer<Double>) in Test
void test(float foo, Consumer<Float> bar) { } …Run Code Online (Sandbox Code Playgroud) java overloading compiler-warnings java-8 functional-interface
如何计算流过滤器的匹配?我正在尝试将以下代码重构为java8 stream:
//java7
int i = 0;
for (Node node : response.getNodes()) {
Integer id = node.getId();
if (id != null) {
node.setContent("This is the id: " + id);
i++;
}
}
//java8
response.getNodes().stream()
.filter(node -> node.getId() != null)
.forEach(node -> node.setValue("This is the id: " + node.getId()));
Run Code Online (Sandbox Code Playgroud)
我现在如何获得已应用的已过滤元素的数量?Sidequestion:在旧代码中我可以Integer id多次重复使用.如何使用流实现相同的功能?
给出以下课程:
public class FooTest {
public static class Base {
}
public static class Derived extends Base {
}
public interface Service<T extends Base> {
void service(T value);
}
public abstract class AbstractService<T extends Derived> implements Service<T> {
public void service(T value) {
}
}
private AbstractService service;
public void bar(Base base) {
if(base instanceof Derived) {
service.service(base); // compile error at this line
}
}
}
Run Code Online (Sandbox Code Playgroud)
使用以下内容构建类时pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mgm-tp</groupId>
<artifactId>java-compiler-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build> …Run Code Online (Sandbox Code Playgroud) 我有以下课程:
public class Insurance {
...
}
Run Code Online (Sandbox Code Playgroud)
public class Customer {
private List<Insurance> insurances;
public List<Insurance> getInsurances() {
return insurances;
}
...
}
Run Code Online (Sandbox Code Playgroud)
public class CustomerRegistry {
private List<Customer> customers;
...
}
Run Code Online (Sandbox Code Playgroud)
以及这个帮助方法,它将a减少List<Predicate<T>>为单个Predicate<T>:
public Predicate<T> reducePredicates(List<Predicate<T>> predicates) {
return predicates.stream()
.reduce(Predicate::and)
.orElse(p -> true);
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是获得与过滤器列表匹配的保险列表,这些过滤器属于与过滤器列表匹配的客户.如果不清楚,下面的代码将有希望澄清.
方法在CustomerRegistry上面的类中.
public List<Insurance> findInsurances(List<Predicate<Customer>> cusPredicates,
List<Predicate<Insurance>> insPredicates) {
List<Insurance> matches = new LinkedList<>();
customers.stream()
.filter(reducePredicates(cusPredicates)
.forEach(cus -> cus.getInsurances()
.stream()
.filter(reducePredicates(insPredicates))
.forEach(cus -> matches.add(cus))) …Run Code Online (Sandbox Code Playgroud) 假设我有两个int[]数组input1和input2.我想从第一个中取正数,从第二个中取出不同的数字,将它们合并在一起,排序并存储到结果数组中.这可以使用流执行:
int[] result = IntStream.concat(Arrays.stream(input1).filter(x -> x > 0),
Arrays.stream(input2).distinct()).sorted().toArray();
Run Code Online (Sandbox Code Playgroud)
我想加快任务,所以我考虑使流并行.通常这只意味着我可以.parallel()在流构造和终端操作之间插入任何地方,结果将是相同的.IntDream.concat的JavaDoc 表示如果任何输入流是并行的,则生成的流将是并行的.所以,我认为做parallel()任何input1流或input2流或级联的流将产生相同的结果.
实际上我错了:如果我添加.parallel()到结果流,似乎输入流保持顺序.此外,我可以将输入流(它们中的任何一个或两者)标记为.parallel(),然后将结果流转换为.sequential(),但输入保持平行.所以实际上有8种可能性:input1,input2和连接流都可以是并行的:
int[] sss = IntStream.concat(Arrays.stream(input1).filter(x -> x > 0),
Arrays.stream(input2).distinct()).sorted().toArray();
int[] ssp = IntStream.concat(Arrays.stream(input1).filter(x -> x > 0),
Arrays.stream(input2).distinct()).parallel().sorted().toArray();
int[] sps = IntStream.concat(Arrays.stream(input1).filter(x -> x > 0),
Arrays.stream(input2).parallel().distinct()).sequential().sorted().toArray();
int[] spp = IntStream.concat(Arrays.stream(input1).filter(x -> x > 0),
Arrays.stream(input2).parallel().distinct()).sorted().toArray();
int[] pss = IntStream.concat(Arrays.stream(input1).parallel().filter(x -> x > 0),
Arrays.stream(input2).distinct()).sequential().sorted().toArray(); …Run Code Online (Sandbox Code Playgroud) 我们正在运行java-8-oracle.
我们六个月前搬到了java8.
在过去的几天里,我们不时收到OOME,但我们无法识别或重现问题.
当我们执行对服务器(tomcat)的调用时,我们在stacktrace上得到这个错误:
java.lang.OutOfMemoryError: Compressed class space
Run Code Online (Sandbox Code Playgroud)
重新启动服务器可以解决问题.对其他服务器的相同调用也起作用,对另一个类型的另一个调用也是如此.
在gc.log上查看时,我们看到:
2015-05-27T16:05:42.991+0000: 98774.440: [Full GC (Last ditch collection) 98774.440: [CMS: 575745K->575330K(3495936K), 0.8687777 secs] 575745K->575330K(4107008K), [Metaspace: 97940K->97940K(1396736K)], 0.8696093 secs] [Times: user=0.95 sys=0.00, real=0.88 secs]
2015-05-27T16:05:55.486+0000: 98786.935: [Full GC (Metadata GC Threshold) 98786.935: [CMS: 573414K->578735K(3495936K), 0.9372859 secs] 925046K->578735K(4107008K), [Metaspace: 99428K->99428K(1396736K)], 0.9386626 secs] [Times: user=1.01 sys=0.00, real=0.94 secs]
Run Code Online (Sandbox Code Playgroud)
jstat -gc 收益:
S0C S1C S0U S1U EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT GCT
87296.0 87296.0 0.0 3151.4 523776.0 148284.4 …Run Code Online (Sandbox Code Playgroud) Java 8使用能够动态扩展的元空间.GC将在满足空间运行时运行.这是否意味着GC永远不会在元空间上运行?
我的Java 8应用程序使用了大量内存.我想知道运行时我的元空间大小是多少.我怎么做?
我正在考虑设置maxMetaSpace.我该怎么做呢?有什么建议?
java ×10
java-8 ×10
java-stream ×3
lambda ×2
collections ×1
datetime ×1
eclipse ×1
java-7 ×1
java-time ×1
jls ×1
jvm ×1
metaspace ×1
overloading ×1
security ×1
unit-testing ×1