gpt4 book ai didi

java - 如何将重复值列表转换为 T 类字段的不同值列表

转载 作者:行者123 更新时间:2023-11-30 05:24:21 26 4
gpt4 key购买 nike

我有一个List<String>其中包含名称,我想将该列表转换为其他唯一名称列表,我找到了一种效果很好的方法,即:

public class Main {

public Main() {
List<String> nameList = new ArrayList<>();

nameList.add("Ali");
nameList.add("Al");
nameList.add("Ali");
nameList.add("Al");
nameList.add("Ai");
nameList.add("li");
nameList.add("li");
nameList.add("Ai");
System.out.print("All names are: ");
for (String string : nameList) {
System.out.print(string + ", ");
}
System.out.println("");

System.out.print("Unique names are: ");

for (String string : convertToUniqueList(nameList)) {
System.out.print(string + ", ");
}

}

public List<String> convertToUniqueList(List<String> listInts) {
List<String> listDistinctInts = new ArrayList<>(listInts.size());
for (String i : listInts) {
if (!listDistinctInts.contains(i)) {
listDistinctInts.add(i);
}
}
return listDistinctInts;
}

public static void main(String[] args) {
new Main();
}

}

所有事情都运行良好,我得到了想要的结果,但我想对以下场景实现与上面相同的逻辑:

  @Data // lombok annotation
public class Foo {
String name;
int age;
List<Bar> barList = new ArrayList<>();
}

@Data // lombok annotation
public class Bar {
String bloodGroup;
String dayOfPassing;
}

我想从我尝试过的 Main.java 中的杂项列表中获取具有唯一日期名称的列表:

public class Main {

public Main() {
Foo foo1 = new Foo();

foo1.setName("Foo One");
foo1.setAge(21);

List<Bar> barList = new ArrayList<>();

Bar bar1 = new Bar();
bar1.setBloodGroup("O +ve");
bar1.setDayOfPassing("Wednesday");

Bar bar2 = new Bar();
bar2.setBloodGroup("O +ve");
bar2.setDayOfPassing("Wednesday");

Bar bar3 = new Bar();
bar3.setBloodGroup("O +ve");
bar3.setDayOfPassing("Thursday");

Bar bar4 = new Bar();
bar4.setBloodGroup("O -ve");
bar4.setDayOfPassing("Friday");

barList.add(bar1);
barList.add(bar2);
barList.add(bar3);
barList.add(bar4);

System.out.print("All Bars with all days are: ");
for (Bar bar : barList) {
System.out.print(bar.dayOfPassing + ", ");
}
System.out.println("");

System.out.print("Bars with Unique days are: ");
int count = 0;
for (Bar bar : getBarsWithUniqueDays(barList, count)) {
System.out.print(bar.dayOfPassing + ", ");
count++;
}

}

public List<Bar> getBarsWithUniqueDays(List<Bar> barList, int count) {
List<Bar> listDistinctBars = new ArrayList<>(barList.size());

for (Bar bar : barList) {
if (!listDistinctBars.get(count).getDayOfPassing().contains(bar.dayOfPassing)) {
listDistinctBars.add(bar);
}
count++;
}
return listDistinctBars;
}

public static void main(String[] args) {
new Main();
}
}

使用上面的代码我得到异常:

All Bars with all days are: Wednesday, Wednesday, Thursday, Friday, 
Bars with Unique days are: Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at Main.getBarsWithUniqueDays(Main.java:124)
at Main.<init>(Main.java:113)
at Main.main(Main.java:133)

我已经搜索并尝试了很多但徒劳,任何解决这个问题的方法......

最佳答案

    Map<String, Bar> map = new LinkedHashMap<>();
for (Bar ays : barList) {
map.put(ays.dayOfPassing, ays);
}
barList.clear();
barList.addAll(map.values());


for (Bar ays : barList) {

System.out.println("Unique names are: "+ays.dayOfPassing);
}

关于java - 如何将重复值列表转换为 T 类字段的不同值列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58975032/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com