Map
HashMap
遍历map:
方法一:使用keySet()方法
package com.mcq;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class HashMapDemo {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap();
map.put("小明",18);
map.put("小花",19);
map.put("小丽",16);
map.put("小花", 15);
Set<String> keySet=map.keySet();
for(String key:keySet){
System.out.println(key+":"+map.get(key));
}
}
}
/*
小明:18
小丽:16
小花:15
*/
//Student.java
package com.mcq;
public class Student{
private String name;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int hashCode() { //右键source点hashCode()和equals()快捷生成
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
//HashMapDemp.java
package com.mcq;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class HashMapDemo {
public static void main(String[] args) {
Map<Student, Integer> map = new HashMap();
map.put(new Student("小明", 18),99);
map.put(new Student("小花",19),98);
map.put(new Student("小丽",16),97);
map.put(new Student("小花",20),101);
Set<Student> keySet=map.keySet();
for(Student key:keySet){
System.out.println(key.getName()+" "+key.getAge()+":"+map.get(key));
}
}
}
方法二:使用entrySet方法():
package com.mcq;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class HashMapDemo {
public static void main(String[] args) {
Map<Student, Integer> map = new HashMap();
map.put(new Student("小明", 18), 99);
map.put(new Student("小花", 19), 98);
map.put(new Student("小丽", 16), 97);
map.put(new Student("小丽", 16), 97);
map.put(new Student("小丽", 16), 97);
Set<Entry<Student, Integer>> entrySet = map.entrySet();
for (Entry<Student, Integer> entry : entrySet) {
System.out.println(entry.getKey().getName()+" "+entry.getKey().getAge()+" "+entry.getValue());
}
}
}
TreeMap
匿名内部类方法:
package com.mcq;
import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.Map.Entry;
public class TreeMapDemo {
public static void main(String[] args) {
Map<Student, Integer> map=new TreeMap(new Comparator<Student>() {
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
int t=o1.getAge()-o2.getAge();
return t==0?o1.getName().compareTo(o2.getName()):t;
}
});
map.put(new Student("小明", 18), 99);
map.put(new Student("小花", 19), 98);
map.put(new Student("小丽", 16), 97);
map.put(new Student("小丽", 16), 97);
map.put(new Student("小丽", 16), 97);
Set<Entry<Student, Integer>> entrySet = map.entrySet();
for (Entry<Student, Integer> entry : entrySet) {
System.out.println(entry.getKey().getName()+" "+entry.getKey().getAge()+" "+entry.getValue());
}
}
}
Collections工具类
包含处理集合的一些静态方法
Arrays工具类
包含处理数组的静态方法
实例:
package com.mcq;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class CollectionsDemo {
public static void main(String[] args) {
List<String> list=new ArrayList<String>();
list.add("hello");
list.add("world");
list.add("haha");
System.out.println(list);
Collections.sort(list);//排序
System.out.println(list);
Collections.reverse(list);//反转
System.out.println(list);
Collections.shuffle(list);//洗牌,随机顺序
System.out.println(list);
List<Student> list2=Arrays.asList(new Student("a",16),new Student("c", 19),new Student("b", 15));//数组转list
System.out.println(list2);
Collections.sort(list2,new Comparator<Student>() {
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
int t=o1.getAge()-o2.getAge();
return t==0?o1.getName().compareTo(o2.getName()):t;
}
});
System.out.println(list2);
}
}