- Map
is an interface to the object to map key value. A map can not contain duplicate keys; each key can only be mapped to a value at most.
The same key, value coverage, when you first store the key-value pairs, null is returned
when you store the same key a second time, but with different values, the same key, value coverage occurs, the return is the key the first of the old values - Function HashMap Overview
a:添加功能
V put(K key,V value):添加元素。这个其实还有另一个功能?替换
如果键是第一次存储,就直接存储元素,返回null
如果键不是第一次存在,就用值把以前的值替换掉,返回以前的值
b:删除功能
void clear():移除所有的键值对元素
V remove(Object key):根据键删除键值对元素,并把值返回
c:判断功能
boolean containsKey(Object key):判断集合是否包含指定的键
boolean containsValue(Object value):判断集合是否包含指定的值
boolean isEmpty():判断集合是否为空
d:获取功能
Set<Map.Entry<K,V>> entrySet(): 返回一个键值对的Set集合
V get(Object key):根据键获取值
Set<K> keySet():获取集合中所有键的集合
Collection<V> values():获取集合中所有值的集合
e:长度功能
int size():返回集合中的键值对的对数
//功能测试
import java.util.HashMap;
import java.util.Map;
public class test {
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<>();
map.put("1","1");
map.put("2","2");
map.put("3","3");
map.put("4","4");
map.put("5","5");
String remove = map.remove("1");
System.out.println(remove);
map.clear();
boolean empty = map.isEmpty();
int size = map.size();
boolean b = map.containsKey("1");
boolean b1 = map.containsValue("1");
System.out.println(empty);
System.out.println(size);
System.out.println(map);
System.out.println(b);
System.out.println(b1);
}
}
- HashMap collection traversal
1) key value to find
the key to find out unified as a whole to take out into a collection, through the collection of keys, access to each valueV get(Object key) ,得到键就是找到了对应的值,键找值 Set<K> keySet() 获取键的集合 Collection<V> values() 获取值的集合
import java.util.Collection;
import java.util.HashMap;
import java.util.Set;
public class test1 {
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<>();
map.put("1","1");
map.put("2","2");
map.put("3","3");
map.put("4","4");
map.put("5","5");
Set<String> strings = map.keySet();
for (String key : strings) {
System.out.println(key+"-------"+map.get(key));
}
Collection<String> values = map.values();
System.out.println(map);
}
}
2) key-value pairs
获取所有键值对对象的集合
遍历键值对对象的集合,获取到每一个键值对对象
根据键值对对象找键和值
/*接口 Map.Entry<K, V>
K getKey ()
返回与此项对应的键。
V getValue ()
返回与此项对应的值。*/
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class test2 {
public static void main(String[] args) {
//键值对
HashMap<String, String> map = new HashMap<>();
map.put("1","1");
map.put("2","2");
map.put("3","3");
map.put("4","4");
map.put("5","5");
Set<Map.Entry<String, String>> entries = map.entrySet();
for (Map.Entry<String, String> entry : entries) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key+"-----"+value);
}
}
}
- LinkedHashMap
underlying data structure is an ordered list and hash table elements and unique
ordering of the linked list data structure elements to ensure the
uniqueness of the structure of a hash table to ensure that data from the
data structure and Map key set only relevant
import java.util.LinkedHashMap;
import java.util.function.BiConsumer;
public class test3 {
public static void main(String[] args) {
LinkedHashMap<String, String> map = new LinkedHashMap<>();
map.put("1","1");
map.put("2","2");
map.put("2","1");
map.put("5","3");
map.put("4","4");
map.put("5","5");
map.forEach(new BiConsumer<String, String>() {
@Override
public void accept(String key, String value) {
System.out.println(key+"-----"+value);
}
});
}
}
结果:
1-----1
2-----1
5-----5
4-----4
- TreeMap
data is red-black tree structure of the key, and sorting can ensure uniqueness of the key
sorting into natural ordering and sorting comparator
thread is unsafe more efficient
//键的数据结构是红黑树,可保证键的排序和唯一性
import org.map.demo.Student;
import java.util.TreeMap;
import java.util.function.BiConsumer;
public class test4 {
public static void main(String[] args) {
TreeMap<Student, String> treeMap = new TreeMap<>();
treeMap.put(new Student("张三", 23), "s001");
treeMap.put(new Student("张三", 23), "s002");
treeMap.put(new Student("张三", 23), "哈哈哈哈");
treeMap.put(new Student("张三3333", 230), "s001");
treeMap.put(new Student("张三", 23), "呵呵呵呵呵");
treeMap.put(new Student("lisi", 29), "s001");
treeMap.forEach(new BiConsumer<Student, String>() {
@Override
public void accept(Student student, String s) {
System.out.println(student.getName()+"----"+student.getAge()+s);
}
});
}
}
Collections.sort(List<T> list):
直接给定需要排序的list,然后按照自然排序进行排序。
Collections.sort(List<T> list,Comparator<? super T> c):
这个方法需要提供比较器,然后排序时会根据比较器的实现逻辑进行排序。
- HashMap and Hashtable difference between
HashMap and Hashtable distinction: the underlying data structures are the same, both hash table
HashMap: thread-safe, high efficiency allow null values and the null key.
Hashtable: thread-safe, low efficiency and does not allow null values. null key
HashMap<String, String> stringStringHashMap = new HashMap<>();
stringStringHashMap.put(null,null);
System.out.println(stringStringHashMap);
Hashtable<Object, Object> objectObjectHashtable = new Hashtable<>();
objectObjectHashtable.put(null,null);//NullPointerException
- Collections Tools
for Collection provides a set of Java tools for us
//常见方法
import java.util.*;
public class test5 {
public static void main(String[] args) {
ArrayList<Integer> integers = new ArrayList<>();
integers.add(20);
integers.add(30);
integers.add(50);
integers.add(60);
integers.add(40);
int i = Collections.binarySearch(integers, 30);
System.out.println(i);
System.out.println(integers);
Integer max = Collections.max(integers);
Integer min = Collections.min(integers);
Collections.shuffle(integers);
System.out.println(integers);
}
}