英文:
Comparing two ids to get the lower
问题
我尝试比较两个 ID 以获取较低的 ASCII 值。
这些 ID 是字母数字混合的 "836778436K"
,"K8474874"
....
我尝试了以下方法:
lowerSupplyDeliveryTerm.stream()
.map(dto -> dto.provider.nif)
.min(Comparator.comparing(providerNif -> providerNif))
SupplyDto
类如下:
public class SupplyDto {
public String id;
public long version;
public SupplierProviderDto provider = new SupplierProviderDto();
public SuppliedSparePartDto sparePart = new SuppliedSparePartDto();
public double price;
public int deliveryTerm;
public static class SupplierProviderDto {
public String id;
public String nif;
public String name;
}
public static class SuppliedSparePartDto {
public String id;
public String code;
public String description;
}
}
问题是我不知道在 min
函数中应该引入哪个 lambda 函数来比较 dto 中提供者的 nif。
提前谢谢。
英文:
Im trying to compare two ids to get the lower in ascii.
The ids are alphanumeric "836778436K"
, "K8474874"
....
I tried with
lowerSupplyDeliveryTerm.stream()
.map(dto -> dto.provider.nif)
.min(SupplyDto::provider.nif)
The SupplyDto
class is
public class SupplyDto {
public String id;
public long version;
public SupplierProviderDto provider = new SupplierProviderDto();
public SuppliedSparePartDto sparePart = new SuppliedSparePartDto();
public double price;
public int deliveryTerm;
public static class SupplierProviderDto {
public String id;
public String nif;
public String name;
}
public static class SuppliedSparePartDto {
public String id;
public String code;
public String description;
}
}
The problem is that I don't know which lambda function should I introduce in the min function to compare the nifs of the providers in the dto.
Thanks in advance.
答案1
得分: 0
如果您只想要最低的ID,您无需为min
比较器使用任何特殊的lambda表达式。您可以直接使用Comparator.naturalOrder()
来获得min
元素,根据元素自身的compareTo
方法。
String minId = lowerSupplyDeliveryTerm.stream()
.map(dto -> dto.provider.nif)
.min(Comparator.naturalOrder()).get();
英文:
If you want just the lowest ID, you do not have to use any special lambda for the min
comparator. You can just use Comparator.naturalOrder()
to get the min
element according the the elements' own compareTo
method.
String minId = lowerSupplyDeliveryTerm.stream()
.map(dto -> dto.provider.nif)
.min(Comparator.naturalOrder()).get();
答案2
得分: 0
如果您正在寻找嵌套对象的最小“ID”,您可以按如下方式编写:
lowerSupplyDeliveryTerm.stream()
.map(dto -> dto.provider.nif)
.min(String::compareTo) // 这里返回 Optional<String>
.orElse("找不到最小的 nif")
如果您需要获取具有最小 nif 的 DTO 的 ID:
lowerSupplyDeliveryTerm.stream()
.min(Comparator.comparing(dto -> dto.provider.inf))
.map(dto -> dto.provider.id)
.orElse("找不到最小 nif 的 ID")
英文:
If you're looking for a minimal ID
of a nested object, you could have written as follows:
lowerSupplyDeliveryTerm.stream()
.map(dto -> dto.provider.nif)
.min(String::compareTo) // Optional<String> is returned here
.orElse("No minimal nif found")
If you need to get ID of the DTO with minimal nif:
lowerSupplyDeliveryTerm.stream()
.min(Comparator.comparing(dto -> dto.provider.inf))
.map(dto -> dto.provider.id)
.orElse("No ID for minimal nif found")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论