Java知识点辨析

1. String-StringBuffer-StringBulider

1.1 总
/StringStringBuliderStringBuffer
可变011
性能较高
线程不安全不安全安全
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

在这里插入图片描述

/ArrayListLinkedListVector
实质动态数组双向链表数组
适用随机访问插入删除/
同步001
线程不安全不安全安全
2.2 LinkedList的额外方法

在这里插入图片描述

2.3 Set

在这里插入图片描述在这里插入图片描述

/HashSetTreeSet
NULL10
适用查找排序
结构哈希表红黑树
实现类SetSortedSet
2.4 Map

在这里插入图片描述在这里插入图片描述

2.5 集合的转换

在这里插入图片描述

2.6 集合的遍历

在这里插入图片描述

2.7 集合的排序

在这里插入图片描述

2.8 Collection-Collections
/CollectonCollections
实质接口
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 图示
/TreeMapTreeSet
数据结构红黑树红黑树
存储方式键值对
访问方式
2.9.2 关系

TreeSet底层基于TreeMap

2.9.3 排序

(默认)基于Comparable接口的CompareTo()

2.10 Array-ArrayList

/ArrayArrayList
存储类型引用/基本数据类型引用数据类型
大小固定扩展
内置方法更丰富

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:排序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

似云似月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值