1.List集合子实现类的特点
List集合子实现类 | 底层数据结构 | 增删速度 | 查询速度 | 线程角度 | 是否同步 | 执行效率 | 备注 |
ArrayList | 数组 | 慢 | 快 | 不安全 | 不同步 | 高 | 有扩容机制,扩容后为原来的1.5倍 |
Vector | 数组 | 慢 | 快 | 安全 | 同步 | 低 | |
LinkedList | 链表 | 快 | 慢 | 不安全 | 不同步 | 高 |
2.Collection,List,Vector集合的遍历方式
Collection | List | Vector | |
集合转为对象数组后遍历 | √ | √ | √ |
迭代器Iterator | √ | √ | √ |
get()方法加普通for循环 | √ | √ | |
列表迭代器ListIterator | √ | √ | |
增强for循环 | √ | √ | |
枚举接口Enumeration | √ | ||
elementAt()方法 | √ |
3.List集合去重
方式1:通过新建集合去重
方式2:通过选择排序的方法去重
//应用选择排序思想去重
public class SelectSort{
public static void main (String args[]){
int[] arr = {1,5,9,3,4};
System.out.println("排序前:");
printArr(arr);
for(int i = 0;i<arr.length-1;i++){
for(int j = 1;j<arr.length-i;j++){
if(arr[i+j]<arr[i]){
int temp = arr[i+j];
arr[i+j] = arr[i];
arr[i] = temp;
}
}
}
System.out.println("排序后:");
printArr(arr);
}
public static void printArr(int[] arr){
System.out.print("[");
for(int i = 0;i<arr.length-1;i++){
System.out.print(arr[i]+", ");
}
System.out.println(arr[arr.length-1]+"]");
}
}
4.TreeSet集合排序
方式1:自然排序.泛型E实现Comparable接口,重写compareTo()方法.
方式2:比较器排序.自定义一个类实现Comparator接口,重写compare()方法.
/*键盘录入5个学生的姓名,年龄,语文成绩,数学成绩,英语成绩,按照总分从高到低排序;
自己分析次要条件; 使用TreeSet集合的比较器排序完成*/
import java.util.Comparator;
import java.util.Objects;
class Student {
private String name;
private int yuwen;
private int math;
private int english;
private int age;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", yuwen=" + yuwen +
", math=" + math +
", english=" + english +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return yuwen == student.yuwen &&
math == student.math &&
english == student.english &&
name.equals(student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, yuwen, math, english);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getYuwen() {
return yuwen;
}
public void setYuwen(int yuwen) {
this.yuwen = yuwen;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
public Student(String name, int yuwen, int math, int english, int age) {
this.name = name;
this.yuwen = yuwen;
this.math = math;
this.english = english;
this.age = age;
}
public Student() {
}
}
import java.util.Comparator;
class MyComparator implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
int num1 = o2.getYuwen()+o2.getMath()+o2.getEnglish()-(o1.getYuwen()+o1.getMath()+o1.getEnglish());
int num2 = (num1 == 0)?o2.getYuwen()-o1.getYuwen():num1;
int num3 = (num2 == 0)?o2.getMath()-o1.getMath():num2;
int num4 = (num3 == 0)?o2.getEnglish()-o1.getEnglish():num3;
int num5 = (num4 == 0)?o1.getName().compareTo(o2.getName()):num3;
return num5;
}
}
import java.util.Comparator;
import java.util.TreeSet;
public class Test1 {
public static void main(String[] args) {
Student s1 = new Student("zhao",100,90,90,12);
Student s2 = new Student("qian",90,95,95,12);
Student s3 = new Student("sun",100,90,89,12);
Student s4 = new Student("li",90,95,95,12);
Student s5 = new Student("zhou",97,100,83,12);
Comparator<Student> comparator = new MyComparator();
TreeSet<Student> ts = new TreeSet<>(comparator);
ts.add(s1);
ts.add(s2);
ts.add(s3);
ts.add(s4);
ts.add(s5);
System.out.println(ts);
for(Student s:ts){
System.out.print(s.getYuwen()+s.getMath()+s.getEnglish());
System.out.println(s);
}
}
}
5.Map集合的遍历方式
方式1:通过键获取值(推荐)
//方式1示例
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
public class Test3 {
public static void main(String[] args) {
HashMap<String, ArrayList<String>> hm = new HashMap<>();
ArrayList<String> a1 = new ArrayList<>();
a1.add("刘备");
a1.add("曹操");
ArrayList<String> a2 = new ArrayList<>();
a2.add("贾宝玉");
a2.add("林黛玉");
ArrayList<String> a3 = new ArrayList<>();
a3.add("孙悟空");
a3.add("唐僧");
hm.put("《三国演义》",a1);
hm.put("《红楼梦》",a2);
hm.put("《西游记》",a3);
Set<String> set = hm.keySet();
for(String key:set){
ArrayList<String> value = hm.get(key);
System.out.println(key+" "+value);
}
}
}
方式2:获取所有键值对象.利用Map集合的成员方法entrySet(),获取所有的键值对象Map.Entry<K,V>,再利用getKey(),getValue()方法分别获取键和值.
Map<String,String> map = new HashMap<>();
map.put();//给map集合添加元素
Set<Map.Entry<String,String>> entrySet = map.entrySet();
for(Map.Entry<String,String> en:entrySet){
String key = en.getKey();
String value = en.getValue();
System.out.println(key+"="+value);
}
6.线程的创建方式
1)继承自Thread类,重写run()方法
2)实现Runnable接口,重写run()方法
3)通过线程池创建线程
7.线程的生命周期
1)NEW新建
2)RUNNABLE运行
3)BLOCKED阻塞
4)WAITTING等待(一直等待)
5)TIMED_WATTING超时等待
6)TERMINATED终止
8.静态代理
代理角色与真实角色都需要实现同一接口,代理角色帮助真实角色完成事情,真实角色专注于自己的事情.在实际开发中,业务代码和系统监控要分离,代理角色完成系统监控的东西(日志,权限校验等),真实角色完成CRUD(业务代码:添加,查询,修改,删除).
9.如何解决线程的安全问题
校验多线程安全问题的标准
1)是否是多线程环境?
2)是否有共享数据?
3)是否有多条语句对共享数据操作?
以上三条只能以第3条为突破口解决线程安全问题.可以通过同步锁或同步方法来解决安全问题.
synchronized(锁对象){
//同步代码块
}
jdk还提供了另一个锁:Lock(可重入的互斥锁).
10.等待唤醒机制
多线程中加入同步锁之后,可以有效解决安全问题,但可能又出现死锁的问题.对此,需引入生产者消费者模式来解决问题,应用wait()和notify()方法,可以解决线程通信问题.
11.方法递归解决问题的思想
将大的问题拆分为小的问题
1)首先要有一个方法
2)方法要有出口条件,否则是死递归
3)有一定的规律
构造方法不存在方法递归.
12.IO流的分类
字节流 | 字节输入流InputStream | 文件字节输入流FileInputStream |
字节缓冲输入流BufferedInputStream | ||
字节输出流OutputStream | 文件字节输出流FileOutputStream | |
字节缓冲输出流BufferedOutputStream | ||
字符流 | 字符输入流Reader | 字符转换输入流InputStreamReader |
字符转换输入流的便捷类FileReader | ||
字符缓冲输入流BufferedReader | ||
字符输出流Writer | 字符转换输出流OutputStreamWriter | |
字符转换输出流的便捷类FileWriter | ||
字符缓冲输出流BufferedWriter |
13.使用字节输入流和字节输出流进行文件读写复制
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test4 {
public static void main(String[] args) throws Exception {
copyTxt("d://a.mp4","copy.mp4");
// copyTxt2("d://a.mp4","copy.mp4");
}
private static void copyTxt2(String srcDir,String dstDir) throws IOException {
FileInputStream fis = new FileInputStream(srcDir);
FileOutputStream fos = new FileOutputStream(dstDir);
byte[] bytes = new byte[1024];
int len = 0;
while((len = fis.read(bytes))!= -1) {
fos.write(bytes,0,len);
}
}
private static void copyTxt(String srcDir,String dstDir) throws Exception {
FileInputStream fis = new FileInputStream(srcDir);
FileOutputStream fos = new FileOutputStream(dstDir);
int by = 0;
while((by = fis.read())!= -1){
fos.write(by);
}
fos.close();
fis.close();
}
}
14.属性集合列表Properties
属于Map集合,但是没有泛型,键和值都只能是String类型,能够使用Map集合的功能.有自己特有的功能.
//Properties特有功能
public Object setProperties(String key ,String value) //添加键和值
public Set<String> stringPropertyNames() //获取属性列表中的所有键的集合
public String getProperty(String key) //属性列表中通过键获取值