Map
ConcurrentHashMapTest
package Test.map;
import org.junit.jupiter.api.Test;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class ConcurrentHashMapTest {
@Test
public void test() {
Map map = new ConcurrentHashMap<>();
map.put("xm", 18);
System.out.println(map.get("xm"));
}
//18
}
HashMapTest
package Test.map;
import org.junit.jupiter.api.Test;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
public class HashMapTest {
@Test
public void test01() {
Map map = new HashMap<>();
map.put("xm", 95);
map.put("x", 90);
System.out.println(map.get("xm"));
System.out.println(map.get("x"));
}
@Test
public void test02() {
Map map = new HashMap<>();
map.put("xm", 95);
map.put("x", 90);
map.put("xm", 66);
System.out.println("size >>> " + map.size());
System.out.println("xm >>> " + map.get("xm"));
System.out.println("x >>> " + map.get("x"));
}
@Test
public void test03() {
Map map = new HashMap<>();
map.put("xm", 95);
map.put("x", 90);
System.out.println("size >>> " + map.size());
System.out.println("xm >>> " + map.get("xm"));
System.out.println("x >>> " + map.get("x"));
map.remove("x");
System.out.println("size >>> " + map.size());
System.out.println("xm >>> " + map.get("xm"));
System.out.println("x >>> " + map.get("x"));
}
@Test
public void test04() {
Map map = new HashMap<>();
map.put("xm", 95);
map.put("x", 90);
System.out.println("size >>> " + map.size());
System.out.println("xm >>> " + map.get("xm"));
System.out.println("x >>> " + map.getOrDefault("x", 0));
map.remove("x");
System.out.println("size >>> " + map.size());
System.out.println("xm >>> " + map.get("xm"));
System.out.println("x >>> " + map.getOrDefault("x", 0));
}
@Test
public void test05() {
Map map = new HashMap<>();
for (int i = 0; i < 10; i++) {
map.put("xm" + (i + 1), ThreadLocalRandom.current().nextInt(100));
}
// 获取集合中所有的key
Set keySet = map.keySet();
Iterator it = keySet.iterator();
while (it.hasNext()) {
String key = it.next();
System.out.println("key >>> " + key + "tvalue >>> " + map.get(key));
}
}
@Test
public void test06() {
Map map = new HashMap<>();
for (int i = 0; i < 10; i++) {
map.put("xm" + (i + 1), ThreadLocalRandom.current().nextInt(100));
}
Set> entrySet = map.entrySet();
Iterator> it = entrySet.iterator();
while (it.hasNext()){
Map.Entry entry = it.next();
System.out.println("key >>> " + entry.getKey() + "tvalue >>> " + entry.getValue());
}
}
@Test
public void test07() {
Map map = new HashMap<>();
for (int i = 0; i < 10; i++) {
map.put("xm" + (i + 1), ThreadLocalRandom.current().nextInt(100));
}
Collection values = map.values();
Iterator it = values.iterator();
while (it.hasNext()){
Integer value = it.next();
System.out.println(value);
}
}
@Test
public void test08() {
Map map = new HashMap<>();
for (int i = 0; i < 10; i++) {
map.put("xm" + (i + 1), ThreadLocalRandom.current().nextInt(100));
}
// 判断map中是否存在key
System.out.println(map.containsKey("xm"));
System.out.println(map.containsKey("xm3"));
}
}
HashtableTest
package Test.map;
import org.junit.jupiter.api.Test;
import java.util.Hashtable;
import java.util.Map;
public class HashtableTest {
@Test
public void test() {
Map map = new Hashtable<>();
map.put("xm", 95);
System.out.println(map.get("xm"));
}
//95
}
LinkedHashMapTest
package Test.map;
import org.junit.jupiter.api.Test;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
public class LinkedHashMapTest {
@Test
public void test05() {
Map map = new LinkedHashMap<>();
for (int i = 0; i < 10; i++) {
map.put("xm" + (i + 1), ThreadLocalRandom.current().nextInt(100));
}
// 获取集合中所有的key
Set keySet = map.keySet();
Iterator it = keySet.iterator();
while (it.hasNext()) {
String key = it.next();
System.out.println("key >>> " + key + "tvalue >>> " + map.get(key));
}
}
@Test
public void test06() {
Map map = new LinkedHashMap<>();
for (int i = 0; i < 10; i++) {
map.put("xm" + (i + 1), ThreadLocalRandom.current().nextInt(100));
}
Set> entrySet = map.entrySet();
Iterator> it = entrySet.iterator();
while (it.hasNext()) {
Map.Entry entry = it.next();
System.out.println("key >>> " + entry.getKey() + "tvalue >>> " + entry.getValue());
}
}
}