Wednesday, January 27, 2016

HashSet
HashMap
HashSet implements Set interface.
HashMap implements Map interface.
HashSet stores the data as objects.
HashMap stores the data as key-value pairs.
HashSet internally uses HashMap.
HashMap internally uses an array of Entry<K, V>objects.
HashSet doesn’t allow duplicate elements.
HashMap doesn’t allow duplicate keys, but allows duplicate values.
HashSet allows only one null element.
HashMap allows one null key and multiple null values.
Insertion operation requires only one object.
Insertion operation requires two objects, key and value.
HashSet is slightly slower than HashMap.
HashMap is slightly faster than HashSet.


Similarities Between HashMap And HashSet In Java :

1) Both data structures don’t maintain any order for the elements.
2) Both use hashCode() and equals() method to maintain the uniqueness of the data.
3) The iterators returned by both are fail-fast in nature.
4) Both give constant time performance for insertion and removal operations.
5) Both are not synchronized.


Tuesday, January 26, 2016

Linked List Implementation

Linked List Impl in Java

public class Node {
            private int data;
            private Node nextNode;

            public Node(int data){
                        this.data=data;
            }
           
            public int getData() {
                        return data;
            }
           
            public void setData(int data) {
                        this.data = data;
            }
           
            public Node getNextNode() {
                        return nextNode;
            }
           
            public void setNextNode(Node nextNode) {
                        this.nextNode = nextNode;
            }
           
            @Override
            public String toString(){
                        return ""+this.data;
            }
}
           

public class LinkedList {

            public Node head;

            public Node reverseLinkedList(Node currentNode) {
                        // For first node, previousNode will be null
                        Node previousNode = null;
                        Node nextNode;
                        while (currentNode != null) {
                                    nextNode = currentNode.getNextNode();
                                    // reversing the link
                                    currentNode.setNextNode(previousNode);
                                    // moving currentNode and previousNode by 1 node
                                    previousNode = currentNode;
                                    currentNode = nextNode;
                        }
                        return previousNode;
            }

            public void add(int data) {
                        if (head == null)
                                    insertAtHead(data);
                        else
                                    insertAtEnd(data);
            }

            public void insertAtHead(int data) {
                        Node newNode = new Node(data);
                        newNode.setNextNode(this.head);
                        this.head = newNode;
            }

            public int length() {
                        Node current = this.head;
                        int count = 0;
                        while (current != null) {
                                    current = current.getNextNode();
                                    count++;
                        }
                        return count;
            }

            public void deleteAtHead() {
                        this.head = this.head.getNextNode();
            }

            public String find(int data) {
                        Node current = this.head;
                        while (current != null) {
                                    if (current.getData() == data)
                                                return data + " Found";
                                    current = current.getNextNode();
                        }
                        return data + " Not Found ";
            }

            public void insertAfterData(int dataPresent, int dataAdd) {
                        Node current = this.head;
                        while (current != null) {
                                    if (current.getData() == dataPresent) {
                                                Node newNode = new Node(dataAdd);
                                                newNode.setNextNode(current.getNextNode());
                                                current.setNextNode(newNode);
                                                break;
                                    }
                                    current = current.getNextNode();
                        }
            }

            public void insertBeforeData(int dataPresent, int dataAdd) {
                        Node current = this.head;
                        while (current != null) {
                                    if (current.getNextNode().getData() == dataPresent) {
                                                Node newNode = new Node(dataAdd);
                                                newNode.setNextNode(current.getNextNode());
                                                current.setNextNode(newNode);
                                                break;
                                    }
                                    current = current.getNextNode();
                        }
            }

            public void insertAtEnd(int dataAdd) {
                        Node current = this.head;
                        while (current.getNextNode() != null) {
                                    current = current.getNextNode();
                        }
                        Node newNode = new Node(dataAdd);
                        newNode.setNextNode(current.getNextNode());
                        current.setNextNode(newNode);
            }

            public void deleteNode(int dataPresent) {
                        Node current = this.head;
                        while (current != null) {
                                    if (current.getNextNode().getData() == dataPresent) {
                                                current.setNextNode(current.getNextNode().getNextNode());
                                                break;
                                    }
                                    current = current.getNextNode();
                        }
            }

            @Override
            public String toString() {
                        String result = "{";
                        Node current = this.head;
                        while (current != null) {
                                    result += current.toString() + "-->";
                                    current = current.getNextNode();
                        }
                        result += "null }";
                        return result;
            }
}

public class LinkedNodeDemo {

            public static void main(String[] args) {
                        LinkedList list = new LinkedList();
                        list.insertAtHead(10);
                        list.insertAtHead(20);
                        list.insertAtHead(15);
                        list.insertAtHead(25);
                        list.insertAtHead(40);
                        System.out.println("List after insertAtHead Operation" + list);
                        System.out.println("Length is " + list.length());
                        list.deleteAtHead();
                        System.out.println("List after deleteAtHead Operation" + list);
                        System.out.println(list.find(100));
                        list.insertAfterData(20, 12);
                        System.out.println("List after insertAfterData Operation " + list);
                        list.insertBeforeData(20, 6);
                        System.out.println("List after insertBeforeData Operation" + list);
                        list.insertAtEnd(19);
                        System.out.println("List after insertAtEnd Operation" + list);
                        list.deleteNode(20);
                        System.out.println("List after deleteNode Operation" + list);
                       
                        LinkedList list1 = new LinkedList();
                        list1.add(10);
                        list1.add(20);
                        list1.add(20);
                        System.out.println(list1);
            }
}

Output :
List after insertAtHead Operation{40-->25-->15-->20-->10-->null }
Length is 5
List after deleteAtHead Operation{25-->15-->20-->10-->null }
100 Not Found
List after insertAfterData Operation {25-->15-->20-->12-->10-->null }
List after insertBeforeData Operation{25-->15-->6-->20-->12-->10-->null }
List after insertAtEnd Operation{25-->15-->6-->20-->12-->10-->19-->null }
List after deleteNode Operation{25-->15-->6-->12-->10-->19-->null }
New List is(Like Build-In) {10-->20-->20-->null }

Pictorial representation of LinkedList