接口Set的
HashMap和LinkedHashMap、
TreeMap实现
package jianzun.net;
import java.util.HashMap;
/**
* @author jianzun
*2021-09-22
*/
public class testMap {
//测试HashMap
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap<Integer, String> map = new HashMap<>();
map.put(10122, "Java");
map.put(21, "Spring");
map.put(9999, "hibernate");
map.put(87, "Java");
map.put(2101, "Struts2.x");
map.put(2101, "JSP");
//输出结果:{21=Spring, 2101=JSP, 87=Java, 10122=Java, 9999=hibernate}
System.out.println(map);
//输出结果21:Spring 2101:JSP 87:Java 10122:Java 9999:hibernate
for (int i:map.keySet()) {
System.out.print(i+":"+map.get(i)+" ");
}
System.out.println();
//输出结果JSP
System.out.println(map.get(2101));
map.replace(21, "SpringMVC");
//输出结果是21:SpringMVC 2101:JSP 87:Java 10122:Java 9999:hibernate
for(Integer i:map.keySet()) {
System.out.print(i+":"+map.get(i)+" ");
}
System.out.println();
System.out.println(map.size());
// System.out.println(map.entrySet());
/*System.out.println(map.values());
System.out.println(map.keySet());
for (String i:map.values()) {
System.out.println(i);
}*/
}
}
package jianzun.net;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* @author jianzun
*2021-09-22
*/
public class testMap2 {
//测试LinkedHashMap
public static void main(String[] args) {
// TODO Auto-generated method stub
LinkedHashMap<Integer, String> map = new LinkedHashMap<>();
map.put(10, "JAVA");
map.put(90, "Spring");
map.put(20, "hibernate");
map.put(50, "Struts2.x");
map.put(40, "android");
//输出{10=JAVA, 90=Spring, 20=hibernate, 50=Struts2.x, 40=android}
//按照顺序输出,,跟HashMap的输出不一样
System.out.println(map);
//[10=JAVA, 90=Spring, 20=hibernate, 50=Struts2.x, 40=android]
System.out.println(map.entrySet());
//[10, 90, 20, 50, 40]
System.out.println(map.keySet());
//Map的遍历方式二:
Iterator<Map.Entry<Integer, String>> it =map.entrySet().iterator();
while(it.hasNext()) {
Map.Entry<Integer, String> entry = it.next();
System.out.print(entry.getKey()+":"+entry.getValue()+" ");
}
System.out.println();
//Map的遍历方式三:
for(Map.Entry<Integer, String> entry:map.entrySet()) {
System.out.print(entry.getValue()+"::"+entry.getKey()+" ");
}
}
}
package jianzun.net;
import java.util.TreeSet;
/**
* @author jianzun
*2021-09-23
*/
public class testMap3 {
//测试TreeSet
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeSet<String> set = new TreeSet<>();
set.add("JAVA");
set.add("spring");
set.add("hibernate");
set.add("hibernate");
set.add("Struts2.x");
//输出结果[JAVA, Struts2.x, hibernate, spring]
//没有重复的元素
System.out.println(set);
//反向输出
System.out.println(set.descendingSet());
//哈希码366715226
System.out.println(set.hashCode());
//最后一个元素spring
System.out.println(set.last());
//元素长度4
System.out.println(set.size());
//输出结果[JAVA, Struts2.x],截取元素的函数
System.out.println(set.subSet("JAVA", "hibernate"));
}
}