Find word in a generated code that stored in hashset

hashset contains java
hashset contains time complexity
hashset get
hashset contains c#
set in java
hashmap in java
hashset vs hashmap
hashset iterator

Example generated code: 910love009tre

I would like to check if the generated code has a specific word inside it. I'm trying to use the contains-method, but it looks like it doesn't give me the desired output.

import java.util.HashSet;
import java.util.Set;

public class AlphaNumericExample {
  public static void main(String[] args) {

    enter code here
    AlphaNumericExample example = new AlphaNumericExample();
    Set<String> codes = new HashSet<>();
    for (int x = 0;x< 100 ;x++ ) {

    System.out.println(codes.contains("love"));//Im trying to check if the generated codes that have been stored in hashset have form a word 

    System.out.println("Size of the set: "+codes.size());


  public String getAlphaNumeric(int len) {

    char[] ch = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();

    char[] c = new char[len];
    SecureRandom random = new SecureRandom();//
    for (int i = 0; i < len; i++) {
      c[i] = ch[random.nextInt(ch.length)];

    return new String(c);

Use it like this

for (int x = 0; x < 100; x++) {
    String generated = example.getAlphaNumeric(16);

HashSet contains() Method in Java, HashSet.contains() method is used to check whether a specific element is present in the HashSet or not. Java code to illustrate HashSet.contains() method. When an object is added to a Hashtable, it is stored in the bucket that is associated with the hash code that matches the object's hash code. When a value is being searched for in the Hashtable, the hash code is generated for that value, and the bucket associated with that hash code is searched.

In this case, the contains method checks if the entire object matches. Not if it contains that specific char sequence, so a 13 character object will never match a 4 letter sequence

instead of

System.out.println(codes.contains("love"));//Im trying to check if the generated codes that have been stored in hashset have form a word 

try the contains method on each individual string, instead of the contains method of the HashSet

for (String code: codes) { 
    if  (code.contains("love")    {

HashSet in Java, The HashSet class implements the Set interface, backed by a hash table which is actually a HashMap instance. like add, remove, contains and size assuming the hash function disperses the Objects are inserted based on their hash code. of buckets will automatically get increased when the table has 12 elements in it​. 3 Find word in a generated code that stored in hashset Sep 19 '18 3 Working with week days and finding future days Jun 20 '19 2 Java Reflection, creating object of private class from different package Jun 20 '19

You are checking for the complete string "love" in the codes set. You need to check for it's presence in each code in the codes set. You can use streams for checking this.

boolean isPresent = code -> code.indexOf( "love" ) != -1 );

How HashSet Works Internally In Java?, HashSet uses HashMap internally to store it's objects. You can check this in the source code of HashSet class in JDK installation directory. But there are other approaches for which a HashSet would not be an appropriate choice. A classic approach is to create a table of <key, value> (in Java a HashMap, not a HashSet) in which the keys are a canonical representation of the bag of letters in each word, and the value is a list of words formed from those letters.

You should convert both the strings to the same case before using contains to find the match:

String str = "";
str = codes.toString();

Performance of contains() in a HashSet vs ArrayList, HashSet is a collection for storing unique elements. To learn Each bucket corresponds to a hash code generated with hashCode() method. So contains() is actually using hashCode() method to find the object's location. Objects that you insert in HashSet are not guaranteed to be inserted in same order. Objects are inserted based on their hash code. NULL elements are allowed in HashSet. HashSet also implements Searlizable and Cloneable interfaces.

Java HashSet Tutorial with Examples, HashSets are used to store a collection of unique elements. Check if the HashSet contains an element String cityName = "Paris";  This tutorial gives you simple way to find unique values in ArrayList using TreeSet and HashSet. HashSet is much faster than TreeSet (constant-time versus log-time for most operations like add, remove and contains) but offers no ordering guarantees like TreeSet.

HashSet in Java | Java HashSet Methods with Examples, How to Generate Random Numbers using Random Class in Java? Learn what is HashSet in Java and how to get started with it! of the HashMap class that uses a hashing mechanism for storing the information within a HashSet. content into a unique value that is more popularly known as hash code. Given a string find the number of meaningful words which could be formed from the string for eg. programmerit forms: pro+gram+merit program+merit programmer+it pro+grammer+it Looking for code review optimizations and best practices.

Hashing, A hash function is a function which when given a key, generates an address in the table Java provides the following classes HashMap, HashSet and some others We modify the above code by use of the HashMap class to store words along HashMap‹String, Integer>(); for (String key : data) { Integer freq = hm.get(​key);  Find length of the smallest chain from ‘start’ to ‘target’ if it exists, such that adjacent words in the chain only differ by one character and . each word in the chain is a valid word i.e., it exists in the dictionary. It may be assumed that the ‘target’ word exists in dictionary and length of all dictionary words is same.