Tag: java 8

从Java FX中获取给定日期的周数

我有一个javafx.scene.control.DatePicker。 我想从所选日期中提取(区域设置)周数。 到目前为止,我还没有找到解决方案,我不想编写自己的算法。 我使用Java8并希望它可以在新的java时间库中使用。

为什么嵌套类型看不到generics类型参数的注释?

我没有得到以下代码的行为: https : //gist.github.com/tomaszalusky/3e3777b4fd0c6096f3f707bb19b50b52 – 请参阅embedded: import java.lang.reflect.*; import java.util.*; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; public class AnnotationOnTypeArgument { @Target({ElementType.FIELD,ElementType.PARAMETER,ElementType.METHOD,ElementType.TYPE_USE}) @Retention(RetentionPolicy.RUNTIME) public @interface Anno { } interface Nested { } Toplevel toplevel; Nested nested; public static void main(String[] args) throws Exception { print(AnnotationOnTypeArgument.class.getDeclaredField(“toplevel”)); print(AnnotationOnTypeArgument.class.getDeclaredField(“nested”)); } private static void print(Field field) { AnnotatedType […]

Java“空白的最终字段可能尚未初始化”Anonymous Interface vs Lambda Expression

我最近遇到了错误消息“空白的最终字段obj可能尚未初始化”。 通常情况下,如果您尝试引用可能尚未分配给值的字段。 示例类: public class Foo { private final Object obj; public Foo() { obj.toString(); // error (1) obj = new Object(); obj.toString(); // just fine (2) } } 我用Eclipse。 在第(1)我得到错误,在第(2)一切正常。 到目前为止这是有道理的。 接下来,我尝试在构造函数内创建的匿名接口中访问obj 。 public class Foo { private Object obj; public Foo() { Runnable run = new Runnable() { public void run() { obj.toString(); […]

本地类可以访问java 8中的非final变量

在Java 8之前,我们无法在本地类中使用非final变量。 但是现在他们允许最终以及有效的决赛(谁的价值观没有改变),可以由当地的class级推荐。 我所知道的(如果我错了,请纠正我),他们不支持引用非最终值,因为可以更改值。 那么,他们现在如何支持它以及之前为什么不支持它。

为什么必须为方法引用显式指定类/对象名?

当我想引用当前作用域中的方法时,我仍然需要在:: operator之前指定类名(对于静态方法)。 例如,我需要写: import java.util.stream.Stream; public class StreamTest { public static int trimmedLength(String s) { return s.trim().length(); } public static void main(String[] args) { System.out.println(Stream.of(” aaa “, ” bb “, ” c “) .mapToInt(StreamTest::trimmedLength).sum()); } } 这不是一个大问题,但有时看起来过于拥挤静态方法,因为类名可能很长。 如果编译器允许我简单地编写::trimmedLength那将是很好的: public static void main(String[] args) { System.out.println(Stream.of(” aaa “, ” bb “, ” c “) .mapToInt(::trimmedLength).sum()); } […]

无法在JRE 8中加载字体

我无法从JRE 8中的S3 Inputstream加载字体。如果系统安装了JRE 7,JDK 7甚至JDK 8,我就没有问题。 val fontInputStream = s3Client.getObject(bucketName, objectKey).getObjectContent val customFont = Font.createFont(Font.TRUETYPE_FONT, fontInputStream).deriveFont(Font.TRUETYPE_FONT, 20F) 我得到的错误是 Exception in thread “main” java.io.IOException: Problem reading font data. at java.awt.Font.createFont0(Font.java:1000) at java.awt.Font.createFont(Font.java:877) at Main$.delayedEndpoint$Main$1(Main.scala:31) at Main$delayedInit$body.apply(Main.scala:11) at scala.Function0$class.apply$mcV$sp(Function0.scala:40) at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12) at scala.App$$anonfun$main$1.apply(App.scala:76) at scala.App$$anonfun$main$1.apply(App.scala:76) at scala.collection.immutable.List.foreach(List.scala:381) at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35) at scala.App$class.main(App.scala:76) at Main$.main(Main.scala:11) at Main.main(Main.scala) 我试图将输入流加载到临时文件,但它没有帮助。 我还试图直接从本地文件加载字体,但我得到了与获取字体元数据不同的错误。 […]

为什么我的字符串操作使用lambda表达式很慢?

方法将逗号分隔的单词作为String并以逗号分隔的单词返回String ,其中包含自然排序顺序的单词,不包含任何4个字母的单词,包含UPPER大小写中的所有单词,不包含重复项。 与第二种方法相比,第一种方法相当慢。 你能帮我理解为什么以及如何改进我的方法? 方法1: public String stringProcessing(String s){ Stream tokens = Arrays.stream(s.split(“,”)); return tokens.filter(t -> t.length() != 4) .distinct() .sorted() .collect(Collectors.joining(“,”)).toUpperCase(); } 方法2: public String processing(String s) { String[] tokens = s.split(“,”); Set resultSet = new TreeSet(); for(String t:tokens){ if(t.length() != 4) resultSet.add(t.toUpperCase()); } StringBuilder result = new StringBuilder(); resultSet.forEach(key -> { result.append(key).append(“,”); }); […]

如何对Stream.findNth()进行编码?

与Stream.findFirst()类似,有没有办法编写Stream.findNth() ? 我正在通过重写一些遗留代码来练习Java 8。 而且,我想知道如何使用Stream API编写以下函数。 static curPipeNumber = 0; /** skipToEntry() updates ‘curPipeNumber’ to ‘pipeNumber’ and returns the first byte position of the word before the (‘pipeNumber’)-th pipe. * It does so by reading and ignoring unnecessary bytes from the buffer. * eg, */ static int skipToEntry(byte[] buf, int len, int nextByteToBeRead, int pipeNumber) […]

为什么这在Java7中编译而在Java8中不编译?

generics是棘手的。 看起来它们在不同版本的Java中被区别对待。 此代码在Java 7中成功编译,无法使用Java 8进行编译。 import java.util.EnumSet; public class Main { public static void main(String[] args) { Enum foo = null; tryCompile(EnumSet.of(foo)); } static <C extends Enum & Another> void tryCompile(Iterable i) {} static interface Another {} } 这是来自Java 8的错误消息。我用这个来编译它: http : //www.compilejava.net/ /tmp/java_A7GNRg/Main.java:6: error: method tryCompile in class Main cannot be applied to given […]

无法使用Java 8中的DateTimeFormatter和ZonedDateTime从TemporalAccessor获取ZonedDateTime

我最近转向Java 8,希望能更轻松地处理本地和分区时间。 但是,在我看来,在解析一个简单的日期时,我面临一个简单的问题。 public static ZonedDateTime convertirAFecha(String fecha) throws Exception { DateTimeFormatter formatter = DateTimeFormatter.ofPattern( ConstantesFechas.FORMATO_DIA).withZone( obtenerZonaHorariaServidor()); ZonedDateTime resultado = ZonedDateTime.parse(fecha, formatter); return resultado; } 就我而言: fecha是’15 / 06/2014′ ConstantesFechas.FORMATO_DIA是’dd / MM / yyyy’ obtenerZonaHorariaServidor返回ZoneId.systemDefault() 所以,这是一个简单的例子。 但是,解析会抛出此exception: java.time.format.DateTimeParseException: Text ’15/06/2014′ could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {},ISO resolved to 2014-06-15 of […]