View RSS Feed

Algorithm and Data Structure

Algorithm and data structure examples in Java.

  1. Java's SortedMap Interface

    by , 02-19-2012 at 08:38 AM
    The java.util.Map interface’s subtype is the java.util.SortedMap. Functionality of java.util.Map is extended by this interface and elements are sorted which have been stored in the Map, internally.

    Element’s sorting orders are as following:

    • If java.lang.Comparable is implemented then it means that there exist element’s natural sorting order.
    • Comparator determines the order which you will be providing to the SortedSet.

    Default sorting orders are ascending ...
  2. Removing Elements from Map

    by , 02-19-2012 at 08:36 AM
    From Map, the removal of elements is done by using remove (Object key) method. This would be removing pair (Key, Value) from Map.

    Any type of elements could be put into Map. Type of objects can be limited to be used for values or keys in a Map. How to limit values or keys is well explained in the given code.
    Java Code: This is the code to limit values or keys
    Map<String, MyObject> map = new HashSet<String, MyObject>();
    Map just accepts the value son MyObject type or keys of string type, ...
  3. Adding and Accessing Map Elements

    by , 02-19-2012 at 08:24 AM
    For addition of elements to Map, use put() method. This process is explained by the given code.
    Java Code: This is the code to explain map add operation
    Map mapA = new HashMap();
    mapA.put("key1", "element 1");
    mapA.put("key2", "element 2");
    mapA.put("key3", "element 3");
    Key values are mapped by calling 3 put() methods, in the code given above. Both values & keys are String, in above code. To get value, use this code.
    Java Code: This is the code to explain map access operation
  4. Java Map Implementations

    by , 02-19-2012 at 08:21 AM
    Map can’t be instantiated as it is an interface. For creation of the Map interface instance, concrete implementation is required. Map interface has these concrete implementations which are present in Collection API.

    • java.util.HashMap
    • java.util.Hashtable
    • java.util.EnumMap
    • java.util.IdentityHashMap
    • java.util.LinkedHashMap
    • java.util.Properties
    • java.util.TreeMap
    • java.util.WeakHashMap

    Map implementations which ...
  5. Java Hash Map

    by , 02-19-2012 at 08:19 AM
    Key value paired data structure is provided by the Map interface so that for mapping the keys to values. Keys are unique which are used for retrieval of the values, from Map.

    Key features are as following:

    • Key value repair is used for storage of all values. Key is used for the retrieval.
    • When Map doesn’t contain any element, various methods throw the “NoSuchElementException”.
    • When compatibility of element is not present with map elements, ClassCastException gets
    Tags: hash map Add / Edit Tags
  6. Java's Stack Class

    by , 02-19-2012 at 08:16 AM
    Stack is considered to be a data structure which used last in first out policy. New elements could be added or removed to/from the top of Stack. Queue uses First in First out mechanism as compared to Stack.

    Java Code: Stack usage is explained by this code
    Stack myStack = new Stack();
    //look at top object ("3"), without taking it off the stack.    
    Object topObj = stack.peek();
    Tags: stack class Add / Edit Tags
  7. Implementing a Stack in Java

    by , 02-19-2012 at 08:13 AM
    This post will be explaining the Java’s Stack implementation. Last in first order policy is used in Stack in which one by one elements get inserted in a sequence which could be retrieved later on in reverse order. Use these 2 methods to insert or read the elements to/from the Stack.

    • Pop()
    • Push()

    Code is given below to show the implementation of LIFP stack. Total elements that are needed to be inserted to Stack will be taken and after which it will be asked ...
    Tags: java stack Add / Edit Tags
  8. Java Stack Introduction

    by , 02-19-2012 at 08:11 AM
    A stack is basically a data structure which uses the following:

    • Methods for inserting the data by the help of push operation
    • Methods for removal of data by the help of pop operation.

    Peak method is supported by Stack implementations. It will make the head to be read without removal from Stack. Last in First out policy is used in Stack. This means that data which has been inserted in data structure at last will be made to get removed first.

    Data ...
    Tags: stack Add / Edit Tags
    Queue , Stack
  9. Java Deque Implementations

    by , 02-19-2012 at 08:07 AM
    Collection interface subtype is DeQue interface. In DeQue interface, all of the Collection interface methods are present.

    DeQue interface instance can be created. This interface’s concrete implementation is required to do so. In Collections API, these DeQue implementations are present. Among them, you may select to use.

    • java.util.LinkedList
    • java.util.ArrayDeque

    LinkedList is considered to be a pretty queue implementation.

    In an array, ...
    Tags: java deque Add / Edit Tags
  10. Adding and Accessing Queue Elements

    by , 02-19-2012 at 08:05 AM
    Call add() method for addition of the elements to Queue. This is an inherited type of method of Collection interface.

    Different elements are added to a Queue which is explained by this given code.

    Java Code: This is the code to explain add operation in Java Queue
    Queue myQueue = new LinkedList();
    myQueue.add("element 1");
    myQueue.add("element 2");
    myQueue.add("element 3");
    In a Queue, added elements get stored in internal order and they are dependent upon implementation. ...
  11. Java Queue Implementations

    by , 02-19-2012 at 08:02 AM
    Collection Interface’s subtype is Queue. In the Queue interface, collection interface methods are present automatically.

    Queue interface instance can’t be created. Queue interface concrete implementations are required. In Collections API, the available Queue implementations are as following. Among them, one can make a choice:
    • java.util.PriorityQueue
    • java.util.LinkedList

    LinkedList is a considered to be a standard queue implementation.

  12. Java – Queue Example

    by , 02-19-2012 at 08:00 AM
    Queue base is at first in first out policy. The Queue interface is present in java.util package. Queue size is not required to be specified just like it is done in arrays.

    Functionality of Queue is explained in the given code.

    Example of Queue
    Java Code: This is the code to explain Java Queue
    import java.util.Iterator;
    import java.util.LinkedList;
    import java.util.Queue;
    public class QueueTesting {
        public static void main(String[] args) {
    Tags: java queue Add / Edit Tags
  13. Java Queue

    by , 02-19-2012 at 07:58 AM
    Java Queue is also collection interface for holding the objects. Collections store or hold elements, before processing. Following operations are provided by Queues:

    1. Insertion
    2. Inspections
    3. Extraction
    4. (All) Other Collection operations

    Queues are basically first in first order implements. They don’t depend on use order. Poll() and remove() methods are used to remove the elements present at head.

    From queue’s tail, all elements ...
    Tags: java queue Add / Edit Tags
  14. Java Linked List Implementations

    by , 02-19-2012 at 07:57 AM
    Collection interface is subtype of a List. Collection interface methods are also present in list interface.
    List can’t be instantiated as it is an interface. For creation of instance of the List, create concrete implementation instance of the interface. In Java Collection APIs, these options are present:

    • java.util.ArrayList
    • java.util.LinkedList
    • java.util.Vector
    • java.util.Stack

    In java.util.concurrent package, List implementations ...
  15. What is the advantage of using an Iterator compared to the get(index) method?

    by , 02-19-2012 at 07:55 AM
    One of the following methods should be used to iterate the List.

    1. Iterator
    2. get(index)

    get(index) method is quite slower in comparison with Iterator.

    Consider this example:

    Java Code: This is the code to explain Iterator
    public class TestClass {
      public static void main(String[] args) {
        int  len = 100000;
        LinkedList linkedLst = new LinkedList(); 
        ArrayList  arrayLst = new ArrayList();
        for (int m =0; m!= len; m++)
    Linked List
  16. Removing an Item from the List

    by , 02-19-2012 at 07:52 AM
    For removal of an item out of List, different methods would be discussed here. Use these methods.

    • remove()
    • removeFirst()
    • removeLast()

    Java Code: This is the code to explain removal of an element from the List
    import java.util.*;
    public class TestLinkedList
    public static void main(String[] args)
    int pause;
    String temp;
    LinkedList theList = new LinkedList();
    Tags: remove item Add / Edit Tags
    Linked List
  17. Accessing Items from a List

    by , 02-19-2012 at 07:50 AM
    Get List Size:

    To get the List size, method “size()”is used. Size of MyList will be printed by this code.
    Java Code:
    Get Item:

    For getting a specific spot value, use get() method. In this method index of the List is provided. In case when index is not present, IndexOutOfBoundsException gets thrown.

    Element is not removed from the list by this method. Just this object is returned. This given code will ...
  18. Adding Elements to a List

    by , 02-19-2012 at 07:47 AM
    Elements are added to list by using these 4 different methods given below:
    Java Code:
    An item is added by it to end of list.
    At a particular location, elements could be added as given below.
    Java Code:
    MyList.add(0, "2");
    MyList.add(0, varName);
    Zero tells the element location, as given in above code. This is basically added as 1st element.
    Other two ...
    Linked List
  19. Java Linked Lists

    by , 02-19-2012 at 07:45 AM
    Arrays of data are stored by Linked Lists, which is considered to be a well known way of doing so. Linked List has many benefits among which a major one is that fixed size specification is not needed. To the list, you can add desired number of elements.

    Linked lists types would be discussed in this post.

    Name:  1.JPG
Views: 829
Size:  48.1 KB

    Basic Types of Linked Lists
    Tags: linked lists Add / Edit Tags
    Linked List