<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: aryan</title>
    <description>The latest articles on DEV Community by aryan (@aryan19694946).</description>
    <link>https://dev.to/aryan19694946</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F345325%2F04dbb513-b2e6-41b2-aa63-ac98fcb796f8.png</url>
      <title>DEV Community: aryan</title>
      <link>https://dev.to/aryan19694946</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aryan19694946"/>
    <language>en</language>
    <item>
      <title>Static Class in Java</title>
      <dc:creator>aryan</dc:creator>
      <pubDate>Mon, 09 Nov 2020 07:43:47 +0000</pubDate>
      <link>https://dev.to/aryan19694946/static-class-in-java-9nm</link>
      <guid>https://dev.to/aryan19694946/static-class-in-java-9nm</guid>
      <description>&lt;p&gt;Take a look at this article, we are going to study what is a static class in java and how we can create it. Later, we will discuss some implementation consideration and benefits of using a static class&lt;/p&gt;

&lt;p&gt;Visit the original article: &lt;a href="https://www.java8net.com/2020/11/static-class-in-java.html"&gt;https://www.java8net.com/2020/11/static-class-in-java.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Before starting our tutorial on the static class, we first want to briefly remind you that "What is static?",  Static is a keyword that can be used with class, variable, method, and block to create static members. Static members belong to the class instead of a specific instance, this means if you make a member static, .you can access it without an object.&lt;/p&gt;

&lt;p&gt;A static member can be:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;static variables,&lt;/li&gt;
&lt;li&gt;static methods,&lt;/li&gt;
&lt;li&gt;static block or static initialization block, and,&lt;/li&gt;
&lt;li&gt;static class&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Static classes are basically a way of grouping classes together in Java. Java doesn't allow you to create top-level static classes; only nested (inner) classes. For this reason, a static class is also known as a static inner class or static nested class. Let's have a look at how to define a static class in java. Let's have a look at how to define a static class in java.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Employee {
    private String name;
    private String email;
    private String address;

    User(String name, String email, String address) {
        this.name = name;
        this.email = email;
        this.address = address;
    }
    public String getName() {
        return name;
    }
    public String getEmail() {
        return email;
    }
    public String getAddress() {
        return address;
    }

    static class Validator {

        boolean validateEmployee(Employee employee) {
            if (!isValidName(employee.getName())) {
                return false;
            }
            if (!isValidEmail(employee.getEmail())) {
                return false;
            }
            return true;
        }
        private boolean isValidName(String name) {
            if (name == null) {
                return false;
            }
            if (name.length() == 0) {
                return false;
            }
            return true;
        }
        private boolean isValidEmail(String email) {
            if (email == null) {
                return false;
            }
            if (email.length() == 0) {
                return false;
            }
            return true;
        }
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Points to be considered while writing a static class&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It can only be a nested or inner class only.&lt;/li&gt;
&lt;li&gt;It can have any access modifier (private, protected, public, or default) like any other static member.&lt;/li&gt;
&lt;li&gt;It can only access the static members of its enclosing class.&lt;/li&gt;
&lt;li&gt;It cannot access the non-static members of its enclosing class directly. It can interact with the non-static member only through the object of its enclosing class only. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Benefits of Static class in java&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We can define the related or helper classes inside the class by making it static.&lt;/li&gt;
&lt;li&gt;It can access the private member of the enclosing class through the object reference.&lt;/li&gt;
&lt;li&gt;It provides a nice namespace for the nested class.&lt;/li&gt;
&lt;li&gt;If the enclosing class gets updated, we are able to update the static class as well at the same location.&lt;/li&gt;
&lt;li&gt;Classloader loads the static class in JVM only at the time of the first usage, not when its enclosing class gets loaded.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Visit the original article: &lt;a href="https://www.java8net.com/2020/11/static-class-in-java.html"&gt;https://www.java8net.com/2020/11/static-class-in-java.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this article, we have studied what is a static class in java, how to create it and it's benefits. We have created a User with UserValidator class to provide the facility to check whether it is a valid user or not. It is good to have helper classes as a static class inside the enclosing class.&lt;/p&gt;

</description>
      <category>java</category>
    </item>
    <item>
      <title>Immutable class in Java</title>
      <dc:creator>aryan</dc:creator>
      <pubDate>Wed, 04 Nov 2020 11:05:06 +0000</pubDate>
      <link>https://dev.to/aryan19694946/immutable-class-in-java-4m51</link>
      <guid>https://dev.to/aryan19694946/immutable-class-in-java-4m51</guid>
      <description>&lt;p&gt;In this article, we'll define the typical steps for creating an immutable class in Java and also shed light on the common mistakes which are made by developers while creating immutable classes.&lt;/p&gt;

&lt;p&gt;visit the original article: &lt;a href="https://www.java8net.com/2020/11/immutable-class-in-java.html"&gt;https://www.java8net.com/2020/11/immutable-class-in-java.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Steps to create Immutable class in java&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Make your class final, so that no other classes can extend it.&lt;/li&gt;
&lt;li&gt;Make all fields private so that direct access is not allowed.&lt;/li&gt;
&lt;li&gt;Don’t provide “setter” methods — methods that modify fields or objects referred to by fields.&lt;/li&gt;
&lt;li&gt;Special attention when having mutable instance variables
4.1) Inside the constructor, make sure to use a clone copy of the passed argument and never set your mutable field to the real instance passed through the constructor.
4.2) Make sure to always return a clone copy of the field and never return the real object instance.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Implementation of Immutable class in java&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Address{
    private String houseNumber;
    private String street;
    private String city;
}

final class User {
    private final int id;
    private final String name;
    private final String email;
    private final Address address;

    /*
     * To clone the address.
     */
    private static Address cloneAddress(Address address) {
        return new ObjectMapper().convertValue(address, Address.class);
    }

    User(int id, String name, String email, Address address) {
        this.id = id;
        this.name = name;
        this.email = email;
        /*
         * Any field contains reference of any mutable 
                 * object must be
         * initialized with the cloned object
         */
        this.address = cloneAddress(address);
    }
    public int getId() {
        return id;
    }
    public String getName() {
        return name;
    }
    public String getEmail() {
        return email;
    }
    public Address getAddress() {
        /*
         * Getter method must return the reference of cloned object.
         */
        return cloneAddress(address);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Pre-defined Immutable class in java&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The string class represents the immutable object for the sequence of characters.&lt;/li&gt;
&lt;li&gt;Wrapper classes like Integer, Float, Long, etc. all represent the immutable object for the corresponding primitive type.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;java.util.UUID&lt;/code&gt; represents a unique 128-bit hexadecimal string.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Benefits of making an Immutable class in java&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Immutable objects are by default thread-safe. &lt;/li&gt;
&lt;li&gt;Immutable objects can easily be cached.&lt;/li&gt;
&lt;li&gt;Immutable objects are the best options to use as a key object in any map like HashMap.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;visit the original article: &lt;a href="https://www.java8net.com/2020/11/immutable-class-in-java.html"&gt;https://www.java8net.com/2020/11/immutable-class-in-java.html&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>immutable</category>
    </item>
    <item>
      <title>Synchronized ArrayList in Java</title>
      <dc:creator>aryan</dc:creator>
      <pubDate>Mon, 06 Apr 2020 16:36:12 +0000</pubDate>
      <link>https://dev.to/aryan19694946/synchronized-arraylist-in-java-443k</link>
      <guid>https://dev.to/aryan19694946/synchronized-arraylist-in-java-443k</guid>
      <description>&lt;p&gt;Implementation of arrayList is not synchronized is by default. It means if a thread modifies it structurally and multiple threads access it concurrently, it must be synchronized externally. Structural modification means the addition or deletion of the element from the list or explicitly resizes the backing array. Changing the value of existing element is not structural modification.&lt;/p&gt;

&lt;p&gt;See Reference: &lt;a href="https://www.java8net.com/2020/03/synchronized-arraylist-in-java.html"&gt;https://www.java8net.com/2020/03/synchronized-arraylist-in-java.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;import java.util.*; &lt;/p&gt;

&lt;p&gt;class ArrayListExample &lt;br&gt;
{ &lt;br&gt;
    public static void main (String[] args) &lt;br&gt;
    { &lt;br&gt;
        List list = &lt;br&gt;
           Collections.synchronizedList(new ArrayList()); &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    list.add("A"); 
    list.add("B"); 
    list.add("C"); 

    synchronized(list) 
    { 
        // must be in synchronized block 
        Iterator it = list.iterator(); 

        while (it.hasNext()) 
            System.out.println(it.next()); 
    } 
} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;} &lt;/p&gt;

&lt;p&gt;See Reference: &lt;a href="https://www.java8net.com/2020/03/synchronized-arraylist-in-java.html"&gt;https://www.java8net.com/2020/03/synchronized-arraylist-in-java.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Further Readings&lt;br&gt;
&lt;a href="https://www.java8net.com/2020/03/java-arraylist-common-program-examples.html"&gt;https://www.java8net.com/2020/03/java-arraylist-common-program-examples.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.java8net.com/2020/02/how-to-sort-arraylist-in-java.html"&gt;https://www.java8net.com/2020/02/how-to-sort-arraylist-in-java.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.java8net.com/2020/03/initialize-arraylist-in-java.html"&gt;https://www.java8net.com/2020/03/initialize-arraylist-in-java.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.java8net.com/2020/03/how-to-remove-duplicates-from-arraylist-in-java.html"&gt;https://www.java8net.com/2020/03/how-to-remove-duplicates-from-arraylist-in-java.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.java8net.com/2020/03/arraylist-to-hashmap-in-java.html"&gt;https://www.java8net.com/2020/03/arraylist-to-hashmap-in-java.html&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
    </item>
    <item>
      <title>How to serialize and deserialize ArrayList in Java</title>
      <dc:creator>aryan</dc:creator>
      <pubDate>Mon, 23 Mar 2020 07:18:23 +0000</pubDate>
      <link>https://dev.to/aryan19694946/how-to-serialize-and-deserialize-arraylist-in-java-5e81</link>
      <guid>https://dev.to/aryan19694946/how-to-serialize-and-deserialize-arraylist-in-java-5e81</guid>
      <description>&lt;p&gt;ArrayList is serializable by default. This means you need not to implement Serializable interface explicitly in order to serialize an ArrayList. In this tutorial, we will learn how to serialize and de-serialize an ArrayList.&lt;/p&gt;

&lt;p&gt;See original article: &lt;a href="https://www.java8net.com/2020/03/serialize-arraylist-in-java.html"&gt;https://www.java8net.com/2020/03/serialize-arraylist-in-java.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Serialization of ArrayList: &lt;/p&gt;

&lt;p&gt;ArrayList arrayList = new ArrayList&amp;lt;&amp;gt;(&lt;br&gt;
       Arrays.asList("A", "B", "C", "D", "E"));&lt;br&gt;
try {&lt;br&gt;
      FileOutputStream fileOutputStream = new FileOutputStream("data");&lt;br&gt;
      ObjectOutputStream objectOutputStream &lt;br&gt;
                    = new ObjectOutputStream(fileOutputStream);&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  objectOutputStream.writeObject(arrayList);

  objectOutputStream.close();
  fileOutputStream.close();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;} catch (IOException e) {&lt;br&gt;
       e.printStackTrace();&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;Deserialization of ArrayList:&lt;/p&gt;

&lt;p&gt;try {&lt;br&gt;
       FileInputStream fileInputStream = new FileInputStream("data");&lt;br&gt;
       ObjectInputStream objectInputStream &lt;br&gt;
                = new ObjectInputStream(fileInputStream);&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   ArrayList&amp;lt;String&amp;gt; arrayList 
            = (ArrayList&amp;lt;String&amp;gt;) objectInputStream.readObject();
   System.out.println(arrayList);

   fileInputStream.close();
   objectInputStream.close();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;} catch (IOException | ClassNotFoundException e) {&lt;br&gt;
       e.printStackTrace();&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;To learn in details, visit: &lt;a href="https://www.java8net.com/2020/03/serialize-arraylist-in-java.html"&gt;https://www.java8net.com/2020/03/serialize-arraylist-in-java.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Further Readings: &lt;br&gt;
&lt;a href="https://www.java8net.com/2020/03/synchronized-arraylist-in-java.html"&gt;https://www.java8net.com/2020/03/synchronized-arraylist-in-java.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.java8net.com/2020/02/how-to-sort-arraylist-in-java.html"&gt;https://www.java8net.com/2020/02/how-to-sort-arraylist-in-java.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.java8net.com/2020/03/initialize-arraylist-in-java.html"&gt;https://www.java8net.com/2020/03/initialize-arraylist-in-java.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.java8net.com/2020/03/arraylist-to-hashmap-in-java.html"&gt;https://www.java8net.com/2020/03/arraylist-to-hashmap-in-java.html&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
    </item>
    <item>
      <title>How to initialize ArrayList in Java?</title>
      <dc:creator>aryan</dc:creator>
      <pubDate>Wed, 18 Mar 2020 05:56:36 +0000</pubDate>
      <link>https://dev.to/aryan19694946/how-to-initialize-arraylist-in-java-1jp6</link>
      <guid>https://dev.to/aryan19694946/how-to-initialize-arraylist-in-java-1jp6</guid>
      <description>&lt;p&gt;The Java ArrayList can be initialized in number of ways depending on the requirement. In this tutorial, we will learn to initialize ArrayList based on some frequently seen usecases.&lt;/p&gt;

&lt;p&gt;See original article: &lt;a href="https://www.java8net.com/2020/03/initialize-arraylist-in-java.html"&gt;https://www.java8net.com/2020/03/initialize-arraylist-in-java.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How to initialize ArrayList in Java - using Arrays.asList() method?&lt;/p&gt;

&lt;p&gt;import java.util.ArrayList;&lt;br&gt;
import java.util.Arrays;&lt;/p&gt;

&lt;p&gt;public class ArrayListExample {&lt;br&gt;
        public static void main(String[] args) {&lt;br&gt;
                ArrayList list = new ArrayList&amp;lt;&amp;gt;(&lt;br&gt;
                        Arrays.asList("A", "B", "C", "D", "E"));&lt;br&gt;
                System.out.println("ArrayList : " + list);&lt;br&gt;
        }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;Related Posts:&lt;br&gt;
&lt;a href="https://www.java8net.com/2020/03/java-arraylist-common-program-examples.html"&gt;https://www.java8net.com/2020/03/java-arraylist-common-program-examples.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.java8net.com/2020/03/arraylist-to-hashmap-in-java.html"&gt;https://www.java8net.com/2020/03/arraylist-to-hashmap-in-java.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.java8net.com/2020/03/how-to-remove-duplicates-from-arraylist-in-java.html"&gt;https://www.java8net.com/2020/03/how-to-remove-duplicates-from-arraylist-in-java.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.java8net.com/2020/03/iterate-arraylist-in-java.html"&gt;https://www.java8net.com/2020/03/iterate-arraylist-in-java.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.java8net.com/2020/03/java-sublist-from-arraylist.html"&gt;https://www.java8net.com/2020/03/java-sublist-from-arraylist.html&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
    </item>
    <item>
      <title>How to compare two ArrayList in Java?</title>
      <dc:creator>aryan</dc:creator>
      <pubDate>Sat, 14 Mar 2020 14:24:53 +0000</pubDate>
      <link>https://dev.to/aryan19694946/how-to-compare-two-arraylist-in-java-499b</link>
      <guid>https://dev.to/aryan19694946/how-to-compare-two-arraylist-in-java-499b</guid>
      <description>&lt;p&gt;Learn to compare two ArrayList in Java with simple examples. We will first test if two ArrayList are equal or not. If both lists are not equal, we will find the difference between lists.&lt;/p&gt;

&lt;p&gt;The difference in the list equals another third list which contains either additional elements or missing elements.&lt;/p&gt;

&lt;p&gt;Also, learn to find common elements between two ArrayList.&lt;/p&gt;

&lt;p&gt;See the original post: &lt;a href="https://www.java8net.com/2020/03/compare-two-arraylist-in-java.html"&gt;https://www.java8net.com/2020/03/compare-two-arraylist-in-java.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Compare two ArrayList for equality&lt;/p&gt;

&lt;p&gt;With the help of the equals() method, we can compare two ArrayList in java and check whether they are the same or not. But, here is a condition, the order of the elements of both the ArrayList must be the same. Otherwise, it won't work. To be sure, we can sort the ArrayList (sorting can be ascending or descending) and then apply the equals() method for comparison.&lt;/p&gt;

&lt;p&gt;Read Article to learn how to sort ArrayList in Java: &lt;a href="https://www.java8net.com/2020/02/how-to-sort-arraylist-in-java.html"&gt;https://www.java8net.com/2020/02/how-to-sort-arraylist-in-java.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;import java.util.ArrayList;&lt;/p&gt;

&lt;p&gt;public class ArrayListExample {&lt;br&gt;
   public static void main(String[] args) {&lt;br&gt;
      ArrayList list1 = new ArrayList();&lt;br&gt;
      list1.add("A");&lt;br&gt;
      list1.add("B");&lt;br&gt;
      list1.add("C");&lt;br&gt;
      list1.add("D");&lt;br&gt;
      ArrayList list2 = new ArrayList();&lt;br&gt;
      list2.add("A");&lt;br&gt;
      list2.add("B");&lt;br&gt;
      list2.add("C");&lt;br&gt;
      list2.add("D");&lt;br&gt;
      System.out.println(list2);&lt;br&gt;
      System.out.println(list1.equals(list2));&lt;br&gt;
   }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;If you want to learn in detail: &lt;a href="https://www.java8net.com/2020/03/compare-two-arraylist-in-java.html"&gt;https://www.java8net.com/2020/03/compare-two-arraylist-in-java.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Further Reading:&lt;br&gt;
&lt;a href="https://www.java8net.com/2020/03/java-arraylist-common-program-examples.html"&gt;https://www.java8net.com/2020/03/java-arraylist-common-program-examples.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.java8net.com/2020/03/java-sublist-from-arraylist.html"&gt;https://www.java8net.com/2020/03/java-sublist-from-arraylist.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.java8net.com/2020/03/how-to-remove-duplicates-from-arraylist-in-java.html"&gt;https://www.java8net.com/2020/03/how-to-remove-duplicates-from-arraylist-in-java.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.java8net.com/2020/03/iterate-arraylist-in-java.html"&gt;https://www.java8net.com/2020/03/iterate-arraylist-in-java.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.java8net.com/2020/03/arraylist-to-array-in-java.html"&gt;https://www.java8net.com/2020/03/arraylist-to-array-in-java.html&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
    </item>
    <item>
      <title>How HashMap works internally in java</title>
      <dc:creator>aryan</dc:creator>
      <pubDate>Tue, 03 Mar 2020 18:14:22 +0000</pubDate>
      <link>https://dev.to/aryan19694946/how-hashmap-works-internally-in-java-3550</link>
      <guid>https://dev.to/aryan19694946/how-hashmap-works-internally-in-java-3550</guid>
      <description>&lt;p&gt;This is article is about how hashmap works internally in java. It is very important to understand the concept of hashmap as hashmap comes into our day-to-day programming practice and it is the most favorite question on the interviewer during the interviews.&lt;/p&gt;

&lt;p&gt;Original Article: &lt;a href="https://www.java8net.com/2020/01/how-hashmap-works-internally-in-java.html"&gt;https://www.java8net.com/2020/01/how-hashmap-works-internally-in-java.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What is Hashmap and Hashing in java?&lt;/p&gt;

&lt;p&gt;How Hashmap works Internally in Java is majorly dependent upon the Hashing Principle. So, Before going to learn how HashMap works internally in java, lets first understand what is HashMap and hashing.&lt;/p&gt;

&lt;p&gt;HashMap :&lt;br&gt;
A HashMap is a map used to store mappings of key-value pairs. Also, it works on the Principle of Hashing. To know more about the HashMap, visit this article: &lt;a href="https://www.java8net.com/2020/01/hashmap-in-java.html"&gt;https://www.java8net.com/2020/01/hashmap-in-java.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hashing Principle :&lt;br&gt;
Simply, Hashing is a method used to produce an integer value from an object and this integer value known as the hash value. In HashMap, the key object is used for Hashing.&lt;/p&gt;

&lt;p&gt;Before understanding the internal working of HashMap, you must be aware of the hashCode() and equals() method.&lt;/p&gt;

&lt;p&gt;hashCode() : hashCode() method is used to get the hash code of an object. This method is provided by Object class. You can override this in your class to provide your own implementation.&lt;/p&gt;

&lt;p&gt;equals(): equals method is used to check that 2 objects are equal or not. This method is provided by Object class. You can override this in your class to provide your own implementation.&lt;/p&gt;

&lt;p&gt;What is Buckets?:&lt;br&gt;
The array of the node is called buckets. Each node has a data structure like a LinkedList. More than one node can share the same bucket. It may be different incapacity.&lt;br&gt;
A single bucket can have more than one nodes, it depends on the hashCode() method. The better your hashCode() method is, the better your buckets will be utilized.&lt;/p&gt;

&lt;p&gt;Hash Collision :&lt;br&gt;
Here comes the main part. Now, as we know that two unequal objects can have the same hash code value, how two different objects will be stored in the same array location called a bucket.&lt;/p&gt;

&lt;p&gt;The answer is LinkedList. If you remember, the Entry class had an attribute “next”. This attribute always points to the next object in the chain. This is exactly the behavior of the LinkedList.&lt;/p&gt;

&lt;p&gt;How Hashmap Calculate the Index of Bucket in java?&lt;/p&gt;

&lt;p&gt;To understand how HashMap works internally in Java, we must know about how the HashMap calculates the index of the bucket. Till now, we know the internal structure of HashMap, that HashMap maintains an array of the bucket. But when we store or retrieve any key-value pair, HashMap calculates the index of the bucket for each and every operation.&lt;/p&gt;

&lt;p&gt;The Key object is used to calculate the index of the bucket. By using this key, the hash value is calculated using the hash(key) private method of HashMap.&lt;/p&gt;

&lt;p&gt;Note: hash(key) method is a private method of HashMap that returns the hash value of the key, also if the hash value is too large then converts it into a smaller hash value.&lt;/p&gt;

&lt;p&gt;But what will happen, if the hash value of Key Object returns the integer that is greater than the size of the array i.e., hash(key) &amp;gt; n, then ArrayOutOfBoundsException could be raised. To handle this situation, HashMap reduces the hash value between 0 and n-1 using an expression:&lt;/p&gt;

&lt;p&gt;Index Calculating Expression&lt;br&gt;
index = hash(key) &amp;amp; (n-1)&lt;br&gt;
Now, this index value is generated is used by HashMap to find bucket location and can never generate any Exception as the index value always from 0 to n-1.&lt;/p&gt;

&lt;p&gt;To know more about the internal working of the hashmap, read this blog: &lt;a href="https://www.java8net.com/2020/01/how-hashmap-works-internally-in-java.html"&gt;https://www.java8net.com/2020/01/how-hashmap-works-internally-in-java.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Further Learning:&lt;br&gt;
&lt;a href="https://www.java8net.com/2020/02/sort-hashmap-by-key-and-by-value.html"&gt;https://www.java8net.com/2020/02/sort-hashmap-by-key-and-by-value.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.java8net.com/2020/11/static-class-in-java.html"&gt;https://www.java8net.com/2020/11/static-class-in-java.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.java8net.com/2020/02/treemap-in-java.html"&gt;https://www.java8net.com/2020/02/treemap-in-java.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.java8net.com/2020/03/java-optional.html"&gt;https://www.java8net.com/2020/03/java-optional.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.java8net.com/2020/01/lambda-expression-java.html"&gt;https://www.java8net.com/2020/01/lambda-expression-java.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.java8net.com/2020/01/functional-interface-in-java.html"&gt;https://www.java8net.com/2020/01/functional-interface-in-java.html&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
    </item>
    <item>
      <title>Sort HashMap By key and By value</title>
      <dc:creator>aryan</dc:creator>
      <pubDate>Tue, 03 Mar 2020 18:07:32 +0000</pubDate>
      <link>https://dev.to/aryan19694946/sort-hashmap-by-key-and-by-value-53l3</link>
      <guid>https://dev.to/aryan19694946/sort-hashmap-by-key-and-by-value-53l3</guid>
      <description>&lt;p&gt;In this article, we are going to have look at how to sort HashMap by key and to sort HashMap by value in Java. We are going to learn different approaches to sort HashMap.&lt;/p&gt;

&lt;p&gt;Visit this link to read the full article: &lt;a href="https://www.java8net.com/2020/02/sort-hashmap-by-key-and-by-value.html"&gt;https://www.java8net.com/2020/02/sort-hashmap-by-key-and-by-value.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;HashMap does not sort its key-value pair and also it does not maintain any order (neither insertion nor sorted). If we want to sort HashMap then we need to provide custom logic that can sort HashMap and sort HashMap by key we can use either TreeMap or LinkedHashMap but in case of sort HashMap by value, we have the only option of LinkedHashMap as TreeMap cannot be used to sort HashMap by value.&lt;br&gt;
To sort HashMap by key, we can use TreeMap class. TreeMap in java is used to store key-value pairs and sort all the pairs w.r.t. keys. We can create a TreeMap from the HashMap and TreeMap sort HashMap by key.&lt;/p&gt;

&lt;p&gt;Steps to sort HashMap by key using TreeMap&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a Comparator, which provides logic to sort HashMap by key.&lt;/li&gt;
&lt;li&gt;Create a TreeMap using Comparator.&lt;/li&gt;
&lt;li&gt;Add all the entries of HashMap into the TreeMap&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Java Sort HashMap Example&lt;br&gt;
HashMap hashMap = new HashMap&amp;lt;&amp;gt;(); &lt;br&gt;
hashMap.put("Programming In Java", 450); &lt;br&gt;
hashMap.put("Introduction to Algorithms", 800); &lt;br&gt;
hashMap.put("Data Structures", 600); &lt;br&gt;
hashMap.put("Learn C++", 350); &lt;br&gt;
System.out.println("-----------Before Sorting--------------");&lt;br&gt;
Set&amp;gt; unsortedEntrySet = hashMap.entrySet();&lt;br&gt;
for (Entry entry : unsortedEntrySet) {&lt;br&gt;&lt;br&gt;
        System.out.println(entry.getKey() + " = " + entry.getValue()); &lt;br&gt;
} &lt;/p&gt;

&lt;p&gt;Comparator keyComparator = (s1, s2) -&amp;gt; s1.compareTo(s2);&lt;br&gt;
TreeMap treeMap = new TreeMap&amp;lt;&amp;gt;(keyComparator); treeMap.putAll(hashMap); &lt;br&gt;
System.out.println("-----------After Sorting---------------"); &lt;br&gt;
Set&amp;gt; sortedEntrySet = treeMap.entrySet(); &lt;br&gt;
for (Entry entry : sortedEntrySet) { &lt;br&gt;
        System.out.println(entry.getKey() + " = " + entry.getValue()); &lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;Visit this link to read the full article: &lt;a href="https://www.java8net.com/2020/02/sort-hashmap-by-key-and-by-value.html"&gt;https://www.java8net.com/2020/02/sort-hashmap-by-key-and-by-value.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Further Learning:&lt;br&gt;
&lt;a href="https://www.java8net.com/2020/02/how-to-sort-arraylist-in-java.html"&gt;https://www.java8net.com/2020/02/how-to-sort-arraylist-in-java.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.java8net.com/2020/02/treemap-in-java.html"&gt;https://www.java8net.com/2020/02/treemap-in-java.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.java8net.com/2020/03/java-optional.html"&gt;https://www.java8net.com/2020/03/java-optional.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.java8net.com/2020/01/lambda-expression-java.html"&gt;https://www.java8net.com/2020/01/lambda-expression-java.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.java8net.com/2020/01/functional-interface-in-java.html"&gt;https://www.java8net.com/2020/01/functional-interface-in-java.html&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
    </item>
  </channel>
</rss>
