In java how does HasSet enures unique values?

Let us discuss on how the HashSet in java prevents adding the duplicate values. First let us understand how to create a HashSet and add some values to it.

public class HashSetTest{
    
    public static void main(String[] args)
    {
        HashSet<String> hashset = new HashSet>();
        hashset.add("Bangalore");
        hashset.add("Mysore");
        hashset.add("Hubli");
        System.out.println("Set is "+hashset);
    }
} 


Output would be : Hubli, Bangalore,Mysore

What about the insertion order in the above output? Does the insertion order not maintained? Yes in a HashSet the insertion order is not maintained also the values in a HashSet are not sorted.

Now to the above code let us try to add some duplicates as below:

public class HashSetTest{
    
    public static void main(String[] args)
    {
        HashSet<Object> hashset = new HashSet<Object>();
        hashset.add("Bangalore");
        hashset.add("Hubli");
        hashset.add("Mysore");
        hashset.add("Hubli");          // duplicate elements
        hashset.add("Bangalore");      // duplicate elements
        System.out.println("Set is "+hashset);
    }
}

Now the output would be: Bangalore, Mysore, Hubli
Did you notice add() method of HashSet ignored the duplicates? Now the question is how?
When you pass duplicate elements in the  add() method of the Set object, It will return false and do not add to the HashSet, as the element is already present.

Let us go into further deep:


public class HashSet<E>
extends AbstractSet<E>
implements Set<E>, Cloneable, java.io.Serializable

{
    private transient HashMap<E,Object> map;
    // Dummy value to associate with an Object in the backing Map
    private static final Object PRESENT = new Object();
    
    
    public HashSet() {
        map = new HashMap<>();
    }
    
    // SOME CODE ,i.e Other methods in Hash Set
      
    public boolean add(E e) {
        return map.put(e, PRESENT)==null;
    }  
    
    // SOME CODE ,i.e Other methods in Hash Set
}

So , we are achieving uniqueness in Set internally in java through HashMap . Whenever you create an object of HashSet it will create an object of HashMap as you can see in the italic lines in the above code .

Get selected checkbox value using jquery



In this article I will explain how to check uncheck all (select unselect all) CheckBoxes using jQuery.
The check uncheck all functionality will be done using an additional CheckBox that will act as Check All CheckBox. 


Once you double click on the file which has the same code as above, you will see the result as below:




 

How to show saved bookmarks and web history in address bar in Firefox



Hi, after I upgraded to the latest firefox my firefox browser was not showing my saved bookmarks and also the history in the address bar. Usually when I start typing few words I was getting the list of URL’s below as a suggestion which will come handy when I don’t remember the complete URL’s of some websites. 


So how to solve this problem?
  1. In the address bar, type about:config and press Enter.
    • The about:config "This might void your warranty!" warning page may appear. Click I'll be careful, I promise! to continue to the about:config page.
  2. In the Search field, type browser.urlbar.autofill.
  3. Double-click the browser.urlbar.autofill preference to set its value to true
  4. Double-click the browser.urlbar.autofill.typed preference to set its value to true

I did all changes but still bookmarks showing up?
Do your bookmarks show up in the auto-suggest drop-down list?
If not: Under Options > Privacy, in the bottom section, under Location Bar change the selection option for When using location bar, suggest: to “History and Bookmarks”.

You are done! That's it!! Your bookmarks and history will start showing up in the address bar as you start typing words.