Effective Java 2nd Edition, Chapter 7, Item 42
Be careful when using var args. Do not abuse var args in our custmoized methods.
1) Painful Arrays.asList for Primitive Types
package edu.xmu.guava.constant; import static org.junit.Assert.assertEquals; import java.util.Arrays; import java.util.List; import org.junit.Test; import com.google.common.collect.Lists; import com.google.common.primitives.Ints; public class VarArgsTest { @Test public void varArgsTest() { int[] array = new int[] { 1, 2, 3, 4, 5 }; List<int[]> list = Arrays.asList(array); assertEquals(1, list.size()); list = Lists.newArrayList(array); assertEquals(1, list.size()); } @Test public void varArgsTest2() { String[] array = new String[] { "1", "2", "3", "4", "5" }; List<String> list = Arrays.asList(array); assertEquals(5, list.size()); list = Lists.newArrayList(array); assertEquals(5, list.size()); } @Test public void varArgsTest3() { int[] array = new int[] { 1, 2, 3, 4, 5 }; List<Integer> list = Ints.asList(array); assertEquals(5, list.size()); } }
1> The reason why Arrays.asList returns List<int[]> instead of List<Integer> is that:
1> Before JDK 1.4, Arrays.asList(new int[]{1, 2, 3, 4, 5}); will produce a compilation error:
asList(Object[]) in Arrays can't be applied to (int[])
This is because the signature of Arrays.asList is:
public static <T> List<T> asList(T[] a);
And our passed in array is not an Object[] array.
2> But for JDK 1.5 or above, Arrays.asList supports var args, and the signature becomes:
public static <T> List<T> asList(T... a);
And our passed in array, instead of its contents, is considered as the first element in var args.
Thus, the asList(int[]{...}) returns List<int[]>
2> How can we safely handle this?
Use Ints.asList(int... backingArray) in guava.
And guava provides handful asList implementation for every primitive types:
1) Booleans.asList
2) Bytes.asList
3) Shorts.asList
4) Ints.asList
5) Longs.asList
6) Floats.asList
7) Doubles.asList
8) Chars.asList
2) Performance Penalty
1> Everytime we invoke the method who accepts var args will cause a relocation and initiation of an array.
@Test public void varArgsTest4() { String[] returnedStrs = dummyMethod("A", "B", "C"); String[] returnedStrs2 = dummyMethod("A", "B", "C"); String[] returnedStrs3 = dummyMethod("A", "B", "C"); assertNotEquals(returnedStrs, returnedStrs2); assertNotEquals(returnedStrs, returnedStrs3); } private String[] dummyMethod(String... strs) { return strs; }
We can find out in example above that everytime we invoke the same dummyMethod, a totally new String[] will be returned.
And it will affect our code's performance unintentionally.
2> How can we avoid this performance penalty and at the same time have the flexibility of var args.
If we are pretty sure that this method will be invoked within 3 params at 95% confidence level, then we can make changes below to avoid performance penalty.
private void dummyMethod() { } private void dummyMethod(String var1) { } private void dummyMethod(String var1, String var2) { } private void dummyMethod(String var1, String var2, String var3) { } private void dummyMethod(String var1, String var2, String var3, String... rest) { }
Reference Links:
1) https://stackoverflow.com/questions/2607289/converting-array-to-list-in-java/2607327#2607327
2) http://stackoverflow.com/questions/1073919/how-to-convert-int-into-listinteger-in-java
相关推荐
书名为《代码优化:有效使用内存》(Code Optimization: Effective Memory Usage),作者是Kris Kaspersky。本书是关于如何在个人计算机(PC)和Unix平台进行程序优化的指南,重点关注优化的必要性以及通过优化提高程序...
读书笔记:Effective Java中文版学习项目
读书笔记:Effective Java中文版 第2版
读书笔记:Effective Java中文版第3版笔记
读书笔记:Effective Java中文版第二版示例代码
Effective-Java:Effective Java的所有练习程序
读书笔记:Effective.Java中文版(第3版)
读书笔记:Effective Java中文版第二版示例、笔记
读书笔记:Effective Java 中文版(2版和3版)
1.GAWK:Effective AWK Programming_Edition 4.2.dvi 2.GAWK:Effective AWK Programming_Edition 4.2.html 3.GAWK:Effective AWK Programming_Edition 4.2.pdf 4.GAWK:Effective AWK Programming_Edition 4.2.txt
读书笔记:Effective Java 中文版(第2版)总结 (美)Joshua Bloch 著
* 跨平台性:Java语言可以在多种平台上运行,包括Windows、Linux、Mac OS等。 * 安全性:Java语言具有内置的安全机制,保护用户免受恶意代码的攻击。 * 可靠性:Java语言具有强大的错误处理机制,能够快速恢复程序...
C++经典书籍:Effective C++、C++标准模板库实战
《Effective Java》是Java开发领域的经典著作,由Joshua Bloch撰写,中文版第二版更是深受广大Java开发者喜爱。这本书提供了许多实用的编程实践和经验教训,帮助开发者编写出更高效、可维护的Java代码。这里我们将...
C++: Effective Modern C++ (C++ 11, C++ 14) (guide,C Programming, HTML, Javascript, Programming,all,internet, Coding, CSS, Java, PHP Vol 1) By 作者: Paul Laurence ISBN-10 书号: 1547133244 ISBN-13 书号:...
标题“effective-java.pdf”与描述“effective-java.pdf”表明本文档是关于Java编程实践的指南,且内容可能来自于一本名为《Effective Java》的书籍,该书是由Joshua Bloch编写,被广泛认为是Java编程的权威指南。...
7. **终结方法与清理器**:Java中的`finalize`方法往往不推荐使用,因为它不能保证执行,性能影响且可能导致不可预测的行为。 8. **try-with-resources**:从Java 7开始,try-with-resources语句使得资源的自动关闭...
有效的Java SE有效的Java SE 9至16 API和语言功能使您的生活更加轻松。 从Java SE 9+开始,这是Java编程语言和Java虚拟机的主要发行版。 从那时起,javaSwift适应新技术时代,我们每6个月就会看到一次,Java SE 9-16...