常见的数据结构:
- 栈:
先进后出 后进先出
3. 队列:
先进先出 后进后出
- 数组:
数组中元素地址连续 长度固定 使用索引查找数据速度快(不使用索引查找指定元素还是慢 需要遍历) 增删速度慢
- 链表:
单向链表 a -> b -> c 增删相对数组较快
双向链表 a <-> b <-> c 对收尾进行增删改查操作很快
- 查找二叉树:
在不出现一边长一边短的情况下CRUD较快
- 平衡树:
CRUD较快
- 红黑树:
综合性能都很好
list系列ArrayList: 基于数组实现 根据索引查询速度较快 增删相对较慢
LinkedList: 基于双链表实现 增删收尾元素非常快
set系列LinkedHashset 有序 不重复 无索引
HashSet 无序 不重复 无索引
TreeSet 排序 不重复 无索引
Set集合工具类 Collectionsset = new TreeSet<>(new Comparator () { @Override public int compare(Student o1, Student o2) { return o2.age-o1.age; //降序 //return o1.age-o2.age; //升序 } });
Collections.addAll(set, s1, s2, s3) Collections.sort(list); ......
可变参数
get(new int[]{2,1,4},1,2,3); //调用
//定义可变参数方法
public static void get(int[] arr,int...nums) {
System.out.println(Arrays.toString(nums));
System.out.println(Arrays.toString(arr));
}
Map双列集合(键值对集合)
HashMap
LinkedHashMap
entrySet把map集合转化为set集合Set不可变集合> entries = map.entrySet(); for (Map.Entry entry : entries) { System.out.println(entry.getKey()); System.out.println(entry.getValue()); }
1.不可变List
List
- 不可变Set 不能有重复值
Set
- 不可变map
Map
更好更简单操作集合
Listlist2 = new ArrayList<>(); Collections.addAll(list1, "aab", "bbc", "cccccd", "aaae", "ea"); Collections.addAll(list1, "111", "223", "4443d", "23ae", "34211a"); Stream stream1 = list1.stream(); Stream stream2 = list2.stream(); Stream.concat(stream1,stream2).forEach(s -> System.out.print(s+" ")); list1.stream().filter(s -> s.startsWith("a")).limit(1).forEach(s -> System.out.println(s+" ")); 输出:aab bbc cccccd aaae ea 111 223 4443d 23ae 34211a 输出: aab aaae
获取数组流
int[] arr = {1, 1, 1};
Stream stream3 = Stream.of(arr);
IntStream stream4 = Arrays.stream(arr);
stream流收集
恢复到数组 / 集合中去
流只能使用一次
Listlist = stream1.collect(Collectors.toList()); Set set = stream1.collect(Collectors.toSet()); 数组流直接toArray()



