[Java] Map comparison

Map comparison method

·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

[Java] Map comparison
JAVA (Map)
Java framework comparison
Java version notation comparison
Java bidirectional map library
[Development] Java framework comparison
[Java] How to use Map
[Java] How to use Map
java ArrayList, Vector, LinkedList comparison
[Java] Type conversion speed comparison
How to use Java Map
Java
Java
[Java] Stream (filter, map, forEach, reduce)
[Java] Collection-List / Set / Map / Stack / Queue
Java comparison using the compareTo () method
Use composite keys in Java Map.
[Java] Correct comparison of String type
Java8 list conversion with Stream map
Java learning (0)
Studying Java ―― 3
[Java] array
Java protected
[Java] Annotation
[Java] Module
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
[Java] Get List / Map elements with Iterator
Java methods
Java method
java (constructor)
Regarding String type equivalence comparison in Java
Java array
java (override)
java (method)
Java Day 2018
Java string
Duplicate Map sorted by key in Java
Java static
Java serialization
java beginner 4
JAVA paid
Studying Java ―― 4
Java (set)
[Java] compareTo
Studying Java -5
Java Integer comparison (==) does not work correctly
java reflexes
java (interface)
Map without using an array in java
Java memorandum
☾ Java / Collection
Java array
[Java] Array
Reverse Key from Value in Java Map
Studying Java # 0
Java review
java framework
Java features