map
1. HashMap 和 HashTable 和 ConcurrentHashMap 和 LinkedHashMap 和 TreeMap
1)hashMap可以空键空值, 基于hash算法
2)hashTable不允许空键, 基于线程安全(利用synchronized进行方法同步)
3)ConcurrentHashMap不允许有空键, 同样为线程安全(基于对象锁,同样为线程同步,有hash算列表进行线程隔离)
4)LinkedHashMap与hashMap类似, 但是基于链表形式存储, key为有序集合
5)TreeMap为有序map,非线程安全, 基于二叉树
2. Map遍历方式
1) 迭代器遍历
1-1:
Map<String, String> map = new HashMap<String, String>();
Set<String> keys = map.keySet();
Iterator<String> it = keys.iterate();
while(it.hashNext()){
String key = it.next();
Sysout(key + map.get(key));
}
1-2:
Map<String, String> map = new HashMap<String, String>();
Set<Entry<String, String>> entrys = map.entrySet();
2)foreach遍历
2-1:
Map<String, String> map = new HashMap<String, String>();
Set<String> keys = map.keySet();
for(String key : keys){
Sysout(key + map.get(key));
}
2-2:
Map<String, String> map = new HashMap<String, String>();
Set<Entry<String, String>> entrys = map.entrySet();
for(Entry<String, String> entry : entrys){
Sysout(entry.getKey + entry.getValue());
}
3.Map的实现方式
(1):简化版
public class MyMap<K, V> {
static class MyEntry<K, V> {
K key;
V value;
public K getKey() {
return key;
}
public void setKey(K key) {
this.key = key;
}
public V getValue() {
return value;
}
public void setValue(V value) {
this.value = value;
}
}
private List<MyEntry<K, V>> list = new ArrayList<>();
private Set<MyEntry<K, V>> sets = new HashSet<>();
public void put(K key, V value){
MyEntry<K, V> e = new MyEntry<K, V>();
e.setKey(key);
e.setValue(value);
list.add(e);
sets.add(e);
}
public V get(K key){
for(MyEntry<K, V> e : list){
if(e.getKey().equals(key)){
return e.getValue();
}
}
return null;
}
public Set<MyEntry<K, V>> entrySet(){
return sets;
}
}
1. HashMap 和 HashTable 和 ConcurrentHashMap 和 LinkedHashMap 和 TreeMap
1)hashMap可以空键空值, 基于hash算法
2)hashTable不允许空键, 基于线程安全(利用synchronized进行方法同步)
3)ConcurrentHashMap不允许有空键, 同样为线程安全(基于对象锁,同样为线程同步,有hash算列表进行线程隔离)
4)LinkedHashMap与hashMap类似, 但是基于链表形式存储, key为有序集合
5)TreeMap为有序map,非线程安全, 基于二叉树
2. Map遍历方式
1) 迭代器遍历
1-1:
Map<String, String> map = new HashMap<String, String>();
Set<String> keys = map.keySet();
Iterator<String> it = keys.iterate();
while(it.hashNext()){
String key = it.next();
Sysout(key + map.get(key));
}
1-2:
Map<String, String> map = new HashMap<String, String>();
Set<Entry<String, String>> entrys = map.entrySet();
2)foreach遍历
2-1:
Map<String, String> map = new HashMap<String, String>();
Set<String> keys = map.keySet();
for(String key : keys){
Sysout(key + map.get(key));
}
2-2:
Map<String, String> map = new HashMap<String, String>();
Set<Entry<String, String>> entrys = map.entrySet();
for(Entry<String, String> entry : entrys){
Sysout(entry.getKey + entry.getValue());
}
3.Map的实现方式
(1):简化版
public class MyMap<K, V> {
static class MyEntry<K, V> {
K key;
V value;
public K getKey() {
return key;
}
public void setKey(K key) {
this.key = key;
}
public V getValue() {
return value;
}
public void setValue(V value) {
this.value = value;
}
}
private List<MyEntry<K, V>> list = new ArrayList<>();
private Set<MyEntry<K, V>> sets = new HashSet<>();
public void put(K key, V value){
MyEntry<K, V> e = new MyEntry<K, V>();
e.setKey(key);
e.setValue(value);
list.add(e);
sets.add(e);
}
public V get(K key){
for(MyEntry<K, V> e : list){
if(e.getKey().equals(key)){
return e.getValue();
}
}
return null;
}
public Set<MyEntry<K, V>> entrySet(){
return sets;
}
}