1.HashMap
package learn.collection;
import org.testng.annotations.Test;
import org.w3c.dom.ls.LSOutput;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class hashmap {
private Map<String,String> createhashmap = new HashMap<>();
public Map<String,String> createmap(){
String key = "1";
String value = "abc";
String key2 = "c";
String value2 = "123";
String key3 = "b";
String value3 = "d";
createhashmap.put(key,value);
createhashmap.put(key2,value2);
createhashmap.put(key3,value3);
return createhashmap;
}
@Test(groups = {"map"},priority = 0 )
public void putmethod(){
String key = "e";
String value = "t";
hashmap map = new hashmap();
Map<String,String> newmap = map.createmap();
System.out.println("使用put方法前"+newmap);
newmap.put(key,value);
System.out.println("使用put方法后"+newmap);
}
@Test(groups = {"map"},priority = 1)
public void removemethod(){
String key = "1";
String delkey = "b";
String delvalue = "d";
hashmap map = new hashmap();
Map<String,String> newmap = map.createmap();
System.out.println("使用remove方法前"+newmap);
newmap.remove(key);
System.out.println("使用remove方法后"+newmap);
System.out.println("去掉"+delkey+delvalue+"前"+newmap);
newmap.remove(delkey,delvalue);
System.out.println("去掉"+delkey+delvalue+"后"+newmap);
}
@Test(groups = {"map"},priority = 2)
public void containsmethod(){
hashmap map = new hashmap();
Map<String,String> newmap = map.createmap();
String containskey = "1";
String containskey2 = "d";
String containsvalue = "d";
String containsvalue2 = "1235";
boolean judge = newmap.containsKey(containskey);
boolean judge2 = newmap.containsKey(containskey2);
boolean judge3 = newmap.containsValue(containsvalue);
boolean judge4 = newmap.containsValue(containsvalue2);
if(judge){
System.out.println(newmap+"包含key:"+containskey);
}else{
System.out.println(newmap+"不包含key:"+containskey);
}
if(judge2){
System.out.println(newmap+"包含key:"+containskey2);
}else{
System.out.println(newmap+"不包含key:"+containskey2);
}
if(judge3){
System.out.println(newmap+"包含value:"+containsvalue);
}else{
System.out.println(newmap+"不包含value:"+containsvalue);
}
if(judge4){
System.out.println(newmap+"包含value:"+containsvalue2);
}else{
System.out.println(newmap+"不包含value:"+containsvalue2);
}
}
@Test(groups = {"map"},priority = 3,description = "map迭代")
public void iteratormethod(){
hashmap map = new hashmap();
Map<String,String> newmap = map.createmap();
int size = newmap.size();
System.out.println(newmap+"长度为"+size);
Set<String> keyset = newmap.keySet();
System.out.println(newmap+"keyset为"+keyset);
for(Object o:keyset){
System.out.println(o+"值为:"+newmap.get(o));
}
}
@Test(groups = {"map"},priority = 4)
public void newmethod(){
hashmap newhashmap = new hashmap();
Map<String,String> newmap = newhashmap.createmap();
Set<Map.Entry<String, String>> es = newmap.entrySet();
for(Map.Entry<String,String> e : es){
System.out.println("Mapentry遍历:"+e.getKey()+e.getValue());
}
System.out.println(newmap+"entrySet:"+es);
Set<String> ks = newmap.keySet();
System.out.println(newmap+"keySet:"+ks);
}
}
2.TreeMap
3.LinkedHashMap
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。