Definition Of Comparator Interface

Definition Of Comparator Interface

Definition Of Comparator Interface ~ First of all, before reading this post you should understand what is an interface in craftingez6-coding.

You can found comparator interface in java.util.package and it contains 2 methods. Thus 2 of those methods are : .compare(obj1, obj2) and equals(Object element)

compare() method

public int compare(object1, object2)

Java Comparator Example (non-generic style)

Thus for this example I’ve created 2 class of sorting elements in the base of age and name. Due to the sake of simplicity of this article i’ve only created 2 :

  1. Age Comparator
  2. Name Comparator

Continue reading Definition Of Comparator Interface

Java Collections

As you see from the last post, choices have methods such as the add() method, well if you don’t know… methods such as add() comes from a Framework in Java called Collections. Collections is a Framework (as I said) that provides the user an architecture that is used to store and manipulate a group of objects.
Not only add() method that you can do, you can do much more like : searching, insertion, sorting, deletion, manipulation etc… in Java Collections.

Here’s a list of what Collections provides :

  1. Deque
  2. List
  3. Set
  4. Queue

 Classes :

  1. ArrayList,
  2. LinkedList,
  3. HashSet,
  4. Treeset, etc…

What is a Framework in java?
Framework provides a readymade architecture that represents a set of classes and interface and it’s optional.

Here’s a list of methods that Collection provides :

  • public boolean add(Object element) :   this is  used to insert an element in this collection.
  • public boolean remove(Object element) :   the method s used to delete an element from this collection.
  • public boolean removeAll(Collection c)  :  this one is used to delete all the elements of specified collection from the invoking collection.
  • public int size()  :  will return the total number of elements in the collection.
  • public boolean contains(Object element) :   all it used is to search an element.
  • public Iterator iterator()   : this’ll returns an iterator.
  • public Object[] toArray()  :  will converts collection into array.
  • public boolean equals(Object element) : will  matches two collection.

well since I didn’t put all of it, I’m gonna say … ETC.

Iterator interface?
An Iterator interface will provides a facility for iterating the elements in forward direction only.

Here’s a list of methods in Iterator interface :

  1. public boolean hasNext() : it will returns true boolean if iterator has more elements.
  2. public object next() : it returns the element and moves the cursor pointer to the next element.
  3. public void remove() : it removes the last elements returned by the iterator. It is rarely used.

 Conclusions

  • Collections is a Framework (as I said) that provides the user an architecture that is used to store and manipulate a group of objects.
  • Framework provides a readymade architecture that represents a set of classes and interface and it’s optional.
  • An Iterator interface will provides a facility for iterating the elements in forward direction only.

    Iterating List == ListIterator Interface

    Ever heard of ListIterator? Ever try it on real life coding?, if no then let’s try it together.A few little thing that you need to know before we start, ListIterator interface contains a method that can insert or delete, and.. transverse element in forwards even backwards direction, oh and yea! I forgot to mention that ListIterator interface is a subinterface out of Collection keep that in mind.The method in the ListIterator interface only consist of 7 method, which I’m sure you’ll get the hang of it if you understand the whole thing and you get enough practice. Let’s start with knowing the method that List interface provide :

    First:
    public void add(int index, Object element);
    This method is used to add an element/object into the collection.


    Second:
    public void add(int index, Collection c);
    This method is used to add a collection of an element to an existing collection.

    Third:
    public object get(int index position);
    This method is used to get (output) an element from a collection based on the index position.

    Fourth:
    public object set(int index, object element);
    This method is used to set/replace an existing element from inside a collection.

    Fifth :
    public object remove(int index position);
    Now for this one is different from the methods before, because this method remove an object from the collection instead of set/create/get an element from a collection.

    Sixth and Seventh :
    public ListIterator listIterator();
    public ListIterator listIterator(int i);
    This methods is the one that iterate and list the collection of object.

    For the record, the method above is methods that is frequently used in List interface, we haven’t got into the ListIterator yet, the methods that is frequently used in ListIterator interface are 4 methods, see them :

    • public boolean hasNext(); This method will check if there are any object left in the collection in forward direction.
    • public object next(); This method will output the next object in the collection if hasNext() boolean turns out to be true.
    • public boolean hasPrevious(); This method will check if there are any object left in the collection in backwards direction.
    • public object previous(); This method will output the next object in the collection if hasPrevious() boolean turns out to be true.

    Now let’s take an example, let’s work with  the method : add, set,and we’ll iterate the list in backwards direction just to make things a little bit more interesting. Now, let’s go ! :

        import java.util.*; 
        public class ListIteratorExmpl{ 

        public static void main(String args[]){ 
        ArrayList<String> li=new ArrayList<String>(); 

        //first we create an array (which is the collection of object/element)
        li.add(“Ipang”);  //we add the first element to array li which is “Ipang”
        li.add(“Ipeng”);  //we add the second element to array li which is “Ipeng”
         
        System.out.println(“Now the 1st element is : “+ li.set(1, “Ipung”)); 

       /*we set and overwrite the first element from “Ipang” to “Ipung”, now the first elemet is”Ipung”*/
         
        ListIterator<String> itr = li.listIterator();   //now we create the iterator    
         
        System.out.println(“Now we are iterating elements in a backward direction :”); 
        while(itr.hasPrevious()){  

        /*this will make a boolean condition to true based on : while there are still objects in the array in backwards direction*/
        System.out.println(itr.previous());

       /* this is a continuation from the boolean above, if true : output the objects over and over until there are no objects left in backwards direction other way false */ 
         } 
        } 
        }  

    This program will output :
    Now the 1st element is : Ipung
    Ipeng
    Ipung // which actually before this was Ipang, but we changed it in the set() method.

    You see that it outputs the array in backwards direction? its because we’re using the previous() method.

     Conclusions 

    • ListIterator interface contains a method that can insert or delete, and.. transverse element in forwards even backwards direction.
    • ListIterator interface is a subinterface out of Collection
    • There are 7 frequently used method in List interface.
    • There are 4 frequently used method in ListIterator interface.
    • We can output values from forwards nor backwards direction.