集合 List Set Map Queue
List:
有序 重复
Set:
无序 不重复
Map集合:
以键—值对的形式保存数据
通过键操作数据
键不可以重复
值可以重复
Deque(Queue)集合:
就是一种双端队列的实现,允许在队列的两端操作元素
有序的,允许重复的先进先出(FIFO),不能随机访问队列中的数据 尾部添加头部删除
List
package Content; import java.util.*; public class ListDemo { public static void main(String[] args) { /*List集合: 有序 重复 * 代表一个元素有序、可重复的集合; * List集合每个元素都有对应的索引 * List集合可以双向遍历ListIterator *分类: * ArrayList: 动态数组 查询快 增删慢 * LinkedList: 链表列表,既是一个链表,又是一个双端队列 查询慢 增删快 */ ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add("Anna"); arrayList.add("Sam"); arrayList.add("John"); arrayList.add("Mike"); arrayList.add("Peter"); arrayList.add("Sam"); System.out.println(arrayList); //常用方法 System.out.println(arrayList.size()); System.out.println(arrayList.contains("John")); System.out.println(arrayList.isEmpty()); arrayList.remove("Anna"); System.out.println(arrayList); arrayList.add(0, "Jerry"); System.out.println(arrayList); arrayList.remove(1); System.out.println(arrayList); System.out.println(arrayList.get(3)); /*遍历: * 1、通过迭代器Iterator循环遍历 * 2、通过foreach循环遍历*/ Iterator<String> iterator = arrayList.iterator(); while (iterator.hasNext()) { System.out.print(iterator.next()+" "); } System.out.println(); for (String string : arrayList) { System.out.print(string + " "); } System.out.println(); //双向遍历迭代器ListIterator ListIterator<String> listIterator = arrayList.listIterator(); //向后遍历 while (listIterator.hasNext()) { System.out.print(listIterator.next() + " "); } System.out.println(); //向前遍历 while (listIterator.hasPrevious()) { System.out.print(listIterator.previous() + " "); } System.out.println(); ListIterator<String> listIterator2 = arrayList.listIterator(arrayList.size()); while (listIterator.hasPrevious()) { System.out.print(listIterator.previous() + " "); } System.out.println(); arrayList.clear(); System.out.println(arrayList.isEmpty()); LinkedList<Integer> linkedList = new LinkedList<>(); linkedList.add(500); linkedList.add(200); linkedList.add(800); linkedList.add(300); linkedList.add(200); System.out.println(linkedList); System.out.println(linkedList.size()); System.out.println(linkedList.isEmpty()); System.out.println(linkedList.contains(500)); linkedList.add(0, 1000); System.out.println(linkedList); linkedList.remove(1); System.out.println(linkedList); linkedList.remove(new Integer(200)); System.out.println(linkedList); //LinkedList实现了双端队列的功能 linkedList.addFirst(800); linkedList.addLast(700); System.out.println(linkedList.removeFirst()); System.out.println(linkedList.removeLast()); linkedList.push(10000); System.out.println(linkedList.pop()); } }
Set
package Content; import java.util.*; public class SetDemo { public static void main(String[] args) { /* HashSet: 无序 不重复 * LinkedHashSet: 有序(进出顺序) 不重复 * TreeSet: 排序 不重复 * */ System.out.println("HashSet集合:无序 不重复"); HashSet<String> hashSet = new HashSet<>(); hashSet.add("Tom"); hashSet.add("Anna"); hashSet.add("John"); hashSet.add("MiKe"); hashSet.add("Prter"); hashSet.add("Tom"); System.out.println(hashSet); HashSet<Integer> hashSet11 = new HashSet<>(); hashSet11.add(1); hashSet11.add(9); hashSet11.add(8); hashSet11.add(7); hashSet11.add(3); hashSet11.add(2); System.out.println(hashSet11); System.out.println("LinkedHashSet集合:"); LinkedHashSet<String> linkedHashSet = new LinkedHashSet<>(); linkedHashSet.add("Tom"); linkedHashSet.add("Anna"); linkedHashSet.add("John"); linkedHashSet.add("MiKe"); linkedHashSet.add("Prter"); linkedHashSet.add("Tom"); System.out.println(linkedHashSet); System.out.println("TreeSet集合:"); TreeSet<String> treeSet = new TreeSet<>(); treeSet.add("Tom"); treeSet.add("Anna"); treeSet.add("John"); treeSet.add("MiKe"); treeSet.add("Prter"); treeSet.add("Tom"); System.out.println(treeSet); //遍历 Iterator<String> iterator = hashSet.iterator(); while (iterator.hasNext()) { System.out.print(iterator.next()+" "); } System.out.println(); for (String string : hashSet) { System.out.print(string + " "); } System.out.println(); } }
Map
package Content; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; import java.util.TreeMap; public class MapDemo { /*Map集合: * 以键—值对的形式保存数据 * 通过键操作数据 * 键不可以重复 * 值可以重复 * HashMap: 无序 * LinkedHashMap: * TreeMap: 有序 * */ public static void main(String[] args) { HashMap<String, String> hashMap = new HashMap<>(); hashMap.put("CN", "China"); hashMap.put("US","USA"); hashMap.put("GB","Great Britain"); hashMap.put("JP", "Japan"); hashMap.put("XRB", "Japan"); System.out.println(hashMap); //使用put()方法来修改Map中已存在的映射 System.out.println(hashMap.put("CN", "People's Republic of China")); System.out.println(hashMap); System.out.println(hashMap.get("US")); System.out.println(hashMap.size()); System.out.println(hashMap.isEmpty()); System.out.println(hashMap.remove("JP")); System.out.println(hashMap.containsKey("US")); System.out.println(hashMap.containsValue("Japan")); //遍历 //通过keySet方法,返回Map中所有“键”的Set集合 Set<String> set = hashMap.keySet(); Iterator<String> iterator = set.iterator(); while(iterator.hasNext()) { String key = iterator.next(); String value = hashMap.get(key); System.out.println(key + "——" + value); } System.out.println(); for (String string : hashMap.keySet()) { System.out.println(string + "——" +hashMap.get(string)); } System.out.println(); /* * entrySet()方法返回映射所包含的映射关系的Set集合(一个关系就是一个键-值对), * 把(key-value)作为一个整体一对一对地存放到Set集合当中。 * 迭代后可以e.getKey(),e.getValue()取key和value。 */ Set<Map.Entry<String,String>> entrySet = hashMap.entrySet(); for(Map.Entry<String,String> entry:entrySet){ System.out.println(entry.getKey() + "--" + entry.getValue()); } HashMap<String, Integer> hashMap1 = new HashMap<String,Integer>(); hashMap1.put("ABC", 0); hashMap1.put("BCD",2); System.out.println(hashMap1.get("ABC")); hashMap1.put("ABC",hashMap1.get("ABC")+1); System.out.println(hashMap1.get("ABC")); for(String e:hashMap1.keySet()) { if(hashMap1.get(e)==2) System.out.println(e); } System.out.println("---------------------HashMap-----------------------"); HashMap<String, String> hashMap111 = new HashMap<>(); hashMap111.put("CN", "China"); hashMap111.put("US","USA"); hashMap111.put("GB","Great Britain"); hashMap111.put("JP", "Japan"); hashMap111.put("XRB", "Japan"); System.out.println(hashMap111); System.out.println("---------------------LinkedHashMap-----------------------"); LinkedHashMap<String, String> LinkedHashMap111 = new LinkedHashMap<>(); LinkedHashMap111.put("CN", "China"); LinkedHashMap111.put("US","USA"); LinkedHashMap111.put("GB","Great Britain"); LinkedHashMap111.put("JP", "Japan"); LinkedHashMap111.put("XRB", "Japan"); System.out.println(LinkedHashMap111); /* HashMap: 无序 * LinkedHashMap: 有序 * TreeMap: 排序 * */ System.out.println("---------------------TreeMap-----------------------"); TreeMap<String, String> TreeMap111 = new TreeMap<>(); TreeMap111.put("CN", "China"); TreeMap111.put("US","USA"); TreeMap111.put("GB","Great Britain"); TreeMap111.put("JP", "Japan"); TreeMap111.put("XRB", "Japan"); System.out.println(TreeMap111); } }
Queue
package Content; import java.util.Comparator; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.Queue; public class QueueDemo { /* Deque(Queue)集合: * 就是一种双端队列的实现,允许在队列的两端操作元素 * 有序的,允许重复的先进先出(FIFO),不能随机访问队列中的数据 尾部添加头部删除 * * PriorityQueue。 每次从队列中取出的是具有最高优先权的元素。 如果想实现按照自己的意愿进行优先级排列的队列的话,需要实现Comparator接口。 优先队列中元素默认按自然顺序排列,也就是数字默认是小的在队列头,字符串则按字典序排列。 */ /*Queue队列: * 就是一种双端队列的实现,允许在队列的两端操作元素 * 有序的,允许重复的 * *添加: * add 增加一个元索 如果队列已满,则抛出一个IIIegaISlabEepeplian异常 * offer 添加一个元素并返回true 如果队列已满,则返回false * put 添加一个元素 如果队列满,则阻塞 *刪除: * remove 移除并返回队列头部的元素 如果队列为空,则抛出一个NoSuchElementException异常 * poll 移除并返问队列头部的元素 如果队列为空,则返回null * take 移除并返回队列头部的元素 如果队列为空,则阻塞 *查找: * peek 返回队列头部的元素 如果队列为空,则返回null * element 返回队列头部的元素 如果队列为空,则抛出一个NoSuchElementException异常 * * */ static class node{ int num1; int num2; String string; public node(int num1,int num2,String string) { this.num1 = num1; this.num2 = num2; this.string = string; } } public static void main(String[] args) { //LinkedList实现了Deque接 口 Queue<Integer> queue1 = new LinkedList<Integer>(); queue1.offer(1); queue1.offer(3); queue1.offer(2); queue1.offer(5); System.out.println(queue1); System.out.println(queue1.size()); System.out.println(queue1.peek()); System.out.println(queue1); System.out.println(queue1.poll()); System.out.println(queue1); Queue<node> queue = new LinkedList<node>(); queue.add(new node(33, 55, "Amy")); queue.add(new node(44, 55, "Bob")); queue.add(new node(44, 66, "Cindy")); queue.add(new node(44, 66, "Dam")); System.out.println(queue); //默认由小到大 PriorityQueue<Integer> priorityQueue = new PriorityQueue<Integer>(); priorityQueue.add(4); priorityQueue.add(1); priorityQueue.add(3); priorityQueue.add(5); priorityQueue.add(2); System.out.println(priorityQueue); //由大到小 PriorityQueue<Integer> priorityQueue1 = new PriorityQueue<Integer>(11,new Comparator<Integer>(){ public int compare(Integer i1,Integer i2){ return i2-i1; } }); priorityQueue1.add(4); priorityQueue1.add(1); priorityQueue1.add(3); priorityQueue1.add(2); System.out.println(priorityQueue1); } }