Java 8的orElse没有按预期工作

考虑以下方法,如果该字段存在,则返回该字段,或者在找到该字段之前递归调用自身: private Field getField(Class clazz, String p) { Optional field = Arrays.stream(clazz.getDeclaredFields()) .filter(f -> p.equals(f.getName())) .findFirst(); return field.isPresent() ? field.get() : getField(clazz.getSuperclass(), p); } 虽然这有效,但我认为我可以缩短它: private Field getField(Class clazz, String p) { return Arrays.stream(clazz.getDeclaredFields()) .filter(f -> p.equals(f.getName())) .findFirst() .orElse(getField(clazz.getSuperclass(), p)); } 但奇怪的是, .orElse部分似乎总是被称为。 我在这里想念的是什么?

何时记录捕获的exception的堆栈跟踪

我最近询问是否报告捕获的exception的getMessage()文本 。 相当令人惊讶的是,大多数答案误解了我的问题并且认为我在询问是否报告了捕获exception的堆栈跟踪,这表明这样做被认为是常态。 所以我要问一个跟进问题。 在catchexception时,您应该或者不应该在哪种情况下报告堆栈跟踪? 通过“报告”,我包括要求日志框架为您记录堆栈跟踪 。 我不是在问一些事情 。 我在问这个报告是否应该包含堆栈跟踪。

SimpleDateFormat(“dd-MMM-YYYY”)印刷年份提前一年

我在我的代码中使用SimpleDateFormat(“dd-MMM-YYYY”),它输出错误。 SimpleDateFormat dateFormat = new SimpleDateFormat(“dd-MMM-YYYY”); System.out.println(“Actual date : “+new Date()+” after Formatting : “+ dateFormat.format(new Date())); 以上代码给出: 实际日期:Tue Dec 30 13:51:06 IST 2014格式化后:2015年12月30日 以上代码为打印日期,年份为1年。 此问题仅适用于2014年12月28日至31日的日期。 提前致谢。 –Ajay

如何inheritance静态字段并改变它的值?

我正在开发程序/游戏,我有静态实用程序类和params。 class ParamsGeneral { public static final int H_FACTOR = 100; public static int MAX_SCORE = 1000; … } 然后我需要在某些特定情况下覆盖这些值,例如在分数有限的地图上播放。 所以我做了以下事情: class ParamsLimited extends ParamsGeneral { public static int MAX_SCORE = 500; // other params stay same } 预期用途如下: class Player { ParamsGeneral par; public Player() { if(onLimitedMap()){ par = new ParamLimited(); } } public […]

为什么CompletableFuture.supplyAsync成功随机次数?

我是Java 8中lambdas和异步代码的新手。我不断得到一些奇怪的结果…… 我有以下代码: import java.util.concurrent.CompletableFuture; public class Program { public static void main(String[] args) { for (int i = 0; i < 100; i++) { String test = "Test_" + i; final int a = i; CompletableFuture cf = CompletableFuture.supplyAsync(() -> doPost(test)); cf.thenRun(() -> System.out.println(a)) ; } } private static boolean doPost(String t) { System.out.println(t); […]

Base64:java.lang.IllegalArgumentException:非法字符

我正在尝试在用户注册后发送确认电子邮件。 我正在使用JavaMail库和Java 8 Base64 util类。 我正在以下列方式编码用户电子邮件: byte[] encodedEmail = Base64.getUrlEncoder().encode(user.getEmail().getBytes(StandardCharsets.UTF_8)); Multipart multipart = new MimeMultipart(); InternetHeaders headers = new InternetHeaders(); headers.addHeader(“Content-type”, “text/html; charset=UTF-8”); String confirmLink = “Complete your registration by clicking on following”+ “\nlink”; MimeBodyPart link = new MimeBodyPart(headers, confirmLink.getBytes(“UTF-8”)); multipart.addBodyPart(link); 其中confirmationURL是: private final static String confirmationURL = “http://localhost:8080/project/controller?command=confirmRegistration&ID=”; 然后以这种方式在ConfirmRegistrationCommand中对此进行解码: String encryptedEmail = request.getParameter(“ID”); String […]

如何在Android onMediaButtonEvent中收听“ACTION_DOWN”(按下键)事件,以便测量时间?

我试着获得时间System.currentTimeMillis() ,而不是用户按下媒体按钮。 但是当媒体按钮再次上升时执行回调action == KeyEvent.ACTION_UP 。 我不想在我的解决方案中使用BroadcastReceiver。 这是我的代码: MediaSession audioSession = new MediaSession(getApplicationContext(), “TAG”); audioSession.setCallback(new MediaSession.Callback() { @Override public boolean onMediaButtonEvent(final Intent mediaButtonIntent) { String intentAction = mediaButtonIntent.getAction(); if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) { KeyEvent event = (KeyEvent)mediaButtonIntent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); if (event != null) { int action = event.getAction(); if (action == KeyEvent.ACTION_DOWN) { long stopTimeOfGame_millis = System.currentTimeMillis(); UtilsRG.info(“time stopped: […]

如何在android中设置TimeZone

在这里这样做,但仍然没有翻译成所需的: String dtc = “2014-04-02T07:59:02.111Z”; SimpleDateFormat readDate = new SimpleDateFormat(“yyyy-MM-dd’T’HH:mm:ss.SSS’Z'”); Date date = null; try { date = readDate.parse(dtc); Log.d(“myLog”, “date “+date); } catch (ParseException e) { Log.d(“myLog”, “dateExcep ” + e); } SimpleDateFormat writeDate = new SimpleDateFormat(“dd.MM.yyyy, HH.mm”); writeDate.setTimeZone(TimeZone.getTimeZone(“GMT+04:00”)); String s = writeDate.format(date); 在变量“s”的输出仍然给出时间07:59:02,我想提前+4小时,即11:59:02

在Java中检测并操作键盘方向键

G’day全部, 我有一个控制台项目,用户按下键盘方向键(非数字键盘)来移动头像。 我编码很难检查这些键的按键。 在Pascal中,使用“readkey”和代码很容易,例如,#80用于向下按键。 但是,我很难理解如何在Java中实现相同的function,尽管我认为我理解使用System.in和BufferedInputStream。 任何人都可以帮我吗? 非常感谢您的想法或提示。

Java有懒惰的评估吗?

我知道Java在这种情况下有智能/懒惰的评估: public boolean isTrue() { boolean a = false; boolean b = true; return b || (a && b); // (a && b) is not evaluated since b is true } 但是关于: public boolean isTrue() { boolean a = isATrue(); boolean b = isBTrue(); return b || a; } 即使isBTrue()返回true, isBTrue()是否被调用?