All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.elibracha.compare.MapKeyDiff Maven / Gradle / Ivy

There is a newer version: 2.3.6
Show newest version
package com.github.elibracha.compare;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * compare two Maps by key
 *
 * @author QDesrame
 */
public class MapKeyDiff {

  private Map increased;
  private Map missing;
  private List sharedKey;

  private MapKeyDiff() {
    this.sharedKey = new ArrayList<>();
    this.increased = new LinkedHashMap<>();
    this.missing = new LinkedHashMap<>();
  }

  public static  MapKeyDiff diff(Map mapLeft, Map mapRight) {
    MapKeyDiff instance = new MapKeyDiff<>();
    if (null == mapLeft && null == mapRight) return instance;
    if (null == mapLeft) {
      instance.increased = mapRight;
      return instance;
    }
    if (null == mapRight) {
      instance.missing = mapLeft;
      return instance;
    }
    instance.increased = new LinkedHashMap<>(mapRight);
    instance.missing = new LinkedHashMap<>();
    for (Entry entry : mapLeft.entrySet()) {
      K leftKey = entry.getKey();
      V leftValue = entry.getValue();
      if (mapRight.containsKey(leftKey)) {
        instance.increased.remove(leftKey);
        instance.sharedKey.add(leftKey);

      } else {
        instance.missing.put(leftKey, leftValue);
      }
    }
    return instance;
  }

  public Map getIncreased() {
    return increased;
  }

  public Map getMissing() {
    return missing;
  }

  public List getSharedKey() {
    return sharedKey;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy