java对两个字符串数组取交集、并集和差集直接上代码。
import java.util.*;
public class StringArrayUtil {
// 求两个字符串数组的并集,利⽤set的元素唯⼀性
public static String[] union(String[] arr1, String[] arr2) {
Set<String> set = new HashSet<>();
Collections.addAll(set, arr1);
Collections.addAll(set, arr2);
String[] result = {};
Array(result);
}
/
/ 求两个数组的交集
public static String[] intersect(String[] arr1, String[] arr2) {
Map<String, Boolean> map = new HashMap<>();
LinkedList<String> list = new LinkedList<>();
for (String str : arr1) {
if (!ainsKey(str)) {
map.put(str, Boolean.FALSE);
}
string字符串转化数组}
for (String str : arr2) {
if (ainsKey(str)) {
map.put(str, Boolean.TRUE);
}
}
for (Map.Entry<String, Boolean> e : Set()) {
if (e.getValue().equals(Boolean.TRUE)) {
list.Key());
}
}
String[] result = {};
Array(result);
}
/
/ 求两个数组的差集
public static String[] minus(String[] arr1, String[] arr2) {
LinkedList<String> list = new LinkedList<>();
LinkedList<String> history = new LinkedList<>();
String[] longerArr = arr1;
String[] shorterArr = arr2;
// 出较长的数组来减较短的数组
if (arr1.length > arr2.length) {
longerArr = arr2;
shorterArr = arr1;
}
for (String str : longerArr) {
if (!ains(str)) {
list.add(str);
}
}
for (String str : shorterArr) {
if (ains(str)) {
history.add(str);
} else {
if (!ains(str)) {
list.add(str);
}
}
}
String[] result = {};
Array(result);
}
}
字符串数组的操作在平常的业务开发中⽤得⽐较多,是每个Java开发者都应当掌握的技能。"⼤⼈的每⼀次流泪,都是⼀场⽆声的孤独。"

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。