java - 比较两个 ArrayList 中的共同项并从其中一个 ArrayList 中删除这些项

标签 java arraylist

我有两个数组列表。其中一份列出了某个州出生的所有总统(列表 A)。第二个列出了历任总统(列表 B)。我必须对两者进行比较,并打印出一份并非出生在我们当前各州的总统名单。基本上,它们不会出现在列表 A 中。因此,我必须找到列表之间的所有共同名称,并将它们从列表 B 中删除。我该如何执行此操作?

import java.util.*;
import java.io.*;

// NO CMD ARGS - ALL FILENAMES MUST BE HARDOCDED AS I HAVE DONE FOR YOU HERE

public class Potus
{
public static void main( String[] args )  throws Exception
{
    BufferedReader infile1 = new BufferedReader( new       FileReader("state2Presidents.txt") );
    BufferedReader infile2 = new BufferedReader( new FileReader("allPresidents.txt") );
    //BufferedReader infile3 = new BufferedReader( new FileReader("allStates.txt") );
    HashMap<String,ArrayList<String>> Map = new HashMap<String,ArrayList<String>>();
    HashMap<String,String> Maping = new HashMap<String,String>();
    ArrayList<String> inverting = new ArrayList<String>();
    ArrayList<String> presState = new ArrayList<String>();
    String state;
    while ((infile1.ready()))
    {
        ArrayList<String> president = new ArrayList<String>();
        state = infile1.readLine();
        String [] states = state.split(" ");
        for(int i=1; i<states.length; i++)
        {
            president.add(states[i]);
            inverting.add(states[i]);
            Maping.put(states[i],states[0]);


        }
        Map.put(states[0], president);
        presState.add(states[0]);
    }
    Collections.sort(presState);
    Collections.sort(inverting);
    System.out.println( "The following states had these presidents born in them:\n");  // DO NOT REMOVE OR MODIFY

    for(int i=0; i<presState.size(); i++)
    {
        System.out.print(presState.get(i));
        ArrayList<String> value = Map.get(presState.get(i));
        for (int j=0; j< value.size() ; j++)
        {
            System.out.print(" "+value.get(j));
        }
        System.out.println();
    }
    System.out.println( "\nList of presidents and the state each was born in:\n");  // DO NOT REMOVE OR MODIFY
    for(int i=0; i<inverting.size(); i++)
    {
        System.out.print(inverting.get(i));
        String val = Maping.get(inverting.get(i));
        System.out.println(" "+val);

    }
    System.out.println( "\nThese presidents were born before the states were formed:\n");  // DO NOT REMOVE OR MODIFY
    ArrayList<String> america = new ArrayList<String>();
    ArrayList<String> am = new ArrayList<String>();
    String l;
    String line;
    while((line = infile2.readLine()) != null) 
    {

        america.add(line);
    }
    while((l = infile1.readLine()) != null)
    {
        for(int i=1; i<l.length();i++)
        {
            am.add(l);
        }
    }
    Collections.sort(america);
    Collections.sort(am);
    america.removeAll(am);

}

最佳答案

您可以尝试list.removeAll(list)

关于java - 比较两个 ArrayList 中的共同项并从其中一个 ArrayList 中删除这些项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23098301/

相关文章:

Java,SocketChannel选择器,将写 channel 与阻塞队列结合起来

java - Hibernate中如何实现复合主键?

java - jackson 为所有日期字段设置全局日期格式

java - 是否可以在 java 中定义与基元一起使用的自定义类型?

java - 在ArrayList中存储对象时出错

java - 将 spring-reactor 集成到现有 Spring Framework 4 STOMP Over WebSocket 应用程序中

java - Android ArrayList 的自定义对象问题

java - 将 arraylist(integer) 转换为整数数组

java - Java ArrayList 迭代器中可能的内存泄漏

arraylist - 如何动态地将类放入<>