第一部分 Map集合
Map<K,V>
Map集合中的鍵值具有一一映射的關係。且不能有重複的鍵。是一個頂層接口。Map集合和Collection集合的區別:
Map集合存儲元素是成對出現的。Collection集合存儲的元素是單獨出現的。
Map集合的鍵是唯一的,由hash表保障了鍵的唯一性。值是可重複的。
Collection中的set不可重複,List可以重複。
Map集合的數據結構只針對鍵有效,跟值無關。
Collection集合的數據結構是針對元素有效。
Map集合的功能概述:
1、添加
V put (K key, V value);添加元素。如果該鍵存在,就會將值替代,並返回oldValue。
2、刪除
void clear();移除所有的鍵值對
V.remove(Object key);移除指定的鍵,返回該鍵的值。
3、判斷
boolean containsKey(Object key);判斷集合是否包含指定的鍵
boolean containsValue(Object value);判斷集合是否包含指定的值
boolean isEmpty()判斷集合是否爲空。
4、獲取
Set<Map.Entry<K,V>entrySet():???
V get(Object key):根據鍵獲取值。
Set<K> keySet();獲取集合中所有鍵的集合。
Collection<V> values();獲取集合中所有值的集合。
5、長度
int size();返回集合中的鍵值對的對數。
public class MapDemo{
public static void main(String [] args){
Map<String,String> map=new HashMap<String,String>();
map.put("黑馬安卓","3");
map.put("黑馬大數據","5");
map.put("黑馬ios","1");
System.out.println(map);//這裏將會無序輸出,因爲Map的鍵是Set集合型的,Set集合型的爲無序輸出。又因爲鍵值一一對應,所以如此
System.out.println(map.get("黑馬安卓"));//輸出3.
//獲取map集合所有鍵的集合,並遍歷。
Set<String> set=map.keySet();
for(String key:set){
System.out.println(key);
}
//獲取map集合所有值的集合
Collection<String> value=map.values();
for(String value:map){
System.out.println(value);//輸出3 5 1
}
//第一種Map集合的遍歷
Set<String> set1=map.keySet();
for(String key:set1){
String value=map.get(key);
System.out.println(key+"*****"+value);
}
//第二種Map集合遍歷
Set<Map.Entry<String,String>> set=map.entrySet();
for(Map.Entry<String,String> me:set){
String key=me.getKey();
String value=me.getValue();
System.out.println(key+"*****"+value);
}
}
}
一、HashMap
(1)、HashMap<String,String>
import java.util.HashMap;
import java.util.Set;
public class HashMapDemo{
public static void main(String [] args){
HashMap<String,String> hm=new HashMap<String,String>();
hm.put("it001","北京黑馬");
hm.put("it002","北京傳智");
hm.put("it003","北京安卓");
hm.put("it003","北京黑馬安卓");//將會覆蓋上一個鍵值。
Set<String> set=hm.keySet();
for(String key:set){
String value=hm.get(key);
System.out.println(key+"*****"+value);
}
}
}
輸出無序結果:
it003*****北京黑馬安卓
it002*****北京傳智
it001*****北京黑馬
(2)HashMap<String,Student>自定義類型
自定義類型在遍歷集合的時候需要注意鍵值的類型。
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
class HashMapDemo{
public static void main(String [] args){
Map<String, Student> map1 = new HashMap<String, Student>();
Student stu1 = new Student("heima1", 25);
Student stu2 = new Student("heima2", 28);
Student stu3 = new Student("heima3", 22);
map1.put("huanzhi2", stu1);
map1.put("huanzhi1a", stu2);
map1.put("chuanzhi3", stu3);
Set<String> set = map1.keySet();
for (String s : set) {
Student s11 = map1.get(s);
System.out.println(s + "******" + s11.getName() + "^^^"
+ s11.getAge());
}
}
}
class Student{
private String name;
private int age;
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
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;
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
}
輸出結果爲:
chuanzhi3******heima3^^^22
huanzhi2******heima1^^^25
huanzhi1a******heima2^^^28
二、LinkedHashTable
Map接口的哈希表和鏈接列表實現,具有可預知的迭代順序。
由哈希表保證鍵的唯一性
由鏈表保證輸出的有序。
import java.util.LinkedHashMap;
import java.util.Set;
public class LinkedHashMapDemo {
public static void main(String[] args) {
// 創建集合對象
LinkedHashMap<String, String> hm = new LinkedHashMap<String, String>();
// 創建並添加元素
hm.put("2345", "hello");
hm.put("1234", "world");
hm.put("3456", "java");
hm.put("1234", "javaee");
hm.put("3456", "android");
// 遍歷
Set<String> set = hm.keySet();
for (String key : set) {
String value = hm.get(key);
System.out.println(key + "---" + value);
}
}
}
輸出結果爲:
2345---hello
1234---javaee
3456---android
三、TreeMap
由紅黑樹結構保證鍵的排序和唯一性。
import java.util.Set;
import java.util.TreeMap;
public class TreeMapDemo {
public static void main(String[] args) {
// 創建集合對象
TreeMap<String, String> tm = new TreeMap<String, String>();
// 創建元素並添加元素
tm.put("hello", "你好");
tm.put("world", "世界");
tm.put("java", "爪哇");
tm.put("world", "世界2");//這一條語句將會覆蓋另外一個world鍵的值。
tm.put("javaee", "爪哇EE");
// 遍歷集合
Set<String> set = tm.keySet();
for (String key : set) {
String value = tm.get(key);
System.out.println(key + "---" + value);
}
}
}
輸出結果爲:
hello---你好
java---爪哇
javaee---爪哇EE
world---世界2
從結果可以看出TreeMap還可以實現鍵的排序。默認情況下按照自然排序。
如果在TreeMap集合中,鍵的類型爲自定義的話,如果想要進行鍵的自然排序,就需要在自定義的類中重寫compareTo().如果想實行的是比較器排序,就需要重寫compare()方法。
練習題:
"aababcabcdabcde",獲取字符串中每一個字母出現的次數要求結果:a(5)b(4)c(3)d(2)e(1)
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
public class TreeMapDemo {
public static void main(String[] args) {
// 定義一個字符串(可以改進爲鍵盤錄入)
Scanner sc = new Scanner(System.in);
System.out.println("請輸入一個字符串:");
String line = sc.nextLine();
// 定義一個TreeMap集合
TreeMap<Character, Integer> tm = new TreeMap<Character, Integer>();
//把字符串轉換爲字符數組
char[] chs = line.toCharArray();
//遍歷字符數組,得到每一個字符
for(char ch : chs){
//將得到的字符ch拿到空集合中去比對,由於每一個元素第一次出現的時候,都會是和null比較,因此,該字符將會被添加,並且該字符作爲鍵對應的值將會被該寫爲1.當第二個這個字符出現的時候,發現不是空,於是就i++,記錄該字符出現的次數。
Integer i = tm.get(ch);
//是null:說明該鍵不存在,就把該字符作爲鍵,1作爲值存儲
if(i == null){
tm.put(ch, 1);
}else {
//不是null:說明該鍵存在,就把值加1,然後重寫存儲該鍵和值
i++;
tm.put(ch,i);
}
}
//定義字符串緩衝區變量
StringBuilder sb= new StringBuilder();
//遍歷集合,得到鍵和值,進行按照要求拼接
Set<Character> set = tm.keySet();
for(Character key : set){
Integer value = tm.get(key);
sb.append(key).append("(").append(value).append(")");
}
//把字符串緩衝區轉換爲字符串輸出
String result = sb.toString();
System.out.println("result:"+result);
}
}
1:Hashtable和HashMap的區別?
Hashtable:線程安全,效率低。不允許null鍵和null值
HashMap:線程不安全,效率高。允許null鍵和null值
2:List,Set,Map等接口是否都繼承子Map接口?
List,Set不是繼承自Map接口,它們繼承自Collection接口
Map接口本身就是一個頂層接口
第二部分 Collections
此類完全由在collection上進行操作或返回collection的靜態方法組成。
* public static <T> void sort(List<T> list):排序 默認情況下是自然順序。
* public static <T> int binarySearch(List<?> list,T key):二分查找
* public static <T> T max(Collection<?> coll):最大值
* public static void reverse(List<?> list):反轉
* public static void shuffle(List<?> list):隨機置換
Collections.sort(list)默認是按照自然排序進行。如果需要給自定義類排序,
ArrayList集合,可以通過實現排序接口,重寫compareTo()方法,實現自然排序。還可以通過其他方法實現去除重複。
ArrayList存儲自定義對象的兩種排序方法:
1、自然排序:實現接口,重寫方法
public class Student implements Comparable<Student> {
private String name;
private int age;
public Student() {
super();
}
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 compareTo(Student s) {
int num = this.age - s.age;
int num2 = num == 0 ? this.name.compareTo(s.name) : num;
return num2;
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class CollectionsDemo {
public static void main(String[] args) {
// 創建集合對象
List<Student> list = new ArrayList<Student>();
// 創建學生對象
Student s1 = new Student("黑馬1", 27);
Student s2 = new Student("黑馬2", 30);
Student s3 = new Student("黑馬3", 28);
Student s4 = new Student("黑馬4", 29);
Student s5 = new Student("黑馬1", 27);
// 添加元素對象
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
list.add(s5);
// 排序
// 自然排序
// Collections.sort(list);
// 遍歷集合
for (Student s : list) {
System.out.println(s.getName() + "---" + s.getAge());
}
}
}
2、比較器排序。匿名對象
</pre><pre>
<pre name="code" class="java">public class Student {
private String name;
private int age;
public Student() {
super();
}
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;
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class CollectionsDemo {
public static void main(String[] args) {
// 創建集合對象
List<Student> list = new ArrayList<Student>();
// 創建學生對象
<pre name="code" class="java"><span style="white-space:pre"> </span>Student s1 = new Student("黑馬1", 27);
Student s2 = new Student("黑馬2", 30);
Student s3 = new Student("黑馬3", 28);
Student s4 = new Student("黑馬4", 29);
Student s5 = new Student("黑馬1", 27);
// 添加元素對象
list.add(s1);list.add(s2);list.add(s3);list.add(s4);list.add(s5);
//比較器排序
Collections.sort(list, new Comparator<Student>() {
public int compare(Student s1, Student s2) {
int num = s2.getAge() - s1.getAge();
int num2 = num == 0 ? s1.getName().compareTo(s2.getName()): num;
return num2;
}
});
// 遍歷集合
for (Student s : list) {
System.out.println(s.getName() + "---" + s.getAge());
}
}
}
其實在自然排序和比較器排序同時存在的情況下,以比較器排序爲主