List
Map
两种方式循环遍历map
public static void main(String[] args) { Map<String,Integer> map = new HashMap<>(); map.put("a",1); map.put("b",2); map.put("c",3); //map.keySet() 返回key值的set集合 for(String s:map.keySet()){ System.out.println(s+" = "+map.get(s)); } }
public static void main(String[] args) { Map<String,Integer> map = new HashMap<>(); map.put("a",1); map.put("b",2); map.put("c",3); //entrySet()包含每一个key-value映射,可以同时遍历 key和value for(Map.Entry<String,Integer> entry:map.entrySet()){ System.out.println(entry.getKey()+"="+entry.getValue()); } }
如果key是 enum类型的话,推荐使用EnumMap
public static void main(String[] args) { Map<DayOfWeek, String> map = new EnumMap<>(DayOfWeek.class); map.put(DayOfWeek.MONDAY, "星期一"); map.put(DayOfWeek.TUESDAY, "星期二"); map.put(DayOfWeek.WEDNESDAY, "星期三"); map.put(DayOfWeek.THURSDAY, "星期四"); map.put(DayOfWeek.FRIDAY, "星期五"); map.put(DayOfWeek.SATURDAY, "星期六"); map.put(DayOfWeek.SUNDAY, "星期日"); System.out.println(map); System.out.println(map.get(DayOfWeek.MONDAY)); }
Set
Queue
把元素添加到队列末尾、从队列头部取出元素
int size()
:获取队列长度boolean add(E)
/boolean offer(E)
:添加元素到队尾E remove()
/E poll()
:获取队首元素并从队列中删除E element()
/E peek()
:获取队首元素但并不从队列中删除
add()/remove()/element() 失败会抛异常,offer()/poll()/peek() 失败会返回false或null
PriorityQueue:优先队列
PriorityQueue
和Queue
的区别在于,它的出队顺序与元素的优先级有关,对PriorityQueue
调用remove()
或poll()
方法,返回的总是优先级最高的元素。
放入PriorityQueue
的元素,必须实现Comparable
接口,PriorityQueue
会根据元素的排序顺序决定出队的优先级,以下例子是按照字母排序出队的
public class Main { public static void main(String[] args) { Queue<String> q = new PriorityQueue<>(); // 添加3个元素到队列: q.offer("apple"); q.offer("pear"); q.offer("banana"); System.out.println(q.poll()); // apple System.out.println(q.poll()); // banana System.out.println(q.poll()); // pear System.out.println(q.poll()); // null,因为队列为空 } }
如果要放入的元素并没有实现Comparable
接口,PriorityQueue
允许我们提供一个Comparator
对象来判断两个元素的顺序
public class Main { public static void main(String[] args) { //UserComparator()是 compator对象 Queue<User> q = new PriorityQueue<>(new UserComparator()); // 添加3个元素到队列: q.offer(new User("Bob", "A1")); q.offer(new User("Alice", "A2")); q.offer(new User("Boss", "V1")); System.out.println(q.poll()); // Boss/V1 System.out.println(q.poll()); // Bob/A1 System.out.println(q.poll()); // Alice/A2 System.out.println(q.poll()); // null,因为队列为空 } } class UserComparator implements Comparator<User> { public int compare(User u1, User u2) { if (u1.number.charAt(0) == u2.number.charAt(0)) { // 如果两人的号都是A开头或者都是V开头,比较号的大小: return u1.number.compareTo(u2.number); } if (u1.number.charAt(0) == 'V') { // u1的号码是V开头,优先级高: return -1; } else { return 1; } } } class User { public final String name; public final String number; public User(String name, String number) { this.name = name; this.number = number; } public String toString() { return name + "/" + number; } }
Deque:双端队列
与Queue对比下
public class Main { public static void main(String[] args) { Deque<String> deque = new LinkedList<>(); deque.offerLast("A"); // A deque.offerLast("B"); // A <- B deque.offerFirst("C"); // C <- A <- B System.out.println(deque.pollFirst()); // C, 剩下A <- B System.out.println(deque.pollLast()); // B, 剩下A System.out.println(deque.pollFirst()); // A System.out.println(deque.pollFirst()); // null } }
Stack (栈)
只有入栈和出栈的操作:
- 把元素压栈:
push(E)
- 把栈顶的元素“弹出”:
pop()
- 取栈顶元素但不弹出:
peek()
Java中,我们用Deque
可以实现Stack
的功能,注意只调用push()
/pop()
/peek()
方法,避免调用Deque
的其他方法