Start learning free

LESSON 196 OF 671 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V5 in Java: Rejecting Common Passwords with a HashSet

Practice Read the full explanation

Use a dictionary (or set) as a fast lookup table to reject well-known weak passwords.

You start with:

Set<String> common = new HashSet<>(Arrays.asList("password", "123456", "qwerty", "letmein"));
String password = "qwerty";

Build:

Password: qwerty
Common: True
Weak: reject

Rules:

  • Print "Password: " followed by the password.
  • Look the password up in the set common and store the result as a boolean.
  • Print "Common: " followed by the boolean. Write it as True or False with a capital letter, exactly as in the example (your language's own boolean prints differently, so choose the text yourself).
  • If it is common, print "Weak: reject"; otherwise print "Strong: accept".

Try other passwords: with Tr0ub4dor the output is

Password: Tr0ub4dor
Common: False
Strong: accept

Use a Set<String> built from Arrays.asList(...) and call .contains(password).

Set<String> common = new HashSet<>(Arrays.asList("password", "123456", "qwerty", "letmein"));
String password = "qwerty";

System.out.println("Password: " + password);
boolean isCommon = common.contains(password);
System.out.println("Common: " + (isCommon ? "True" : "False"));
if (isCommon) {
    System.out.println("Weak: reject");
} else {
    System.out.println("Strong: accept");
}

STEP 1 OF 5
    Ctrl + Enter to check

    JAVA · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V5 in Java, explained

    Problem overview

    Some passwords are weak whatever their characters look like, because everyone tries them first. This version rejects those. The program starts with a Set<String> named common holding four well-known passwords (password, 123456, qwerty and letmein) and a String password to check.

    It prints the password, prints whether it is in the set, and then prints a verdict: Weak: reject for a common password and Strong: accept otherwise. For qwerty:

    Password: qwerty
    Common: True
    Weak: reject

    For Tr0ub4dor, which is not in the set:

    Password: Tr0ub4dor
    Common: False
    Strong: accept

    Approach: ask a set

    The question is "is this exact text one of the known passwords?" A HashSet answers it directly. It stores each value once and finds a value by its hash code, so contains takes constant time on average, whether the set holds four passwords or four million. A real list of common passwords can be very long, and this is why a set is the right structure. Looping over a list with equals would work but takes time proportional to the list's size.

    The program does not need to search for a position or count anything: it needs one boolean, and the verdict follows from it. Storing the answer in a variable, isCommon, lets the program use it twice, once for the printed line and once for the decision.

    Step by step

    1. Print the password. The line is Password: joined to password with +.
    2. Look it up. boolean isCommon = common.contains(password); is true when the set holds that exact text.
    3. Print the answer. The text Common: followed by True or False. Java prints a boolean in lowercase, so the program picks the text with isCommon ? "True" : "False".
    4. Branch on the result. An if (isCommon) prints Weak: reject.
    5. Add the else. When the password is not common it prints Strong: accept.

    The solution in Java

    Set<String> common = new HashSet<>(Arrays.asList("password", "123456", "qwerty", "letmein"));
    String password = "qwerty";
    
    System.out.println("Password: " + password);
    boolean isCommon = common.contains(password);
    System.out.println("Common: " + (isCommon ? "True" : "False"));
    if (isCommon) {
        System.out.println("Weak: reject");
    } else {
        System.out.println("Strong: accept");
    }
    

    The Java details worth a closer look:

    • Building the set. Arrays.asList(...) turns the four strings into a fixed-size list, and new HashSet<>(...) copies them into a set. The variable has the interface type Set<String>, which is the usual way to declare it.
    • Imports. Set, HashSet and Arrays are in java.util, so a standalone file needs import java.util.*;.
    • contains uses equals. Strings are objects, and comparing them with == checks whether they are the same object, not whether they have the same characters. new String("qwerty") == "qwerty" is false, while equals gives true. A set relies on hashCode and equals, so a String built at run time is found just as well as a literal.
    • The set has no order. A HashSet does not remember insertion order. The program never iterates over it, so the order does not matter here.
    • The boolean as text. Without the conditional operator, "Common: " + isCommon prints Common: true. The parentheses around the conditional are required, because + binds tighter than ? :.

    Complexity

    Time: one contains call, which is O(1) on average for the lookup plus the time to hash the password, proportional to its length. Space: O(m) for a set of m common passwords, which the program builds once.

    Edge cases and common mistakes

    • Case matters. Qwerty is not the same text as qwerty, so it is not found: the output is Common: False and Strong: accept. Lowercasing the password before the lookup, with password.toLowerCase(), would catch such variants, but this lesson does an exact match.
    • Not common does not mean strong. The verdict says Strong: accept for any password missing from the set, even a very short one. This version only checks the list.
    • Whitespace. A password with a trailing space, such as "qwerty ", is a different string and is not found.
    • Deciding with ==. Comparing a password to a set element with == checks references. Use contains or equals.
    • Printing before looking up. The order of the output is fixed: the password line first, then Common, then the verdict. Compute isCommon before printing the second line.
    • Using a List for the lookup. list.contains gives the same answers but scans the list from the start, so it slows down as the list grows.

    Testing

    • qwerty gives Password: qwerty, Common: True and Weak: reject.
    • Tr0ub4dor gives Password: Tr0ub4dor, Common: False and Strong: accept.
    • Qwerty gives Password: Qwerty, Common: False and Strong: accept.
    Keep going with Java650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free