以下代码不打印"CE"或"当前时代":
System.out.println(IsoEra.CE.getDisplayName(TextStyle.SHORT, Locale.UK)); // output: AD
System.out.println(IsoEra.CE.getDisplayName(TextStyle.FULL, Locale.UK)); // output: Anno Domini
Run Code Online (Sandbox Code Playgroud)
当然,IsoEra.CE.name()如果需要像"普通时代"或"当代时代"这样的完整显示名称,则有所帮助.我认为这有点奇怪,因为javadoc IsoEra在其类描述中明确提到术语"当前时代".它甚至不适用于根区域设置.这里的用例是为非宗教背景的客户提供服务.
这也没有帮助:
LocalDate date = LocalDate.now();
String year = date.format(DateTimeFormatter.ofPattern("G yyyy", Locale.UK)); // AD 2015
System.out.println(year);
Run Code Online (Sandbox Code Playgroud)
我找到的唯一方法是:
TextStyle style = ...;
Map<Long,String> eras = new HashMap<>();
long bce = (long) IsoEra.BCE.getValue(); // 0L
long ce = (long) IsoEra.CE.getValue(); // 1L
if (style == TextStyle.FULL) {
eras.put(bce, "Before current era");
eras.put(ce, "Current era");
} else {
eras.put(bce, "BCE");
eras.put(ce, "CE");
}
DateTimeFormatter dtf =
new …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
public static void main(String[] args) {
List<String> s = new ArrayList<String>();
s.add("kshitiz");
//This is not typesafe. It should blow up at runtime
List<Integer> i = new ArrayList(s);
System.out.println(i.get(0));
}
Run Code Online (Sandbox Code Playgroud)
这个程序运行正常,并打印kshitiz.如果我用以下内容替换最后一行,它只会失败:
System.out.println(i.get(0).getClass());
Run Code Online (Sandbox Code Playgroud)
例外:
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?
当我运行此代码时
List<int[]> list = Arrays.asList(new int[]{1, 2, 3}, new int[]{4, 5});
int[][] arr = list.stream().map(j -> j.clone()).toArray(int[][]::new);
System.out.println(Arrays.deepToString(arr));
Run Code Online (Sandbox Code Playgroud)
它按预期工作,我得到输出
[[1, 2, 3], [4, 5]]
Run Code Online (Sandbox Code Playgroud)
但是,如果我用方法引用替换lambda clone()
int[][] arr = list.stream().map(int[]::clone).toArray(int[][]::new);
Run Code Online (Sandbox Code Playgroud)
我得到一个运行时异常:
Exception in thread "main" java.lang.NoClassDefFoundError: Array
at Main.lambda$MR$main$clone$8ed4b78b$1(Main.java:14)
at Main$$Lambda$1/1160460865.apply(Unknown Source)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:512)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:502)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:576)
at java.util.stream.AbstractPipeline.evaluateToArrayNode(AbstractPipeline.java:255)
at java.util.stream.ReferencePipeline.toArray(ReferencePipeline.java:438)
at Main.main(Main.java:14)
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:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.ClassNotFoundException: Array
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method) …Run Code Online (Sandbox Code Playgroud) 我们有一个groovy单例,它使用池大小为200的PoolingHttpClientConnectionManager(httpclient:4.3.6)来处理与搜索服务的非常高的并发连接并处理xml响应.
尽管已经指定了超时,但它每月冻结一次,但在其余时间运行完全正常.
下面的常规单身人士.方法retrieveInputFromURL似乎在client.execute(get)上阻塞;
@Singleton(strict=false)
class StreamManagerUtil {
// Instantiate once and cache for lifetime of Signleton class
private static PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
private static CloseableHttpClient client;
private static final IdleConnectionMonitorThread staleMonitor = new IdleConnectionMonitorThread(connManager);
private int warningLimit;
private int readTimeout;
private int connectionTimeout;
private int connectionFetchTimeout;
private int poolSize;
private int routeSize;
PropertyManager propertyManager = PropertyManagerFactory.getInstance().getPropertyManager("sebe.properties")
StreamManagerUtil() {
// Initialize all instance variables in singleton from properties file
readTimeout = 6
connectionTimeout = 6
connectionFetchTimeout =6
// Pooling …Run Code Online (Sandbox Code Playgroud) 我有一个包含URL和电子邮件的文本文件.我需要从文件中提取所有这些内容.每个URL和电子邮件可以找到一次以上,但结果不应包含重复项.我可以使用以下代码提取所有URL:
Files.lines(filePath).
.map(urlPattern::matcher)
.filter(Matcher::find)
.map(Matcher::group)
.distinct();
Run Code Online (Sandbox Code Playgroud)
我可以使用以下代码提取所有电子邮件:
Files.lines(filePath).
.map(emailPattern::matcher)
.filter(Matcher::find)
.map(Matcher::group)
.distinct();
Run Code Online (Sandbox Code Playgroud)
我是否可以提取所有Files.lines(filePath)只读取一次返回的流的URL和电子邮件?类似于将行流分割为URL流和电子邮件流的东西.
我正在使用.txt文件读取数字BufferedReader.我想颠倒这种蒸汽中元素的顺序,这样当它们被收集时,它们将从最高到最低排列.我不想在构建数组后进行排序,因为我不知道它中可能有多少个元素,我只需要最高的N个元素.
in = new BufferedReader(reader);
int[] arr = in.lines()
.mapToInt(Integer::parseInt)
.sorted()
.limit((long) N)
.toArray();
Run Code Online (Sandbox Code Playgroud) J. Bloch在撰写的Effective Java中Java 6提到了以下内容(第17项):
如果您认为必须允许从这样的类继承,一种合理的方法是确保该类永远不会调用其任何可覆盖的方法并记录此事实.换句话说,完全 消除了类对可覆盖方法的自我使用.
第18项:
如果您使用抽象类来定义类型,那么您可以让想要添加功能的程序员别无选择,只能使用继承.结果类比包装类更强大,更脆弱.
虽然不允许接口包含方法实现,但使用接口来定义类型并不会阻止您向程序员提供实现帮助.
现在Java 8使用其默认方法的实现(使用接口中的其他方法)接口对于继承是危险的.
例如:
public inteface MyInterface{
public void compute(Object o);
public default void computeAll(List<Object> oo){
for(Object o: oo)
compute(o); //self-use
}
}
Run Code Online (Sandbox Code Playgroud)
因此,根据J. Bloch的说法,当我们尝试实现接口时,它可能会引入一些问题,因为:
覆盖这样的方法(类似于J.Bloch提供的):
public class MyInterfaceCounter implements MyInterface{
private int count = 0;
@Override
public void compute(Object o) {
count++;
}
@Override
public void computeAll(List<Object> oo){
count += oo.size(); //Damn!!
MyInterface.super.computeAll(oo);
}
}
Run Code Online (Sandbox Code Playgroud)客户端访问接口的内部,即他们必须知道默认实现.
在Java 8中如何处理它?Effective Java的规则是否适用仍然适用?
而且,我们不能将默认方法声明为final(因为我们可以对类进行声明,它会使自用对于重写器来说不太危险).
我正在编写一个可序列化的类,它带有几个参数,包括Function:
public class Cls implements Serializable {
private final Collection<String> _coll;
private final Function<String, ?> _func;
public Cls(Collection<String> coll, Function<String, ?> func) {
_coll = coll;
_func = func;
}
}
Run Code Online (Sandbox Code Playgroud)
func存储在成员变量中,因此需要可序列化.如果它们被分配的类型是可序列化的,则 Java lambdas 是可序列化的.Function如果使用lambda创建,那么确保我在构造函数中传递的最佳方法是可序列化的?
创建一个SerializableFunction类型并使用它:
public interface SerializableFunction<F, R> implements Function<F, R>, Serializable {}
....
public Cls(Collection<String> coll, SerializableFunction<String, ?> func) {...}
Run Code Online (Sandbox Code Playgroud)
问题:
coll和func参数之间存在不匹配,在func签名中声明为可序列化,但coll不是,但两者都需要可序列化才能工作.Function可序列化.在构造函数上使用类型参数:
public <F extends Function<String, …Run Code Online (Sandbox Code Playgroud)说我有一个包含元素的列表(34, 11, 98, 56, 43).
使用Java 8流,如何找到列表中最小元素的索引(例如,在这种情况下为1)?
我知道这可以在Java中轻松完成list.indexOf(Collections.min(list)).但是,我正在寻找一个类似Scala的解决方案,我们可以简单地说List(34, 11, 98, 56, 43).zipWithIndex.min._2获得最小值的索引.
有没有什么可以使用流或lambda表达式(比如Java 8特定功能)来实现相同的结果.
注意:这仅用于学习目的.我在使用Collections实用程序方法时没有任何问题.
我试图找到最优雅的方法从x开始从集合中获取n个元素.我总结的是使用流:
Set<T> s;
Set<T> subS = s.stream().skip(x).limit(n).collect(Collectors.toSet());
Run Code Online (Sandbox Code Playgroud)
这是这样做的最好方法吗?有什么缺点吗?
java-8 ×10
java ×9
java-stream ×3
generics ×1
inheritance ×1
java-time ×1
lambda ×1
serializable ×1
set ×1