Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {

    public static List<String> getDiff(List<String> source, List<String> target) {
        if (source.size() == 0 || target.size() == 0) {
            return null;
        }
        if (source == null || target == null) {
            return null;
        }
        int frequency = 1;
        List<String> max;
        List<String> min;
        List<String> diff = new ArrayList<>();
        if (source.size() > target.size()) {
            max = source;
            min = target;
        } else {
            max = target;
            min = source;
        }

        Map<String, Integer> map = new HashMap<>();
        max.stream().forEach(s -> map.put(s, frequency));

        min.stream().forEach(s -> {
            if (map.containsKey(s)) {
                map.put(s, map.get(s) + 1);//rewrite value
            } else {
                map.put(s, frequency);
            }
        });

        //get value=1
        map.forEach((k, v) -> {
            if (v == 1) {
                diff.add(k);
            }
        });

        return diff;
    }
}