比较两个 ID 获取较低的一个。

huangapple go评论84阅读模式
英文:

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 -&gt; dto.provider.nif)
                       .min(String::compareTo) // Optional&lt;String&gt; is returned here
                       .orElse(&quot;No minimal nif found&quot;)

If you need to get ID of the DTO with minimal nif:

lowerSupplyDeliveryTerm.stream()
                       .min(Comparator.comparing(dto -&gt; dto.provider.inf))
                       .map(dto -&gt; dto.provider.id)
                       .orElse(&quot;No ID for minimal nif found&quot;)

huangapple
  • 本文由 发表于 2020年10月22日 00:05:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/64467565.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定