import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class MapDemo {
public static void main(String[] args) {
// demo();
demo2();
// demo3();
}
@SuppressWarnings("unchecked")
public static void demo() {
Map map = new HashMap();
map.put("xt001", "zhangsan");
map.put("xt002", "lisi");
map.put("xt003", "wangwu");
System.out.println(map);
System.out.println(map.get("xt001"));
System.out.println(map.get("xt002"));
System.out.println(map.get("xt003"));
}
public static void demo2() {
Map<String, String> map = new HashMap<String, String>();
map.put("xt001", "zhangsan");
map.put("xt002", "lisi");
map.put("xt003", "wangwu");
System.out.println(map);
System.out.println("***********************************");
System.out.println(map.get("xt001"));
System.out.println(map.get("xt002"));
System.out.println(map.get("xt003"));
System.out.println("***********************************");
// Set<Map.Entry<K,V>> entrySet()
Set<Map.Entry<String, String>> set = map.entrySet();
Iterator<Map.Entry<String, String>> it = set.iterator();
while (it.hasNext()) {
Map.Entry<String, String> entry = it.next(); // 接口对应的子类对象
System.out.println(entry.getKey() + " " + entry.getValue());
}
System.out.println("***********************************");
Set<String> keys = map.keySet();
Iterator<String> it2 = keys.iterator();
while (it2.hasNext()) {
String key = it2.next();
System.out.println(key + " " + map.get(key));
}
}
public static void demo3() {
Map<String, Student> map = new HashMap<String, Student>();
Student s1 = new Student();
s1.setName("zhangsan");
s1.setAge(20);
Student s2 = new Student();
s2.setName("lisi");
s2.setAge(30);
Student s3 = new Student();
s3.setName("wangwu");
s3.setAge(40);
map.put("xt1", s1);
map.put("xt2", s2);
map.put("xt3", s3);
System.out.println(map);
Student s = map.get("xt2");
System.out.println(s.getName() + " " + s.getAge());
System.out.println("************************************");
Set<Map.Entry<String, Student>> set = map.entrySet();
Iterator<Map.Entry<String, Student>> it = set.iterator();
while (it.hasNext()) {
Map.Entry<String, Student> entry = it.next(); // 接口对应的子类对象
Student stu = entry.getValue();
System.out.println(entry.getKey() + " " + stu.getName() + " "
+ stu.getAge());
}
System.out.println("************************************");
// 留着自己写
}
}
想要对字符串数组中的元素进行长度排序但是的包含重复的,是否可以先用TreeSet集合来存储呢??? |
|