-1

In the javadocs, a FindDups2 class is outlined that will remove duplicates from a set. I copied that code into IntelliJ but now I'm not sure what to call it on. I have created an arrayList in another class in the same folder and tried calling FindDups2 in my arrayList class and tried using the arrayList in the FindDups2 class.

I'm confused because I would know how to call it if it were a method, but I'm not sure how to call a class (FindDups2) on an instance of a class (allFriends) in another class (arrayList). I'm also confused since this the FindDups class doesn't have a constructor. My intuition would be to instantiate my array list instance (allFriends) as an instance variable in the FindDups class and then call it in the findDups class.

PS - this is my first time posting on stackOverflow and I'm a newbie to programming so please be kind and let me know if I'm missing any community norms!

Here's the code I have from the javadocs:

import java.util.*;


/**
 * This program removes all duplicate items in a set
 * & prints out which words were unique & which were dups.
 */

public class FindDups {
    public arrayList allFriends;
    public static void main(String[] args) {

        Set<String> uniques = new HashSet<String>();
        Set<String> dups    = new HashSet<String>();

        for (String a : args)
            if (!uniques.add(a))
                dups.add(a);

        // Destructive set-difference
        uniques.removeAll(dups);

        System.out.println("Unique words:    " + uniques);
        System.out.println("Duplicate words: " + dups);
    }
}
Tim Moore
  • 8,958
  • 2
  • 23
  • 34
selena
  • 1
  • 1

1 Answers1

0

This code wasn't written in a way that can be easily reused in a larger program. It can only be run from the command-line. Some refactoring is necessary to make it into a reusable method. Here's one possibility:

import java.util.*;


/**
 * This program removes all duplicate items in a set
 * & prints out which words were unique & which were dups.
 */

public class FindDups {
    public static void main(String[] args) {
        List<String> words = Arrays.asList(args);

        Result result = findDups(words);

        System.out.println("Unique words:    " + result.uniques);
        System.out.println("Duplicate words: " + result.dups);
    }

    public static class Result {
        final Set<String> uniques;
        final Set<String> dups;

        public Result(Set<String> uniques, Set<String> dups) {
            this.uniques = uniques;
            this.dups = dups;
        }
    }

    public static Result findDups(List<String> words) {
        Set<String> uniques = new HashSet<String>();
        Set<String> dups    = new HashSet<String>();

        for (String word : words)
            if (!uniques.add(word))
                dups.add(word);

        // Destructive set-difference
        uniques.removeAll(dups);

        return new Result(uniques, dups);
    }
}

This can then be used from another class as:

FindDups.Result result = FindDups.findDups(allFriends);

Note that it is not required to define an explicit constructor in a Java class. If no constructors are provided, a default, zero-argument constructor is generated by the compiler. In this case, because the class does not need to be instantiated, it is a good practice to define an explicit private constructor so that trying to create one with new FindDups() will be disallowed.

Other notes:

  1. If you don't need to run FindDups from the command line in your own project, you can remove its main method.
  2. It might suit your project better to copy the findDups method into one of your own classes, rather than having this FindDups class.
  3. If you don't need to know both the unique words and the duplicate words, this could be simplified by having findDups return one or the other Set<String> values instead of using the nested Result class (which could be removed in that case). If you only need the unique values, I would also rename the method to findUniques, findUniqueValues, or something more meaningful to the context where it's used.
Tim Moore
  • 8,958
  • 2
  • 23
  • 34