I have two arrayLists. One of them lists all of the presidents born in a state(List A). The second lists all of the presidents ever(List B). I have to compare the two and print out a list of presidents who were not born in any of our current states. Basically, they would not be in List A. So I have to find all names in common between the lists and delete them from list B. How do I go about doing this?
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)
Put all your presidents from List
A in a Set
, then iterate through List
B, checking for each item if it belongs to A, if so add the element's index to a third list. After that iterate through your indexes list removing the items from List
B. This runs in O(m+n), if deletions from B are O(1).
If the lists are relatively small though I would use removeAll()
as suggested in another answer.
There are multiple ways to do this. One, as stated by Evgeniy Dorofeev, you can use listA.removeAll( listB );
You can also try iterating through all the elements of ListB (assuming the lists are of type String because you haven't specified)
for( String s : ListA ) {
if ( ListB.contains( s ) )
ListB.remove( ListB.indexOf( s ) );
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.