1. String-StringBuffer-StringBulider
1.1 总
/ | String | StringBulider | StringBuffer |
---|---|---|---|
可变 | 0 | 1 | 1 |
性能 | 较高 | 高 | 低 |
线程 | 不安全 | 不安全 | 安全 |
1.2 分
1.2.1 String的不可变性
(1)String不可变但不代表引用不可以变
String str = "Hello";
str = str + " World";
System.out.println("str=" + str);
// str = Hello World
实质:String内部不变
只是str由原来指向"Hello"的内存地址转为指向"Hello
World"的内存地址而已,
也就是说多开辟了一块内存区域给"Hello World"字符串。
应用: 在使用 HashMap 的时候,用 String 做 key
(不可变:一次缓存即可)
1.2.2 StringBulider的高性能
(1)可变性
(2)线程不安全:无需处理同步(多线程)
(3)高效方法:insert();append()
1.2.3 StringBuffer的线程安全
加锁(同步锁等)
2. 集合
2.1 List
/ | ArrayList | LinkedList | Vector |
---|---|---|---|
实质 | 动态数组 | 双向链表 | 数组 |
适用 | 随机访问 | 插入删除 | / |
同步 | 0 | 0 | 1 |
线程 | 不安全 | 不安全 | 安全 |
2.2 LinkedList的额外方法
2.3 Set
/ | HashSet | TreeSet |
---|---|---|
NULL | 1 | 0 |
适用 | 查找 | 排序 |
结构 | 哈希表 | 红黑树 |
实现类 | Set | SortedSet |
2.4 Map
2.5 集合的转换
2.6 集合的遍历
2.7 集合的排序
2.8 Collection-Collections
/ | Collecton | Collections |
---|---|---|
实质 | 接口 | 类 |
2.8.1 Collections常用方法
(1)sort
List<Integer> numbers = new ArrayList<>();
numbers.add(10);
numbers.add(5);
numbers.add(20);
numbers.add(15);
Collections.sort(numbers);
System.out.println("排序后的数字列表:");
for (Integer number : numbers) {
System.out.println(number);
}
(2)binarySearch
List<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
names.add("Charlie");
names.add("Dave");
int index = Collections.binarySearch(names, "Charlie");
if (index >= 0) {
System.out.println("找到了,索引位置为 " + index);
} else {
System.out.println("未找到");
}
(3)replaceAll
List<String> colors = new ArrayList<>();
colors.add("red");
colors.add("green");
colors.add("blue");
colors.add("yellow");
Collections.replaceAll(colors, "blue", "orange");
System.out.println("替换后的颜色列表:");
for (String color : colors) {
System.out.println(color);
}
2.9 TreeMap-TreeSet
2.9.1 图示
/ | TreeMap | TreeSet |
---|---|---|
数据结构 | 红黑树 | 红黑树 |
存储方式 | 键值对 | 值 |
访问方式 | 键 | 值 |
2.9.2 关系
TreeSet底层基于TreeMap
2.9.3 排序
(默认)基于Comparable接口的CompareTo()
2.10 Array-ArrayList
/ | Array | ArrayList |
---|---|---|
存储类型 | 引用/基本数据类型 | 引用数据类型 |
大小 | 固定 | 扩展 |
内置方法 | 更丰富 |
2.11 Compare-CompareTo
前与后 | 返回值 |
---|---|
> | 正数 |
= | 0 |
< | 负数 |
(1)Compare
import java.util.Comparator;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Fruit {
private String name;
private int price;
// 自定义比较规则:按照价格从高到低排序
public static class PriceComparator implements Comparator<Fruit> {
public int compare(Fruit fruit1, Fruit fruit2) {
return fruit2.getPrice() - fruit1.getPrice();
}
}
public static void main(String[] args) {
List<Fruit> fruits = new ArrayList<>();
fruits.add(new Fruit("apple", 10));
fruits.add(new Fruit("banana", 5));
fruits.add(new Fruit("orange", 8));
// 使用自定义比较器进行排序
fruits.sort(new PriceComparator());
System.out.println("按照价格从高到低排序的水果列表:");
for (Fruit fruit : fruits) {
System.out.println(fruit.getName() + " - " + fruit.getPrice());
}
}
}
(2)CompareTo
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Fruit implements Comparable<Fruit> {
private String name;
private int price;
// 实现 compareTo 方法,按照价格从高到低排序
public int compareTo(Fruit other) {
return other.getPrice() - this.getPrice();
}
public static void main(String[] args) {
List<Fruit> fruits = new ArrayList<>();
fruits.add(new Fruit("apple", 10));
fruits.add(new Fruit("banana", 5));
fruits.add(new Fruit("orange", 8));
// 使用默认比较规则进行排序
fruits.sort();
System.out.println("按照价格从高到低排序的水果列表:");
for (Fruit fruit : fruits) {
System.out.println(fruit.getName() + " - " + fruit.getPrice());
}
}
}
2.12 HashMap-TreeMap
适用
HashMap:增删查
TreeMap:排序