List 接口
List 继承自 Collection 接口。List 是有序的 Collection,List 允许有重复元素,实现 List 接口的常用类有 LinkedList,ArrayList,Vector 和 Stack。
-
ArrayList
ArrayList 实现了可变大小的数组。它允许所有元素,包括 null。ArrayList 没有同步
List list = Collections.synchronizedList(new ArrayList(...));
ArrayList 遍历输出
public static void main(String[] args) {
List<String> mList = new ArrayList<String>();
mList.add("hello");
mList.add("张三");
mList.add("李四");
mList.add("王五");
// 方法一
for (int i = 0; i < mList.size(); i++) {
System.out.println(mList.get(i));
}
// 方法二
for (String string : mList) {
System.out.println(string);
}
// 方法三
Iterator<String> it = mList.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
// 把字符串内容挨个输出
String str = "ABCDEFG";
for (int i = 0; i < str.length(); i++) {
System.out.println(str.charAt(i));
}
}
- LinkedList
LinkedList 实现了 List 接口,允许null元素,LinkedList 没有同步方法
List list = Collections.synchronizedList(new LinkedList(...));
- Vector
Vector 非常类似 ArrayList,但是 Vector 是同步的
Set 接口
Set 继承自 Collection 接口。Set 是一种不能包含有重复元素的集合,Set 最多有一个 null元素
- HashSet
此类实现 Set 接口,由哈希表(实际上是一个 HashMap 实例)支持。它不保证集合的迭代顺序;特别是它不保证该顺序恒久不变。集合元素可以是 null,但只能放入一个 null,存入 HashSet 的对象必须定义 hashCode()。
Set s = Collections.synchronizedSet(new HashSet(...));
- TreeSet
TreeSet 是 SortedSet 接口(Set 的子接口)的唯一实现类,TreeSet 可以确保集合元素处于排序状态,TreeSet 支持自然排序(Comparable)和定制排序(Comparator),
Map 接口
Map 和 Collection 是2种不同的集合。Collection 可以看作是(value)的集合,而 Map 可以看作是(key,value)的集合
- Hashtable
Hashtable继承Map接口,实现一个key-value映射的哈希表。任何非空(non-null)的对象都可作为key或者value。Hashtable 是同步的(线性安全)。
- HashMap
HashMap 和 Hashtable 类似,不同之处在于 HashMap 是非同步的,并且允许 null,即 null value 和null key
对 HashMap 排序,用 Collections 中的方法
public class HaspMapTest {
public static void main(String[] args) {
HashMap<Integer, Users> hashMap = new HashMap<Integer, Users>();
hashMap.put(1, new Users(22, "小娟"));
hashMap.put(3, new Users(21, "秀秀"));
hashMap.put(2, new Users(28, "小列"));
HashMap<Integer, Users> sortedHashMap = sortedHashMap(hashMap);
System.out.println("排序后:");
System.out.println(sortedHashMap);
}
private static HashMap<Integer, Users> sortedHashMap(HashMap<Integer, Users> hashMap) {
// 首先拿到 map 的键值对集合
Set<Entry<Integer,Users>> entrySet = hashMap.entrySet();
// 将 set 集合转为 List 集合,为什么,为了使用工具类的排序方法
ArrayList<Entry<Integer, Users>> list = new ArrayList<Entry<Integer,Users>>(entrySet);
Collections.sort(list, new Comparator<Entry<Integer,Users>>() {
@Override
public int compare(Entry<Integer, Users> o1, Entry<Integer, Users> o2) {
///按照要求根据 User 的 age 的倒序进行排
return o2.getValue().getAge()-o1.getValue().getAge();
}
});
LinkedHashMap<Integer,Users> linkedHashMap = new LinkedHashMap<Integer, Users>();
//将 List 中的数据存储在 LinkedHashMap 中
for (Entry<Integer, Users> entry : list) {
linkedHashMap.put(entry.getKey(), entry.getValue());
}
return linkedHashMap;
}
}
但凡是对集合的操作, 我们应该保持一个原则就是能用 JDK 中的 API 就有 JDK 中的 API, 比如排序算法我们不应该 去 用 冒 泡 或 者 选 择 , 而 是 首 先 想 到 用 Collections 集 合 工 具 类
HashMap 的遍历输出
public static void main(String[] args) {
Map<Integer, Student> hm = new HashMap<Integer, Student>();
hm.put(1001, new Student("张三"));
hm.put(1002, new Student("熊无"));
hm.put(1003, new Student("林之海"));
//方法一
Iterator<Integer> it = hm.keySet().iterator();
while (it.hasNext()) {
int key = it.next();
Student values = hm.get(key);
System.out.println("key-->"+key+"\tvalues-->"+values);
}
////方法二(推荐)
Iterator<Entry<Integer, Student>> it = hm.entrySet().iterator();
while (it.hasNext()) {
Entry<Integer, Student> entry = it.next();
int key = entry.getKey();
Student s = entry.getValue();
System.out.println("key-->"+key+"\tvalues-->"+s);
}
}
判断一个字符串中相同的字符有多少个(用HashMap)
public static Map<Character, Integer> CountsCharacter(String str) {//str:要判断的字符串
Map<Character, Integer> hm = new HashMap<Character, Integer>();
char[] ch = str.toCharArray();
for (Character c : ch) {
if (hm.containsKey(c)) {
int values = hm.get(c);
System.out.println(values);
hm.put(c, values + 1);
} else {
hm.put(c, 1);
}
}
return hm;
}