9.0 KiB
9.0 KiB
集合:
总结:
其中Collection接口中又分为set接口和list接口
Collection:
注意事项:
remove and contains方法依赖于底层equals方法
集合通用遍历方法:
1.迭代器 2.增强for循环 3.forEach
首先获取迭代器:
Iterator<Student> it = c.iterator();
其次判断循环是否还有元素
hasNext()方法 next()方法
注意:next()方法只调用一次,要不然会引起指针异常
迭代器源码解析:
三种方法演示:
public class Test2 {
public static void main(String[] args) {
Collection<Student> c = new ArrayList<>();
c.add(new Student("小鸟游六花",15));
c.add(new Student("小鸟游七花",15));
c.add(new Student("小鸟游八花",15));
//迭代器循环
Iterator<Student> it = c.iterator();
while (it.hasNext()){
Student stu = it.next();
System.out.println(stu.getName()+"---"+stu.getAge());
}
//增强for循环
for (Student stu:c){
System.out.println(stu);
}
//forEach遍历
c.forEach(student -> System.out.println(student));
}
}
List集合
list集合特点以及方法
并发修改异常:
List集合的5种遍历方式
-
Iterator迭代器
-
增强for循环
//增强for循环 //for(元素类型,起名 : 遍历的集合) for (Student stu : c) { System.out.println(stu); } -
foreach
//foreach循环 c.forEach(stu -> System.out.println(stu)); -
普通for循环
-
ListIterator迭代器
数据结构:栈 队列 链表 数组
其中Arraylist集合扩容机制为
- 在初始化时长度为0
- 调用添加方法add时长度为10
- 此后长度超过10时,会将数组长度增加原来的1/2
源码分析:此为长度还未超出10时的源码过程
此为超出10时源码过程:
LinkedList特有方法
removeFirst() removeLast()
Set集合
数据结构:树
树的知识点总结:
三种二叉树
平衡二叉树
红黑树(重难点)
红黑树是一种特殊的平衡二叉树,但并不是完全平衡的平衡二叉树,效率最高
添加树的情况
TreeSet集合
排序和取出顺序示例:
自然排序示例:
比较器排序示例:
注意:
this - 0 正序 o-this 降序
o1 - o2 正序,o2 - o1 降序
两种方式比较:
HashSet
基本运用:
特点:去重(hashCode方法和equals方法共同运用)
遍历:迭代器,增强for循环,foreach方法
HashSet组成:
JDK8版本以后Hashset的添加过程(面试常问重点!)
2步骤过程:
将数值调用hashCode方法得到原始哈希值,然后将原始哈希值向右移动16位与原始哈希值进行异或操作,得到的数值与数组长度作取模运算得到位置。
如何提高HashSet查询过程:
LinkedHashSet集合
特点:
有序,不重复,无索引,保证存取顺序。
集合总结:
小知识点:可变参数
可变参数用在形参中可以接受多个数据。
例:int... 名称;
传输参数非常灵活,方便,可以不传参数,可以传1个或多个,也可以传数组
方法本质就是一个数组
Collections集合工具类:
示例:
//多数据添加
TreeSet<Student> alist = new TreeSet<>();
Collections.addAll(alist,new Student("张三",14),new Student("八奈见杏菜",16),new Student("七奈见杏菜",16));
System.out.println(alist);
//二分查找
ArrayList<String> ts = new ArrayList<>();
Collections.addAll(ts,"2","3","4");
System.out.println(Collections.binarySearch(ts, "2"));
//洗牌
ArrayList<Student> blist = new ArrayList<>();
Collections.shuffle(blist);
System.out.println(alist);
//从集合中找最值
System.out.println(Collections.max(ts));
System.out.println(Collections.min(ts));
System.out.println(Collections.min(alist));
System.out.println(Collections.max(alist));
MAP集合:
常用API:
MAP特点:
Map的三种遍历方式
1:entrySet方法
HashMap<String, String> m = new HashMap<>();
m.put("杏菜", "15岁");
m.put("佳树", "14岁");
m.put("温水", "15岁");
m.put("梦子", "15岁");
//用entrySet方法获取到所有键值对象
Set<Map.Entry<String, String>> entrySet = m.entrySet();
//遍历set集合获取到每一个键值对儿对象
for (Map.Entry<String, String> stringStringEntry : entrySet) {
//通过键值对对象获取每一个数值
System.out.println(stringStringEntry.getKey() + "---" + stringStringEntry.getValue());
}
2:foreach方法:
hs.forEach(new BiConsumer<String, List<String>>() {
@Override
public void accept(String key, List<String> value) {
System.out.print(key+"=");
List list = value;
for (int i = 0; i < list.size()-1; i++) {
System.out.print(list.get(i)+",");
}
System.out.println(list.get(value.size()-1));
}
});













