[英]How to get the same position on two ArrayListif I have removed a value from the first ArrayList
I have two ArrayLists 我有两个ArrayList
ArrayList<String> firstList = new ArrayList<>(Arrays.asList("king", "king", "queen", "jack", "queen"));
ArrayList<String> secondList = new ArrayList<>(Arrays.asList("one", "one", "one", "two", "one"));
In the List named firstList
I'll remove the duplicate using Set<String> hashSet = new HashSet<>(firstList);
在名为
firstList
的列表中,我将使用Set<String> hashSet = new HashSet<>(firstList);
删除重复项Set<String> hashSet = new HashSet<>(firstList);
and the output will be "king", "queen", "jack"
输出将是
"king", "queen", "jack"
What I want to do is get the position on the secondList
equal to the position on the firstList
. 我想要做的就是对的位置
secondList
等于在位置firstList
。
The current output is (king & one), (queen, one), (jack, one)
当前输出为
(king & one), (queen, one), (jack, one)
What I want the output to be is (king, one), (queen, one), (jack, two)
我想要的输出是
(king, one), (queen, one), (jack, two)
If any of you could help me, I would appreciate it a lot. 如果有谁可以帮助我,我将不胜感激。
Assuming your two input lists are always the same length you can do something simple like: 假设两个输入列表的长度始终相同,则可以执行以下简单操作:
public static void main(String[] args) {
List<String> firstList = new ArrayList<>(Arrays.asList("king", "king", "queen", "jack", "queen"));
List<String> secondList = new ArrayList<>(Arrays.asList("one", "one", "one", "two", "one"));
List<String> cards = new ArrayList<>();
List<String> positions = new ArrayList<>();
for(int i = 0; i< firstList.size(); i++){
String temp = firstList.get(i);
if(!cards.contains(temp)){
cards.add(temp);
positions.add(secondList.get(i));
}
}
System.out.println(cards);
System.out.println(positions);
}
Use a Map
. 使用
Map
。 In Java 8, it would be (in the form of a passing unit test): 在Java 8中,它将是(以通过单元测试的形式):
final List<String> one = asList("king", "king", "queen", "jack", "queen");
final List<String> two = asList("one", "one", "one", "two", "one");
//Guards against IndexOutOfBoundsExceptions
final int size = Math.min(one.size(), two.size());
//LinkedHashMap to preserve key ordering
final Map<String,String> map = new LinkedHashMap<>(size);
for(int i = 0; i < size; i++) {
map.putIfAbsent(one.get(i), two.get(i));
}
final String actual = map.entrySet().stream()
.map(e -> String.format("[%s,%s]", e.getKey(), e.getValue()))
.collect(Collectors.joining(","));
final String expected = "[king,one],[queen,one],[jack,two]";
assertEquals(expected, actual);
For Java 7, you need to check the Map
doesn't already contain the key before you add it. 对于Java 7,您需要在添加
Map
之前先检查Map
是否不包含该键。 putIfAbsent()
does this for you otherwise. putIfAbsent()
会为您执行此操作。
You can stream the Map
values and keys afterwards if you want to do different things with them: 如果您想对
Map
值和键进行其他操作,则可以在其后流式处理:
final String keys = String.join(",", map.keySet());
assertEquals("king,queen,jack", keys);
final String values = String.join(",", map.values());
assertEquals("one,one,two", values);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.