1. list ArrayList和LinkedList
ListTest
2. map
<key,value>
数据结构很重要
HashMap 用到了hash算法
(1)“aaa”.harshCode() 算的一个值:123456
123456%16
(2)
(3) 连超过8个,会生成红黑树。
//HashMap遍历,背 for (Map.Entry entry :map.entrySet()){ System.out.println(entry.getKey()); System.out.println(entry.getValue()); }
hash是无序的,key不可排序,但是存进去会乱序。
但是TreeMap可以排序,且是自动排序
import java.util.HashMap; import java.util.Map; /** * @author sr * @date 2021/1/18 */ public class MapTest { public static void main(String[] args) { Map<String,User> map = new HashMap<>(); map.put("yeye",new User("sr","123")); map.put("erzi",new User("mc","123")); System.out.println(map.get("yeye")); //HashMap遍历,背 for (Map.Entry entry :map.entrySet()){ System.out.println(entry.getKey()); System.out.println(entry.getValue()); } } }
3. set
HashSet 无序 不可重复
for (Integer i: set){ System.out.println(i); }
4. 迭代器Iterator
(1)list
//list2.iterator().var Iterator<User> iterator = list2.iterator(); while (iterator.hasNext()){ User next = iterator.next(); System.out.println(next); }
List<Integer> list = new LinkedList<>(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(4); list.add(5); //把4全部删除 Iterator<Integer> iterator = list.iterator(); while(iterator.hasNext()){ Integer next = iterator.next(); if (next == 4){ iterator.remove(); } }
//把4全部删除 list.removeIf(next -> next == 4);
(2)set同上
(3)map特殊,map里面存的是一个一个的节点
//map.entrySet().iterator.var
Iterator<Map.Entry<String, User>> iterator = map.entrySet().iterator();
while (iterator.hasNext()){
//iterator.next().var
Map.Entry<String, User> next = iterator.next();
System.out.println(next.getValue());
System.out.println(next.getKey());
}