Java: checked vs unchecked exception explanation

I have read multiple posts on StackOverFlow about checked vs unchecked exceptions. I'm honestly still not quite sure how to use them properly. Joshua Bloch in "Effective Java" said that Use checked exceptions for recoverable conditions and runtime exceptions for programming errors (Item 58 in 2nd edition) Let's see if I understand this correctly. Here is my understandin

Java:检查与未经检查的异常说明

我已经阅读了StackOverFlow中有关检查与未检查异常的多篇文章。 我仍然不确定如何正确使用它们。 Joshua Bloch在“Effective Java”中表示 对可恢复条件和运行时异常使用已检查的异常进行编程错误(第2版中的项目58) 让我们看看我是否正确理解这一点。 这是我对被检查的异常的理解: try{ String userInput = //read in user input Long id = Long.parseLong(userInput); }catch(NumberFormatException e){

How to convert a Map to List in Java?

What is the best way to convert a Map<key,value> to a List<value> ? Just iterate over all values and insert them in a list or am I overlooking something? List<Value> list = new ArrayList<Value>(map.values()); 假设: Map<Key,Value> map; The issue here is that Map has two values (a key and value), while a List only has one value (an element). Therefore, the best t

如何在Java中将地图转换为列表?

将Map<key,value>转换为List<value>的最佳方法是什么? 只需遍历所有值并将它们插入到列表中,或者我忽略了某些内容? List<Value> list = new ArrayList<Value>(map.values()); 假设: Map<Key,Value> map; 这里的问题是Map有两个值(一个键和一个值),而一个List只有一个值(一个元素)。 因此,可以做的最好的方法是获取键或值的List 。 (除非我们做一个包装来保持键/值对)。 假设

How can I initialise a static Map?

How would you initialise a static Map in Java? Method one: static initialiser Method two: instance initialiser (anonymous subclass) or some other method? What are the pros and cons of each? Here is an example illustrating two methods: import java.util.HashMap; import java.util.Map; public class Test { private static final Map<Integer, String> myMap = new HashMap<Integer, St

我怎样才能初始化一个静态地图?

你将如何初始化Java中的静态地图? 方法一:静态初始化器 方法二:实例初始化(匿名子类)或其他方法? 每个的优缺点是什么? 以下是一个说明两种方法的示例: import java.util.HashMap; import java.util.Map; public class Test { private static final Map<Integer, String> myMap = new HashMap<Integer, String>(); static { myMap.put(1, "one"); myMap.put(2, "two");

How to efficiently iterate over each entry in a 'Map'?

If I have an object implementing the Map interface in Java and I wish to iterate over every pair contained within it, what is the most efficient way of going through the map? Will the ordering of elements depend on the specific map implementation that I have for the interface? Map<String, String> map = ... for (Map.Entry<String, String> entry : map.entrySet()) { System.out.prin

如何有效地迭代'Map'中的每个条目?

如果我有一个在Java中实现Map接口的对象,并且我希望迭代其中包含的每个对,那么通过该映射的最有效方式是什么? 元素的排序是否依赖于我为界面设计的特定映射实现? Map<String, String> map = ... for (Map.Entry<String, String> entry : map.entrySet()) { System.out.println(entry.getKey() + "/" + entry.getValue()); } 总结其他答案并将其与我所知道的结合起来,我找到了10个主要方法来做到这一点

Performance considerations for keySet() and entrySet() of Map

All, Can anyone please let me know exactly what are the performance issues between the 2? The site : CodeRanch provides a brief overview of the internal calls that would be needed when using keySet() and get(). But it would be great if anyone can provide exact details about the flow when keySet() and get() methods are used. This would help me understand the performance issues better. First

Map的keySet()和entrySet()的性能注意事项

所有, 任何人都可以让我知道2之间的性能问题究竟是什么? 该站点:CodeRanch提供了使用keySet()和get()时需要的内部调用的简要概述。 但是,如果任何人都可以在使用keySet()和get()方法时提供有关流程的确切细节,那将是非常好的。 这可以帮助我更好地理解性能问题。 首先,这完全取决于您正在使用哪种类型的地图。 但是,由于JavaRanch线程讨论HashMap,我假定这就是你所指的实现。 并且让我们假设您正在讨论

Android SIP registration failed (

Here is my registration code: protected void initializeManagerOpen(){ consoleWrite("initializeOpen"); if(mSipManager==null) { return; } SipProfile.Builder builder; try { builder = new SipProfile.Builder("13", "10.0.0.4"); builder.setPassword("13"); builder.setPort(5062); builder.setProtocol("UDP"); mSipProfile = builder.buil

Android SIP注册失败(

这是我的注册码: protected void initializeManagerOpen(){ consoleWrite("initializeOpen"); if(mSipManager==null) { return; } SipProfile.Builder builder; try { builder = new SipProfile.Builder("13", "10.0.0.4"); builder.setPassword("13"); builder.setPort(5062); builder.setProtocol("UDP"); mSipProfile = builder.build();

JavaFX video not playing

I followed some tutorials about combining JavaFX with Swing (JFrame) to play a video, however all I get is a black screen where the video is supposed to be without any actual content playing, No errors are reported either. What am I doing wrong here and why wont the video play? I tried several .flv videos, none of them will start playing (they do play when I open them in my browser) I'm

JavaFX视频不能播放

我跟着一些关于将JavaFX和Swing结合起来的教程(JFrame)来播放视频,然而我所得到的只是一个黑屏,视频应该没有任何实际的内容播放,也没有错误报告。 我在这里做错了什么,为什么不玩视频游戏? 我尝试了几个.flv视频,他们都不会开始播放(当我在浏览器中打开它们时,它们会播放) 我在Windows 8.1 N Pro上运行jre7和jdk1.7.0_45,并安装了K-lite完整编解码器包 编辑:在jewelsea的评论后更新了我的代码,没有任何改

Replacing JAVA with PHP for PKCS5 encryption

I have been tasked with replacing a legacy java system with something which runs PHP. I am getting a little stuck on replacing the java cryptography with PHP code. cipherAlgorythm = "PBEWithMD5AndDES"; cipherTransformation = "PBEWithMD5AndDES/CBC/PKCS5Padding"; PBEParameterSpec ps = new javax.crypto.spec.PBEParameterSpec(salt, iterations); SecretKeyFactory kf =

用PHP替换JAVA以实现PKCS5加密

我的任务是用运行PHP的东西替换遗留的java系统。 我正在用PHP代码取代Java加密技术。 cipherAlgorythm = "PBEWithMD5AndDES"; cipherTransformation = "PBEWithMD5AndDES/CBC/PKCS5Padding"; PBEParameterSpec ps = new javax.crypto.spec.PBEParameterSpec(salt, iterations); SecretKeyFactory kf = SecretKeyFactory.getInstance(cipherAlgorythm); SecretKey key = kf.generateSecret(new

Common Gotchas

In the same spirit of other platforms, it seemed logical to follow up with this question: What are common non-obvious mistakes in Java? Things that seem like they ought to work, but don't. I won't give guidelines as to how to structure answers, or what's "too easy" to be considered a gotcha, since that's what the voting is for. See also: Perl - Common gotchas .N

共同的问题

本着其他平台的精神,跟进这个问题似乎是合乎逻辑的:Java中常见的非显而易见的错误是什么? 看起来他们应该工作的东西,但不要。 我不会给出如何构建答案的指导方针,也不会给出指导方针,因为这就是投票的目的。 也可以看看: Perl - 常见的陷阱 .NET - 共同的陷阱 比较使用==而不是.equals()的对象的等.equals() - 对于基元的行为完全不同。 当"foo" == "foo"但new String("foo"

How do I compare strings in Java?

I've been using the == operator in my program to compare all my strings so far. However, I ran into a bug, changed one of them into .equals() instead, and it fixed the bug. Is == bad? When should it and should it not be used? What's the difference? == tests for reference equality (whether they are the same object). .equals() tests for value equality (whether they are logically &q

如何比较Java中的字符串?

我一直在使用我的程序中的==运算符来比较我所有的字符串。 但是,我遇到了一个bug,将其中一个改为.equals() ,然后修复了这个bug。 是==不好? 它应该什么时候它应该不被使用? 有什么不同? ==测试引用相等(不管它们是否是同一个对象)。 .equals()测试值是否相等(不管它们在逻辑上是否相等)。 Objects.equals()在调用.equals()之前检查空值,因此您不必(可从JDK7获得,也可在Guava中获得)。 因此,如果要