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.HashSet;
import java.util.List;
import java.util.Set;

public class Main {
    /**
     * Filters duplicate formulas from the given list of formulas.<br/>
     * <b>TODO:</b> Filter method does not consider AST, it compares only the string representation.
     * @param formulas Formulas, which shall be filtered (may contain duplicates). Must not be <tt>null</tt>.
     * @return The filtered list of duplicate, won't be <tt>null</tt>.
     */
    public static List<String> filterDuplicates(List<String> formulas) {
        // Current equality check is only done on String.equals -> this could be improved
        Set<String> alreadyCollectedFormulas = new HashSet<>();
        List<String> results = new ArrayList<>();
        for (int i = 0; i < formulas.size(); i++) {
            String currentFormula = formulas.get(i);
            if (!alreadyCollectedFormulas.contains(currentFormula)) {
                alreadyCollectedFormulas.add(currentFormula);
                results.add(currentFormula);
            }
        }

        if (results.size() < formulas.size()) {
            int skipped = formulas.size() - results.size();
            System.out.println("Skipped " + skipped + " from " + formulas.size());
        }

        return results;
    }
}