Queue: 基本上,一个队列就是一个先入先出(FIFO)的数据结构
Queue接口与List、Set同一级别,都是继承了Collection接口。LinkedList实现了Deque接 口
示例代码:
1. import java.util.LinkedList;
2. import java.util.Queue;
3.
4. public class QueueDemo {
5. public static void main(String[] args) {
6. // add()和remove()方法在失败的时候会抛出异常(不推荐)
7. Queue<String> queue = new LinkedList<String>();
8. // 添加元素
9. queue.offer("同学A");
10. queue.offer("同学B");
11. queue.offer("同学C");
12. queue.offer("同学D");
13. queue.offer("同学E");
14. // 遍历队列
15. System.out.println("----遍历队列----");
16. for (String q : queue) {
17. System.out.print(q + "\t");
18. }
19. System.out.println();
20.
21. // 返回第一个元素,并在队列中删除
22. System.out.println("------poll------");
23. System.out.println("poll=" + queue.poll());
24. System.out.println(queue);
25. // 返回第一个元素
26. System.out.println("------element------");
27. System.out.println("element=" + queue.element());
28. System.out.println(queue);
29. // 返回第一个元素
30. System.out.println("------peek------");
31. System.out.println("peek=" + queue.peek());
32. System.out.println(queue);
33. }
34. }
2、Map集合
Map接口中键和值一一映射. 可以通过键来获取值。
• 给定一个键和一个值,你可以将该值存储在一个Map对象. 之后,你可以通过键来访问对应的值。
• 当访问的值不存在的时候,方法就会抛出一个NoSuchElementException异常.
• 当对象的类型和Map里元素类型不兼容的时候,就会抛出一个 ClassCastException异常。
• 当在不允许使用Null对象的Map中使用Null对象,会抛出一个NullPointerException 异常。
• 当尝试修改一个只读的Map时,会抛出一个UnsupportedOperationException异常。
一、Map接口示例代码
示例代码:
1. import java.util.HashMap;
2. import java.util.Iterator;
3. import java.util.Map;
4. import java.util.Set;
5.
6. /**
7. * Map接口 key-value 根据一个对象查找对象. HashMap、HashTable、TreeMap是它的实现类,
8. *
9. * @author sxj
10. *
11. */
12. public class HashMapAndHashTable {
13. public static void main(String[] args) {
14. // 如果是基本数据类型,声明的map的时候使用包装类
15. Map<Integer, String> map = new HashMap<>();
16.
17. // 添加数据 put当key不存在时,添加key-value
18. map.put(1, "str1");
19. map.put(2, "str2");
20. map.put(3, "str3");
21. map.put(4, "str4");
22. map.put(5, "str5");
23. // put 当key存在时,修改key对应的value
24. map.put(5, "111111");
25.
26. map.put(6, null);
27. map.put(7, null);
28.
29. // 移除 remove(key)
30. map.remove(7);
31.
32. // 判断是否存在key
33. System.out.println("是否存在key:5===》" + map.containsKey(5));
34. // 判断是否存在value
35. System.out.println("是否存在Value:str4====>" + map.containsValue("str4"));
36.
37. // 清空map
38. // map.clear();
39.
40. System.out.println("map是否为空:" + map.isEmpty());
41.
42. // 输出
43. System.out.println(map);
44. // 遍历
45. Set<Integer> keysSet = map.keySet();
46. Iterator<Integer> iterator = keysSet.iterator();
47. while (iterator.hasNext()) {
48. Integer intKey = iterator.next();
49. System.out.println("key:" + intKey + "---->Value:"
50. + map.get(intKey));
51. }
52. System.out.println("--------------------");
53. for (Iterator<Integer> iterator2 = keysSet.iterator(); iterator2.hasNext();) {
54. int intKey = iterator2.next();
55. System.out.println("key:" + intKey + "---->Value:"
56. + map.get(intKey));
57. }
58. System.out.println("--------------------");
59. for (int intKey : keysSet) {
60. System.out.println("key:" + intKey + "---->Value:"
61. + map.get(intKey));
62. }
63. }
64. }
二、TreeMap类
与 TreeSet类似的是,TreeMap中也提供了一系列根据key顺序访问key-value对的方法:
示例代码:
1. import java.util.Map;
2. import java.util.TreeMap;
3.
4. public class TreeMapDemo {
5. public static void main(String[] args) {
6.
7. // 如果是基本数据类型,声明的map的时候使用包装类
8. TreeMap<Integer, String> map = new TreeMap<>();
9.
10. // 添加数据 put当key不存在时,添加key-value
11. map.put(1, "str1");
12. map.put(2, "str2");
13. map.put(3, "str3");
14. map.put(4, "str4");
15. map.put(5, "str5");
16. // put 当key存在时,修改key对应的value
17. map.put(5, "111111");
18.
19. map.put(6, null);
20. map.put(7, null);
21.
22. System.out.println(map);
23. System.out.println("--------------------------");
24. //firstEntry() firstKey() lastEntry() lastKey()
25. System.out.println("firstEntry()--->"+map.firstEntry());
26. System.out.println("firstKey()--->"+map.firstKey());
27. System.out.println("lastEntry()--->"+map.lastEntry());
28. System.out.println("lastKey()--->"+map.lastKey());
29.
30. System.out.println("--------------------------");
31. //higherEntry(K key) higherKey(K key) lowerEntry(K key) lowerKey(K key)
32. System.out.println("higherKey(4)--->"+map.higherKey(4));
33. System.out.println("higherEntry(4)--->"+map.higherEntry(4));
34. System.out.println("lowerKey(4)--->"+map.lowerKey(4));
35. System.out.println("lowerEntry(4)--->"+map.lowerEntry(4));
36.
37. System.out.println("--------------------------");
38. // headMap(K toKey) tailMap(K fromKey) subMap(K fromKey, K toKey)
39. Map<Integer,String> mapHeadMap=map.headMap(4);
40. Map<Integer,String> mapTailMap=map.tailMap(4);
41. Map<Integer,String> mapSubMap=map.subMap(2,5);
42.
43. System.out.println("headMap(4)--->"+mapHeadMap);
44. System.out.println("tailMap(4)--->"+mapTailMap);
45. System.out.println("subMap(2,5)--->"+mapSubMap);
46.
47. }
48. }
由于TreeMap是有序的,也支持Comparable和Comparator两种排序方式。TreeMap主要是Key值排序
TreeMap Comparable排序示例代码:
1. import java.util.Map;
2. import java.util.Set;
3. import java.util.TreeMap;
4.
5. public class TreeMapDemo1 {
6. public static void main(String[] args) {
7. Map<Person1, String> map=new TreeMap<Person1, String>();
8. map.put(new Person1("1",51), "1");
9. map.put(new Person1("2",15), "2");
10. map.put(new Person1("3",32), "3");
11. map.put(new Person1("4",24), "4");
12. map.put(new Person1("5",6), "5");
13.
14. Set<Person1> kSet=map.keySet();
15. for (Person1 person1 : kSet) {
16. System.out.println("name:"+person1.getName()+";age:"+person1.getAge()+";num:"+map.get(person1));
17. }
18. }
19. }
20.
21. class Person1 implements Comparable<Person1> {
22. private String name;
23. private int age;
24.
25. public Person1(String name,int age){
26. this.name=name;
27. this.age=age;
28. }
29.
30. public String getName() {
31. return name;
32. }
33.
34. public void setName(String name) {
35. this.name = name;
36. }
37.
38. public int getAge() {
39. return age;
40. }
41.
42. public void setAge(int age) {
43. this.age = age;
44. }
45.
46. @Override
47. public int compareTo(Person1 o) {
48. // 从小到大 :this-o
49. // 从大到小:o-this
50. // return this.age - o.age;
51. if (this.age != o.age) {
52. return this.age - o.age;
53. } else {
54. return this.name.compareTo(o.name);
55. }
56. }
57. }
TreeMap Comparator排序示例代码:
1. import java.util.Comparator;
2. import java.util.Map;
3. import java.util.Set;
4. import java.util.TreeMap;
5.
6. public class TreeMapDemo2 {
7. public static void main(String[] args) {
8. Map<Person2, String> map = new TreeMap<Person2, String>(
9. new Comparator<Person2>() {
10. @Override
11. public int compare(Person2 o1, Person2 o2) {
12. /**
13. * 从小到大:o1-o2 从大到小:o2-o1
14. */
15. if (o1.getAge() != o2.getAge()) {
16. return o1.getAge() - o2.getAge();
17. } else {
18. return o2.getName().compareTo(o1.getName());
19. }
20. }
21. });
22.
23. map.put(new Person2("1", 51), "1");
24. map.put(new Person2("2", 15), "2");
25. map.put(new Person2("3", 32), "3");
26. map.put(new Person2("4", 24), "4");
27. map.put(new Person2("5", 6), "5");
28.
29. Set<Person2> kSet = map.keySet();
30. for (Person2 Person2 : kSet) {
31. System.out.println("name:" + Person2.getName() + ";age:"
32. + Person2.getAge() + ";num:" + map.get(Person2));
33. }
34. }
35. }
36.
37. class Person2 {
38. private String name;
39. private int age;
40.
41. public Person2(String name, int age) {
42. this.name = name;
43. this.age = age;
44. }
45.
46. public String getName() {
47. return name;
48. }
49.
50. public void setName(String name) {
51. this.name = name;
52. }
53.
54. public int getAge() {
55. return age;
56. }
57.
58. public void setAge(int age) {
59. this.age = age;
60. }
61. }