1. entrySet问题
如果映射表的类型不确定,需要把entrySet()的返回值转换为Set<Map.Entry>,否则编译出错。
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Test2 {
public static final void main(String[] args) {
Map<String, String> map = new HashMap();
for (Map.Entry entry : map.entrySet()) {
System.out.println(entry.getKey() + "," + entry.getValue());
}
Map map2 = map;
for (Map.Entry entry : (Set<Map.Entry>)map2.entrySet()) {
System.out.println(entry.getKey() + "," + entry.getValue());
}
System.out.println("Hello, world!");
}
}
map2.entrySet()前的类型转换不能省略。
2. 泛型T通过返回值的类型进行判定,有时会导致编译错误。
例如Collections.emptyList()的返回值类型为List<T>
public List<Integer> getID(int i) {
return (i == 0) ? (List)Collections.emptyList() : new ArrayList<Integer>();
}
这里的Collections.emptyList()前的类型转换不能省略。
3. 从转储(Dump)文件中调试并除错
http://www.ibm.com/developerworks/cn/java/j-memoryanalyzer/
4. 生产环境jvm内存运行1小时,就接近xmx,系统奇慢。
http://www.iteye.com/topic/1116650?page=5
5. 远程调试Java程序
你现在可以利用下面的命令行用JDB连接:
jdb -attach 8000
或者在Windows上:
jdb -connect com.sun.jdi.SocketAttach:port=8000
Java调试——回归基础
http://www.infoq.com/cn/articles/basic-java-debugging
使用 Eclipse 远程调试 Java 应用程序
http://www.ibm.com/developerworks/cn/opensource/os-eclipse-javadebug/
6. 调整AWT窗口客户区大小
如果用setSize,只能修改整个窗口的大小(包括标题栏)
可以先创建一个Panel(或者是Canvas,Applet),添加到Frame,然后用
setPreferredSize(new Dimension(800, 600))
修改Panel的Preferred大小,再用
frame.add(panel); //如果是JFrame则是.getContentPane().add()
frame.pack();
让Frame自己计算大小。
(有些控件如Button可以不用setPreferredSize,
因为它自己会自动扩大成按钮文本的大小)
由于上面的Panel被套在里面,所以一开始是无法监听键盘事件(除非用鼠标点击),
所以最好在setVisible后让其马上获取焦点。
frame.setVisible(true);
panel.requestFocus();
7. 取消AWT窗口的最大化和添加关闭事件(用于游戏窗口)
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setVisible(true);
8. -XX:+ShowMessageBoxOnError
http://www.iteye.com/topic/1119313?page=2
http://www.oracle.com/technetwork/java/javase/clopts-139448.html#gbmui
9. 异常的性能问题
http://www.iteye.com/topic/857722
http://www.blogjava.net/stone2083/archive/2010/07/09/325649.html
10. JPEGCodec编译错误
http://rxxluowei.iteye.com/blog/671893
http://blog.youkuaiyun.com/defonds/article/details/6314723
删除JRE,然后再次添加。
11. 收录各种猥琐的Java笔试/面试题目
http://blog.youkuaiyun.com/smcwwh/article/details/7315041
12. asArray()类型转换异常问题:
String files [] = (String []) arrList.toArray ();会抛异常。
解决方法是改为使用asArray()的带参数重载版本
String files [] = (String []) arrList.toArray (new String [arrList.size ()]);
见:
http://www.coderanch.com/t/378558/java/java/Class-Cast-exception-ArrayList-toArray
13. SIGTERM信号处理
kill -15
Runtime.getRuntime().addShutdownHook()
http://www.iteye.com/topic/1122093?page=2
(20120617)
14. 讲解如何在C/C++中调用Java的方法
http://tech.ccidnet.com/art/297/20050413/237901_1.html
http://docs.oracle.com/javase/6/docs/technotes/guides/jni/spec/jniTOC.html
(20131202)
15. Applet改写为Application问题
(参考自
https://github.com/Amarang/RPGgit
)
* getDocumentBase()会抛异常,需要做一些处理,例如(用于复杂目录树,不能使用getClass()的情况下)
public Image getImage(URL url, String name) {
if (!isApplet) {
try {
return ImageIO.read(new File(".", name));
} catch (IOException e) {
e.printStackTrace();
}
return null;
} else {
return super.getImage(url, name);
}
}
* main函数书写
private boolean isApplet = true;
public static void main(String[] args) {
Frame frame = new Frame();
XXXApplet app = new XXXApplet();
app.isApplet = false;
app.init();
frame.add(app);
frame.setSize(800, 600);
frame.setVisible(true);
}
* 读取简单文件流(使用getClass(),无需判断是否为Applet)
InputStream is = getClass().getClassLoader().getResourceAsStream(fileName);
* 读取图片或音频(单一目录,使用URL,无需判断是否为Applet)
URL soundURL = getClass().getResource("music/glory.wav");
intro = Applet.newAudioClip(soundURL);
(20140610:)
16. BufferdInputStream方法出现异常:Resetting to invalid mark
在构造函数参数中指定更大的缓存大小(默认为8K)
//Exception in thread "main" java.io.IOException: Resetting to invalid mark
InputStream inputStream = new BufferedInputStream(new FileInputStream(filename), 1024 * 1024 * 5);
(20141211)
17. 避免Eclipse的jsp引用搜索,提高搜索速度
Eclipse预设:Window->Preferences->Web->JSP Files->Search->Include JSP matches in Java searches.
去掉勾选
(20150106)
18. SWT中List控件不需要长按Ctrl键实现多选
方法是添加SWT.SIMPLE风格位,例如:
listName = new List(compositeBody, SWT.BORDER | SWT.MULTI | SWT.SIMPLE | SWT.H_SCROLL | SWT.V_SCROLL);
参考:
http://bbs.youkuaiyun.com/topics/320187649?list=lz
或参考List的实现(OS.LBS_EXTENDEDSEL表示需要按Ctrl键才能多选)
int widgetStyle () {
int bits = super.widgetStyle () | OS.LBS_NOTIFY | OS.LBS_NOINTEGRALHEIGHT;
if ((style & SWT.SINGLE) != 0) return bits;
if ((style & SWT.MULTI) != 0) {
if ((style & SWT.SIMPLE) != 0) return bits | OS.LBS_MULTIPLESEL;
return bits | OS.LBS_EXTENDEDSEL;
}
return bits;
}
(20150320)
19. maven用法(mvn命令行)
(1) 复制依赖jar:
mvn dependency:copy-dependencies -DoutputDirectory=lib -DincludeScope=compile
参考:
http://www.iteye.com/problems/56981
(2) 打印依赖树
mvn dependency:tree
(3) 编译
mvn compile
(4) 打包jar
mvn package
(5) 插件帮助
mvn xxx:help
(2015/04/16)
20. 从v2.3.0开始jogl的javax包名改变(查看jogl-all.jar)
旧项目如果要兼容,需要使用v2.2.4或之前版本的jogl
参考:
http://stackoverflow.com/questions/7210194/where-can-i-find-the-package-javax-media-opengl
http://jogamp.org/deployment/archive/rc/
20161115
跳过测试
方式1:
mvn install -Dmaven.test.skip=true
方式2:在pom.xml里面配置
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
参考:
https://my.oschina.net/u/865478/blog/159657
(TODO)