·Thing you want to do I want to compare List A and List B and extract only those that are in both and those that are in List A. I want to change the process when updating and when new
qiita.rb
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
// MAP-A
Map<String, String> listA = new HashMap<String, String>();
listA.put("fruits","apple");
listA.put("animal","cat");
listA.put("sports","soccer");
listA.put("drink","water");
// MAP-B
Map<String, String> listB = new HashMap<String, String>();
listB.put("sports","soccer");
listB.put("fruits","apple");
listB.put("color","red");
//Set<Map.Entry<K,V>>If it is also in List B, update it, if not, new
for(Map.Entry<String,String> entry : listA.entrySet()){
String key = entry.getKey();
String val = entry.getValue();
if(listB.get(key) != null){
entry.setValue(val + "Koshin");
} else {
entry.setValue(val + "Shinki");
}
}
System.out.println(listA);
}
}
It was actually more complicated, but when simplified, it looks like this. First, prepare list C, throw common elements into list C, delete the elements from lists A and B, and finally, if there are any elements left in list A, add them as new. I made it into a shape, but when I made a change to that element while turning it with an iterator, it seemed to be useless, and an error occurred, so I changed it. This is nice to be cleaner.
Recommended Posts