java toprimitive,如何在Java中呈现可为空的原始类型int?

I am designing an entity class which has a field named "documentYear", which might have unsigned integer values such as 1999, 2006, etc. Meanwhile, this field might also be "unknown", that is, not sure which year the document is created.

Therefore, a nullable int type as in C# will be well suited. However, Java does not have a nullable feature as C# has.

I have two options but I don't like them both:

Use java.lang.Integer instead of the primitive type int;

Use -1 to present the "unknown" value

Does anyone have better options or ideas?

Update: My entity class will have tens of thousands of instances; therefore the overhead of java.lang.Integer might be too heavy for overall performance of the system.

解决方案

You're going to have to either ditch the primitive type or use some arbitrary int value as your "invalid year".

A negative value is actually a good choice since there is little chance of having a valid year that would cause an integer overflow and there is no valid negative year.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Java ,可以使用集合来求两个数组的交集。 首先,将两个数组转换为集合,可以使用 Arrays.asList() 方法。然后,可以使用 Collection 类的 retainAll() 方法来计算两个集合的交集。 retainAll() 方法会修改调用它的集合,使其只包含与传递给它的集合都存在的元素。 例如,假设你有两个数组,分别为 int[] arr1 和 int[] arr2,你可以这样计算它们的交集: ``` List<Integer> list1 = Arrays.asList(ArrayUtils.toObject(arr1)); List<Integer> list2 = Arrays.asList(ArrayUtils.toObject(arr2)); list1.retainAll(list2); int[] intersection = ArrayUtils.toPrimitive(list1.toArray(new Integer[0])); ``` 注意,上面的代码使用了 Apache Commons Lang 库的 ArrayUtils 工具类来将数组转换为集合,并将集合转换回数组。如果你不想使用这个库,也可以手动实现这个转换。 最后,交集的结果存储在 int[] intersection 。 ### 回答2: 可以使用HashSet来求解数组的交集。 首先,我们可以创建一个HashSet去存储第一个数组的元素。接着,我们遍历第二个数组,判断每个元素是否在HashSet存在。如果存在,我们将该元素存储到另一个HashSet,这个HashSet就是我们想要的交集。 以下是一个示例代码: ``` import java.util.Arrays; import java.util.HashSet; public class Main { public static void main(String[] args) { int[] arr1 = {1, 2, 3, 4, 5}; int[] arr2 = {4, 5, 6, 7, 8}; HashSet<Integer> set1 = new HashSet<>(); HashSet<Integer> intersect = new HashSet<>(); for (int num : arr1) { set1.add(num); } for (int num : arr2) { if (set1.contains(num)) { intersect.add(num); } } System.out.println("数组的交集:" + intersect.toString()); } } ``` 运行以上代码,输出结果为: ``` 数组的交集:[4, 5] ``` 通过这种方法,我们可以轻松地找到两个数组的交集。 ### 回答3: 要求实现一个Java函数,该函数可以找到两个给定数组的交集元素。 可以使用HashSet来解决这个问题。首先,创建一个HashSet来存储第一个数组的所有元素。然后,遍历第二个数组,对于每个元素,检查它是否在HashSet出现过。 如果一个元素在HashSet出现过,那么它是两个数组的交集元素之一。因此,将该元素添加到一个结果集合,以便最后返回。 以下是一个示例实现: ```java import java.util.HashSet; import java.util.Set; public class ArrayIntersection { public static int[] findIntersection(int[] nums1, int[] nums2) { Set<Integer> set = new HashSet<>(); Set<Integer> intersection = new HashSet<>(); for (int num : nums1) { set.add(num); } for (int num : nums2) { if (set.contains(num)) { intersection.add(num); } } int[] result = new int[intersection.size()]; int index = 0; for (int num : intersection) { result[index++] = num; } return result; } public static void main(String[] args) { int[] nums1 = {1, 2, 2, 1}; int[] nums2 = {2, 2}; int[] intersection = findIntersection(nums1, nums2); // 输出交集元素 for (int num : intersection) { System.out.print(num + " "); } } } ``` 在上面的示例,我们使用HashSet将数组`nums1`的元素存储在集合。然后,遍历数组`nums2`,并检查每个元素在HashSet是否存在。如果存在,将其添加到结果集合`intersection`。 最后,我们将结果集合转换为一个整型数组,并将其返回。在main函数,我们使用示例数组调用`findIntersection`函数,并输出结果。 以上代码输出为:`2`,表示两个数组的交集元素为`2`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值