简体   繁体   English

如果我从第一个ArrayList中删除了一个值,如何在两个ArrayList上获得相同的位置

[英]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.

相关问题 如何从ArrayList &lt;&gt;获取位置 - How can I get Position from ArrayList<> 如果第二个数组中的值与第一个数组中的值相同,如何从另一个数组中删除一个值 - How to remove one arraylist value from another arraylist if the value in second array is same in first arraylist 如何从 ArrayList 的前 N 个值中获取最大值? - How to get maximum value from of the first N values of an ArrayList? 为什么我从arrayList的不同对象中获得相同的值? - Why do I get the same value from different objects an an arrayList? Java ArrayList-值,而不是从ArrayList中删除的项目 - Java ArrayList - Value, and not Item Removed from ArrayList 从具有两个不同大小/长度的两个arraylist获取相同值的索引 - Get index of the same value from two arraylist with two different sizes/length 如果我有ArrayList,如何比较两个HashMap? - How to compare two HashMap if I have ArrayList? 从arraylist获取字符串的最佳方法是什么,其中我有字符串的第一部分 - What is the best way to get a string from an arraylist where i have the first part of the string 如何新增清单 <String> 在数组列表的选定位置具有相同的值 - How to add List<String> with the same value at selected position to an arraylist 如何从arraylist获得价值? - How to get value from arraylist?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM