Java数字范围计算,求大神看看怎么写,这个数字区间组成的字符串是什么意思?我写的是不是错了啊

现有两个以下任意格式的数字(int类型)区间组成的字符串,如10-15,

现有两个字符串A和B,完成以下两个方法:

  1. 求两个字符串的交集,如:"1-10"和"3-15"两个的交集为"3-10";
  2. 求两个字符串的合集,如: "1-10"和"3-15"两个的合集为"1-15";

 

package intersectionarrays;
import java.util.Arrays;
import java.util.HashSet;
public class IntersectionIntegerArrays {
	public static void main(String[] args) {
		Integer[] firstArray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
		Integer[] secondArray = { 1, 3, 5, 7, 9 };
		Integer[] thirdArray = { 1, 5, 9 };
		HashSet<Integer> set = new HashSet<>();
		set.addAll(Arrays.asList(firstArray));
		set.retainAll(Arrays.asList(secondArray));
		set.retainAll(Arrays.asList(thirdArray));
		System.out.println(set);
		// convert to array
		Integer[] intersection = {};
		intersection = set.toArray(intersection);
		System.out.println(Arrays.toString(intersection));
	}
}

[1, 5, 9]
[1, 5, 9]

package intersectionarrays;

import java.util.Arrays;
import java.util.HashSet;

public class IntersectionStringArrays2 {
	public static void main(String[] args) {
		String str1 = "1,2,3,4,5,6,7,8,9,10";
		String str2 = "3,4,5,6,7,8,9,10,11,12,13,14,15";
		String[] firstArray = str1.split(",");
		String[] secondArray = str2.split(",");

		HashSet<String> set = new HashSet<>();

		set.addAll(Arrays.asList(firstArray));

		set.retainAll(Arrays.asList(secondArray));

		System.out.println(set);

		String[] intersection = {};
		intersection = set.toArray(intersection);

		System.out.println(Arrays.toString(intersection));

		HashSet<String> set2 = new HashSet<>();

		set2.addAll(Arrays.asList(firstArray));

		set2.addAll(Arrays.asList(secondArray));

		System.out.println(set2);

		String[] union = {};
		union = set2.toArray(union);

		System.out.println(Arrays.toString(union));
	}
}

[3, 4, 5, 6, 7, 8, 9, 10]
[3, 4, 5, 6, 7, 8, 9, 10]
[11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]