Common mistakes summary (1)
1. Arrays.asList and java.util.ArrayList
1. Common code:
// convert array to set int [] intArray = new int[]{1,2,3,4}; List<int[]> asList = Arrays.asList(intArray);
At this time, an ArrayList is used in the conversion process of Arrays.asList, but this ArrayList is not a java.util.ArrayList but a static class
inside Arrays. 2.java.util.Arrays.ArrayList (the private internal static class of Arrays)
source code analysis
// asList method in Arrays class public static <T> List<T> asList(T... a) { return new ArrayList<T>(a); } // The above ArrayList is a static class defined inside Arrays, not java.util.ArrayList private static class ArrayList<E> extends AbstractList<E> implements RandomAccess, java.io.Serializable { private static final long serialVersionUID = -2764017481108945198L; private final E[] a; ArrayList(E[] array) { if (array==null) throw new NullPointerException(); a = array; } } // This ArrayList inherits AbstractList // while AbstractList implements List // so asList method can return ArrayList to List public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> { /** * Sole constructor. (For invocation by subclass constructors, typically * implicit.) */ protected AbstractList() { } }
Common method
// The defined private inner static class supports common method calls, basically equivalent to java.util.ArrayList private static class ArrayList<E> extends AbstractList<E> implements RandomAccess, java.io.Serializable { private static final long serialVersionUID = -2764017481108945198L; private final E[] a; ArrayList(E[] array) { if (array==null) throw new NullPointerException(); a = array; } // Note that size here is the number of arrays, not the format of the elements in the array // int [] intArray = new int[]{1,2,3,4}; // List<int[]> asList = Arrays.asList(intArray); // System.out.println(asList.size()); // output result is 1 instead of 4 public int size() { return a.length; } public Object[] toArray() { return a.clone(); } public <T> T[] toArray(T[] a) { int size = size(); if (a.length < size) return Arrays.copyOf(this.a, size, (Class<? extends T[]>) a.getClass()); System.arraycopy(this.a, 0, a, 0, size); if (a.length > size) a[size] = null; return a; } public E get(int index) { return a[index]; } public E set(int index, E element) { E oldValue = a[index]; a[index] = element; return oldValue; } public int indexOf(Object o) { if (o==null) { for (int i=0; i<a.length; i++) if (a[i]==null) return i; } else { for (int i=0; i<a.length; i++) if (o.equals(a[i])) return i; } return -1; } public boolean contains(Object o) { return indexOf(o) != -1; } }
3. Conversion with java.util.ArrayList
List<Integer> arrayList = new ArrayList<Integer>(Arrays.asList(integerArray));
4.java.util.Arrays.ArrayList.size()方法
// The parameter of asList is T generic type, int is the basic data type, // but int[] array is generic int [] intArray = new int[]{1,2,3,4}; List<int[]> asList = Arrays.asList(intArray); System.out.println(asList.size()); // 1 // The parameter of asList is T ... a , multiple generic T // And Integer is a reference data type, which is parsed here as multiple parameters are passed in Integer [] integerArray = new Integer[]{1,2,3,4}; List<Integer> asList2 = Arrays.asList(integerArray); System.out.println(asList2);// 4
2. Determine whether a set contains an element
1. Common code
Set<String> set = new HashSet<String>(Arrays.asList(arr)); return set.contains(targetValue);
Can be changed to:
// Because java.util.Arrays.ArrayList has the contain method, there is no need to convert it to Set Arrays.asList(arr).contains(targetValue);
or
for(String s: arr){ if(s.equals(targetValue)) // Objects.equals(s,targetValue) return true; } return false;
Three, delete elements in the List
1. Three ideas
List<String> listString = new ArrayList<String>(Arrays.asList("a","b","c","d")); // There will be no exception after deleting the element, but it should be noted that the index of the original element will change after deleting the element. For example, after deleting a, the position of b will become 0 at this time; it is impossible to delete b at this time. Because index has become 1. That is: deleting a specific element can for(int index = 0 ; index < listString.size() ; index ++){ if("a".equals(listString.get(index))){ listString.remove(index); } } for(String str : listString){ if("a".equals(str)){ listString.remove(str); } } // use this method to delete Iterator<String> iterator = listString.iterator(); while(iterator.hasNext()){ if("a".equals(iterator.next())){ iterator.remove(); } }
Blog reference: Summary of the method of cyclically deleting elements in
a list in JAVA
4. HashMap and hashtable
Hashtable
hashMap thread unsafe hashtable
thread safety , if the specific generic implementation is not specified, when adding, deleting, and modifying the collection, the type of 1. Code Sample
public class ListMainTest { public static void add(List list, Object o) { list.add(o); } public static void main(String[] args) { List<String> list = new ArrayList<String>(); add(list, 10); String s = list.get(0); } }
An exception will be thrown:
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
2. Exception analysis
For list.get(0), the result at this time is Object, and the Object at this time is copied through the Integer type, but the type of the accepted value is defined as String, and the type corresponds to the error;
3. Summary of the problem
Using the Set<?> method can play a role in type verification during operation , so as to avoid the above problems
6. Access rights
Programming habits, usually when defining an interface or service, it is easy to directly public;
according to the coding standard , the user's access rights should be restricted, and the default can be directly
7. ArrayList vs. LinkedList
array; linked list
is easy to find, It is not conducive to additions, deletions and changes; the latter is the opposite;
ArrayList implements RandomAccess, and uses for, random access when traversing a large amount of data; the latter, implements Deque doubly linked list, uses Iterator when
traversing
.