String array
Array to a list
String[] strings = new String[]{"111","222","333"};
//数组转list方法一
List<String> stringToList1 = Arrays.asList(strings);//数组转list
//数组转list方法二
List<String> stringToList2 = new ArrayList<>();
Collections.addAll(stringToList2,strings);
//数组转list方法三
List<String> stringToList3 = new ArrayList<>();
for(String s:strings){
stringToList3.add(s);
}
turn a string array list
List<String> list = new ArrayList<>();
list.add("aaa");
list.add("bbb");
list.add("ccc");
//list转string方法一
String[] listToString1 = list.toArray(new String[list.size()]);
//list转string方法二
String[] listToString2 = new String[list.size()];
for(int i = 0;i<list.size();i++){
listToString2[i] = list.get(i);
}
ArrayList
Queries position where the element
if(list.contains("aaa")){
int position = list.indexOf("aaa");
}
HashMap (disorder)
HashMap<String,String> map = new HashMap<>();
map.put("aaa","111");
map.put("bbb","222");
map.put("ccc","333");
map.put("ddd","444");
map.put("###","555");
for(Map.Entry<String,String> entry:map.entrySet()){
System.out.println(entry.getKey()+"===="+entry.getValue());
}
Set<String> keys = map.keySet();
for(String s:keys){
System.out.println(s);
}
Collection<String> values = map.values();
for (String s:values){
System.out.println(s);
}
LinkedHashMap (ordered)
Connection interface:
- List orderly, repeatable
ArrayList
advantages: the underlying data structure is an array, the query fast, slow additions.
Disadvantages: thread-safe, high efficiency
Vector
advantages: the underlying data structure is an array, query fast, slow additions and deletions.
Disadvantages: thread-safe, low efficiency
LinkedList
advantages: the underlying data structure is the list, slow queries, additions and deletions fast.
Disadvantages: thread-safe, high efficiency
-Set disorder, the only
HashSet
underlying data structure is a hash table. (Unordered only)
how to ensure the uniqueness of the elements?
1. relies on two methods: hashCode () and equals ()
LinkedHashSet
underlying data structure is a linked list and hash tables. (FIFO ordered insertion, the only)
1 by the ordered list of elements to ensure
2. by a hash table to ensure that the only elements
TreeSet
underlying data structure is a red-black tree. (The only orderly)
- How to ensure the elements to sort it?
Natural order
comparator sort
2. How to ensure the uniqueness of the elements?
Determined based on a comparison of the return value is 0
Map Interface
Map interface has three more important to realize categories, namely HashMap, TreeMap and HashTable.
- List itemTreeMap is ordered, HashMap and HashTable are unordered.
- Hashtable methods are synchronized, the method HashMap is not synchronized. This is the main difference between the two.
This means:
- Hashtable is thread-safe, HashMap is not thread safe.
- HashMap high efficiency, low Hashtable efficiency.
- If no synchronization requirements for compatibility with legacy code or recommended HashMap.
Hashtable view the source code can be found, in addition to the constructor, declare all public methods of the Hashtable are
synchronized keyword, while HashMap is not the source code. - Hashtable does not allow null values, HashMap allows null values (key and value are allowed)
- Different parent: Hashtable parent is Dictionary, HashMap parent is AbstractMap
Reference article:
the Java collection List, Set, and Map, etc. Detailed collection system (the most complete history)