Java在日常工作中用的非常多,每天日复一日的增删改查,集合作为最常见的数据结构为我们处理数据提供了很大的方便,集合中有一些常见的算法,比如:排序,二分查找,洗牌算法等等,本文主要介绍Java集合中常见的通用算法。
函数 | 作用 |
---|---|
sort() | 排序 |
shuffle() | 进行洗牌 |
reverse() | 颠倒元素的顺序 |
fill() | 用指定值替换元素中的每个元素 |
copy() | 创建从指定源到目标的元素副本 |
swap() | 交换集合中两个元素的位置 |
addAll() | 将一个集合的所有元素添加到其他集合中 |
binarySearch() | 进行搜索 |
frequency() | 返回元素在集合中出现的次数 |
disjoint() | 检查两个元素是否包含某些共同元素 |
min() max() | 分别用于查找最小和最大元素 |
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
// 初始化集合
ArrayList<Integer> numbers = new ArrayList<>();
// 添加元素
numbers.add(4);
numbers.add(2);
numbers.add(3);
System.out.println("Unsorted ArrayList: " + numbers);
// 排序
Collections.sort(numbers);
System.out.println("Sorted ArrayList: " + numbers);
}
}
输出
Unsorted ArrayList: [4, 2, 3]
Sorted ArrayList: [2, 3, 4]
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
// 初始化集合
ArrayList<Integer> numbers = new ArrayList<>();
// 添加元素
numbers.add(1);
numbers.add(2);
numbers.add(3);
System.out.println("Sorted ArrayList: " + numbers);
// 洗牌
Collections.shuffle(numbers);
System.out.println("ArrayList using shuffle: " + numbers);
}
}
输出
Sorted ArrayList: [1, 2, 3]
ArrayList using shuffle: [1, 2, 3]
import java.util.ArrayList;
import java.util.Collections;
public class Main16 {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
System.out.println("ArrayList: " + numbers);
Collections.reverse(numbers);
System.out.println("Reversed ArrayList: " + numbers);
}
}
输出
ArrayList: [1, 2]
Reversed ArrayList: [2, 1]
import java.util.ArrayList;
import java.util.Collections;
public class Main16 {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
System.out.println("ArrayList: " + numbers);
Collections.fill(numbers, 0);
System.out.println("ArrayList using fill(): " + numbers);
}
}
输出
ArrayList: [1, 2]
ArrayList using fill(): [0, 0]
import java.util.ArrayList;
import java.util.Collections;
public class Main16 {
public static void main(String[] args) {
ArrayList<Integer> numbers1 = new ArrayList<>();
numbers1.add(1);
numbers1.add(2);
System.out.println("ArrayList1: " + numbers1);
ArrayList<Integer> numbers2 = new ArrayList<>();
numbers2.add(3);
numbers2.add(4);
numbers2.add(5);
numbers2.add(6);
System.out.println("ArrayList2: " + numbers2);
Collections.copy(numbers2, numbers1);
System.out.println("ArrayList using copy(): " + numbers2);
}
}
输出
ArrayList1: [1, 2]
ArrayList2: [3, 4, 5, 6]
ArrayList using copy(): [1, 2, 5, 6]
import java.util.ArrayList;
import java.util.Collections;
public class Main16 {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
System.out.println("ArrayList: " + numbers);
Collections.swap(numbers, 0,1);
System.out.println("ArrayList using swap(): " + numbers);
}
}
输出
ArrayList: [1, 2]
ArrayList using swap(): [2, 1]
import java.util.ArrayList;
public class Main16 {
public static void main(String[] args) {
ArrayList<Integer> numbers1 = new ArrayList<>();
numbers1.add(1);
numbers1.add(2);
System.out.println("ArrayList1: " + numbers1);
ArrayList<Integer> numbers2 = new ArrayList<>();
numbers2.add(3);
numbers2.add(4);
System.out.println("ArrayList2: " + numbers2);
numbers1.addAll(numbers2);
System.out.println("ArrayList using addAll(): " + numbers1);
}
}
输出
ArrayList1: [1, 2]
ArrayList2: [3, 4]
ArrayList using addAll(): [1, 2, 3, 4]
import java.util.ArrayList;
import java.util.Collections;
public class Main16 {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
int pos = Collections.binarySearch(numbers, 3);
System.out.println("The position of 3 is " + pos);
}
}
输出
The position of 3 is 2
import java.util.ArrayList;
import java.util.Collections;
public class Main16 {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(2);
System.out.println("ArrayList: " + numbers);
int count = Collections.frequency(numbers, 2);
System.out.println("Count of 2: " + count);
}
}
输出
ArrayList: [1, 2, 3, 2]
Count of 2: 2
import java.util.ArrayList;
import java.util.Collections;
public class Main16 {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(2);
System.out.println("ArrayList1: " + numbers);
ArrayList<Integer> newNumbers = new ArrayList<>();
newNumbers.add(5);
newNumbers.add(6);
System.out.println("ArrayList2: " + newNumbers);
boolean value = Collections.disjoint(numbers, newNumbers);
System.out.println("Two lists are disjoint: " + value);
}
}
输出
ArrayList1: [1, 2, 3, 2]
ArrayList2: [2, 6]
Two lists are disjoint: false
import java.util.ArrayList;
import java.util.Collections;
public class Main16 {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
int min = Collections.min(numbers);
System.out.println("Minimum Element: " + min);
int max = Collections.max(numbers);
System.out.println("Maximum Element: " + max);
}
}
输出
Minimum Element: 1
Maximum Element: 3