我有两个arrayLists。其中一位列出了在一个州出生的所有总统(列表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);

}

最佳答案

您可以尝试listB.removeAll(listA)

08-05 11:23