内联对象比较 Java
Inline Object Comparison Java
我正在尝试使用以下方法对数组列表进行排序。我希望美国排在第一位,然后是英国,等等。但这似乎没有任何作用。我做错了什么?
orderedTerritories.sort((o1, o2) -> {
if (o1.getCode().equals("US*"))
return 1;
else if (o2.getCode().equals("US*"))
return 0;
else if (o1.getCode().contains("UK") && o1.getContains() != null)
return 1;
else if (o2.getCode().contains("UK") && o2.getContains() != null)
return 0;
else if (o1.getCode().equals("DE*"))
return 1;
else if (o2.getCode().equals("DE*"))
return 0;
else if (o1.getCode().equals("JP"))
return 1;
else if (o2.getCode().equals("JP"))
return 0;
else if (o1.getCode().equals("IN"))
return 1;
else if (o2.getCode().equals("IN"))
return 0;
else return 1;
});
我倾向于同意 Lew Bloch 的观点,您应该通过使用枚举来使用更面向对象的方法。
我不明白你对 Lew Bloch 发表的评论的回应。你说,"Can't since I cant hard code it in. Is there no way to do simple ordering in java?" 但你已经在当前代码中对值进行了硬编码。例如,您的代码中有这样的内容:
o1.getCode().equals("US*")
在此语句中,"US*" 是硬编码的。
以下是仅使用美国和英国代码的枚举方法示例:
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
public class Example implements Comparable<Example>
{
public enum CountryCode{
US,UK
}
public static void main(String[] args)
{
List<Example> codeList = new ArrayList<>();
codeList.add(new Example(CountryCode.UK));
codeList.add(new Example(CountryCode.US));
System.out.println("Before sort: "+codeList);
Collections.sort(codeList);
System.out.println("After sort: "+codeList);
}
private CountryCode countryCode;
public Example(CountryCode code){
this.countryCode = code;
}
public CountryCode getCountryCode(){
return countryCode;
}
public int compareTo(Example other){
return this.countryCode.compareTo(other.getCountryCode());
}
public String toString(){
return "Example: "+countryCode.toString();
}
}
由于枚举按顺序包含美国和英国,并且 Example
class 遵从枚举以实现 compareTo
List
的 [=13] =] 排序后 Example
个国家代码为美国的对象排在国家代码为英国的对象之前。
输出:
Before sort: [Example: UK, Example: US]
After sort: [Example: US, Example: UK]
最终项目:我不清楚“*”在您的代码值中代表什么。如果有您未指定的要求,则此方法可能无法按预期工作。
当 o1.getCode() 小于 o2.getCode() 时,您的排序逻辑不检查两个代码是否相同,也不检查 returns 0 而不是负值。如果不这样做,您将违反比较合同。
根据您的排序逻辑,您不能简单地依赖国家/地区代码的字符串表示形式进行排序。为了缓解它,您需要为每个国家/地区定义自定义订购值。我假设您不能将获取代码方法修改为 return 枚举或某些自定义对象。在这种情况下,您可以定义国家代码和订单之间的映射,并使用该订单值进行排序。这是一个例子:
其中一条评论已经在建议类似的方法。
private static Map<String, Interger> codeToOrderMap = new HashMap<>();
static{
codeToOrderMap.put("US*", 0);
codeToOrderMap.put("UK", 1);
codeToOrderMap.put("DE*", 2);
codeToOrderMap.put("JP", 3);
codeToOrderMap.put("IN", 4);
}
orderedTerritories.sort((o1, o2) ->
codeToOrderMap.get( o1.getCode() ).compareTo( codeToOrderMap.get( o2.getCode() );
如果不使用上述方法或自定义代码,您将不得不编写一个非常长的混乱比较器。
谢谢,我考虑了你们对以下解决方案的看法,该解决方案有效:
List<Territory> orderedTerritories = new LinkedList<>();
List<Integer> integerValues = new ArrayList<>();
Map<Integer, Territory> intToTerritoryMap = new HashMap<>();
int i = 5;
for (Territory territory : finalSet) {
int index = 5;
if (territory.getCode().equals("US*"))
index = 0;
else if (territory.getCode().contains("UK") && territory.getContains() != null)
index = 1;
else if (territory.getCode().equals("DE*"))
index = 2;
else if (territory.getCode().equals("JP"))
index = 3;
else if (territory.getCode().equals("IN"))
index = 4;
if (index < 5) {
intToTerritoryMap.put(index, territory);
integerValues.add(index);
} else {
intToTerritoryMap.put(i, territory);
integerValues.add(i++);
}
}
Collections.sort(integerValues);
integerValues.forEach(j -> {
orderedTerritories.add(intToTerritoryMap.get(j));
});
return orderedTerritories;
这是冗长且硬编码的数字。我希望有一个更简单的方法。
我正在尝试使用以下方法对数组列表进行排序。我希望美国排在第一位,然后是英国,等等。但这似乎没有任何作用。我做错了什么?
orderedTerritories.sort((o1, o2) -> {
if (o1.getCode().equals("US*"))
return 1;
else if (o2.getCode().equals("US*"))
return 0;
else if (o1.getCode().contains("UK") && o1.getContains() != null)
return 1;
else if (o2.getCode().contains("UK") && o2.getContains() != null)
return 0;
else if (o1.getCode().equals("DE*"))
return 1;
else if (o2.getCode().equals("DE*"))
return 0;
else if (o1.getCode().equals("JP"))
return 1;
else if (o2.getCode().equals("JP"))
return 0;
else if (o1.getCode().equals("IN"))
return 1;
else if (o2.getCode().equals("IN"))
return 0;
else return 1;
});
我倾向于同意 Lew Bloch 的观点,您应该通过使用枚举来使用更面向对象的方法。
我不明白你对 Lew Bloch 发表的评论的回应。你说,"Can't since I cant hard code it in. Is there no way to do simple ordering in java?" 但你已经在当前代码中对值进行了硬编码。例如,您的代码中有这样的内容:
o1.getCode().equals("US*")
在此语句中,"US*" 是硬编码的。
以下是仅使用美国和英国代码的枚举方法示例:
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
public class Example implements Comparable<Example>
{
public enum CountryCode{
US,UK
}
public static void main(String[] args)
{
List<Example> codeList = new ArrayList<>();
codeList.add(new Example(CountryCode.UK));
codeList.add(new Example(CountryCode.US));
System.out.println("Before sort: "+codeList);
Collections.sort(codeList);
System.out.println("After sort: "+codeList);
}
private CountryCode countryCode;
public Example(CountryCode code){
this.countryCode = code;
}
public CountryCode getCountryCode(){
return countryCode;
}
public int compareTo(Example other){
return this.countryCode.compareTo(other.getCountryCode());
}
public String toString(){
return "Example: "+countryCode.toString();
}
}
由于枚举按顺序包含美国和英国,并且 Example
class 遵从枚举以实现 compareTo
List
的 [=13] =] 排序后 Example
个国家代码为美国的对象排在国家代码为英国的对象之前。
输出:
Before sort: [Example: UK, Example: US]
After sort: [Example: US, Example: UK]
最终项目:我不清楚“*”在您的代码值中代表什么。如果有您未指定的要求,则此方法可能无法按预期工作。
当 o1.getCode() 小于 o2.getCode() 时,您的排序逻辑不检查两个代码是否相同,也不检查 returns 0 而不是负值。如果不这样做,您将违反比较合同。
根据您的排序逻辑,您不能简单地依赖国家/地区代码的字符串表示形式进行排序。为了缓解它,您需要为每个国家/地区定义自定义订购值。我假设您不能将获取代码方法修改为 return 枚举或某些自定义对象。在这种情况下,您可以定义国家代码和订单之间的映射,并使用该订单值进行排序。这是一个例子:
其中一条评论已经在建议类似的方法。
private static Map<String, Interger> codeToOrderMap = new HashMap<>();
static{
codeToOrderMap.put("US*", 0);
codeToOrderMap.put("UK", 1);
codeToOrderMap.put("DE*", 2);
codeToOrderMap.put("JP", 3);
codeToOrderMap.put("IN", 4);
}
orderedTerritories.sort((o1, o2) ->
codeToOrderMap.get( o1.getCode() ).compareTo( codeToOrderMap.get( o2.getCode() );
如果不使用上述方法或自定义代码,您将不得不编写一个非常长的混乱比较器。
谢谢,我考虑了你们对以下解决方案的看法,该解决方案有效:
List<Territory> orderedTerritories = new LinkedList<>();
List<Integer> integerValues = new ArrayList<>();
Map<Integer, Territory> intToTerritoryMap = new HashMap<>();
int i = 5;
for (Territory territory : finalSet) {
int index = 5;
if (territory.getCode().equals("US*"))
index = 0;
else if (territory.getCode().contains("UK") && territory.getContains() != null)
index = 1;
else if (territory.getCode().equals("DE*"))
index = 2;
else if (territory.getCode().equals("JP"))
index = 3;
else if (territory.getCode().equals("IN"))
index = 4;
if (index < 5) {
intToTerritoryMap.put(index, territory);
integerValues.add(index);
} else {
intToTerritoryMap.put(i, territory);
integerValues.add(i++);
}
}
Collections.sort(integerValues);
integerValues.forEach(j -> {
orderedTerritories.add(intToTerritoryMap.get(j));
});
return orderedTerritories;
这是冗长且硬编码的数字。我希望有一个更简单的方法。