Showing posts with label java tutorials for webdriver. Show all posts
Showing posts with label java tutorials for webdriver. Show all posts

Sunday, August 26, 2018

no image

Frequently Asked Coffee Interview Questions Business Office - 4

Part 4

16 : Can nosotros overload static methods? 

Answer : Yes.. There is non whatsoever restriction to overload static methods. We tin post away overload static together with non static methods inwards java. VIEW MORE on overloading inwards java.

17 : Can nosotros operate somebody fellow member of bring upwardly class inwards sub class?

Answer : No.. It volition non permit to operate somebody members similar somebody method, variable of bring upwardly class inwards little class. Private members are accessible solely within same class. VIEW MORE close class modifiers.

18 : What is an interface inwards java?

Answer : An interface is a bluish impress of a class which tin post away concur abstract methods (Methods without implementation) only. It creates Rules To Follow construction for class where It Is Implemented. We tin post away make 100% abstraction using interface inwards java. READ MORE close interface inwards java.

19 Can nosotros access protected method of bring upwardly class inwards sub class? 

Answer : Yes.. We tin post away access protected members of bring upwardly class inwards all it's sub classes together with classes within the same package. VIEW EXAMPLE on how to access protected method inwards sub class.

20 : What is an array inwards java?

Answer : An array is container object inwards coffee which tin post away concur fixed give away of values of same type. VIEW ARTICLE on array.

Saturday, August 25, 2018

no image

What Is Purpose Of Super Keyword Inwards Java?

Earlier nosotros stimulate got talked almost method overriding concept inwards coffee during THIS POST to modify the implementation of nurture class's method inwards sub class. In inheritance, Super keyword is used to refer object of immediate nurture class. Generally nosotros are using super keyword inwards inheritance for 3 unlike purpose. Let's run across how as well as when to usage super keyword inwards java. Interviewer tin enquire yous this query as well as then delight sympathise how it works.

super keyword usage
  1. To refer to the variable of nurture class.
  2. To invoke the constructor of immediate nurture class.
  3. To refer to the method of nurture class.
Let's stimulate got 1 illustration to sympathise higher upwards 3 things. We stimulate got nurture course of teaching Animal and it's sub class Elephant as bellow.

Animal.java
public course of teaching Animal {  int age = 40;  Animal(){   System.out.println("Animal is moving.");  }    void welcome(){   System.out.println("Welcome to Animal class.");  } }



Elephant.java
public course of teaching Elephant extends Animal {  int age = 100;   Elephant() {   // To invoke nurture course of teaching constructor Animal().   super();   System.out.println("Elephant is running.");    welcome();   // Used super.welcome() to telephone band nurture course of teaching method.   super.welcome();  }   void showAge() {   System.out.println("Average historic menstruum of Elephant : " + age);   // Used super.age to access nurture course of teaching variable value.   System.out.println("Average historic menstruum of Animals : " + super.age);  }   void welcome() {   System.out.println("Welcome to Elephant class.");  }   populace static void main(String[] args) {   Elephant c = novel Elephant();   c.showAge();  } }

Output
Animal is moving. Elephant is running. Welcome to Elephant class. Welcome to Animal class. Average historic menstruum of Elephant : 100 Average historic menstruum of Animals : 40

In higher upwards illustration yous tin run across that nosotros stimulate got used
  1. super.age to refer nurture course of teaching variable age.
  2. super() to invoke immediate nurture course of teaching constructor Animal() and
  3. super.welcome() to access nurture course of teaching method.
This means yous tin invoke constructor of nurture course of teaching inwards to tyke course of teaching or access nurture course of teaching variable or method when same advert method or variable is available inwards tyke class.

<< PREVIOUS || NEXT >>

Tuesday, August 21, 2018

Java Collection Interface

Java Collection Interface

  • Collection is an interface inwards java.
  • Collection interface extends Iterable interface.
  • In hierarchy, Collection interface is rootage of List, Queue together with Set interfaces.

Collection interface extends Iterable interface Java Collection Interface

  • It represents unit of measurement of its elements. i.e. grouping of objects.
  • Some collections produce non permit duplicate elements exactly about allows duplicate elements.
  • Collection interface is base of operations on which collection framework is built.
  • Collection interface contains basic functioning methods every bit bellow.
  • boolean add(E element) - To brand certain that this collection contains the specified element. It volition supply truthful if object is added to collection. Return imitation if object is already at that spot together with collection does non permit duplicates.
  • boolean contains(Object element) - It volition supply truthful if collection already accept specified element.
  • boolean isEmpty() - Return truthful if collection is empty.
  • Iterator<E> iterator() - It volition supply an iterator for the invoking collection.
  • boolean remove(Object element) - It volition take away unmarried instance of given chemical part from collection. Return truthful if it is introduce else it volition supply false.
  • int size() - It volition supply seat out of elements introduce inwards invoking collection.
  •  Collection interface besides contains methods that operate on entire collections every bit bellow.
  • boolean addAll(Collection<? extends E> c) - It volition add together all elements of c to the invoking collection. Return truthful if functioning was success else it volition supply false.
  • boolean containsAll(Collection<?> c) - It volition supply truthful if this collection contains all the elements of specified collection.
  • void clear() - It volition take away all the elements from invoking collection.
  • boolean removeAll(Collection<?> c) - It volition take away all those elements from invoking collection which are available inwards c. It volition supply truthful if given elements removed else it volition supply false.
  • boolean retainAll(Collection<?> c) - Retains all the elements inwards invoking collection which are available inwards c.
  • Also at that spot are methods to perform array operations every bit bellow.
  • Object[] toArray() - Return array of elements from invoking collection.
  • <T> T[] toArray(T[] a) - Return array of all those elements from collection.
Basic Example of Collection
package JAVAExamples;  import java.util.ArrayList; import java.util.Collection; import java.util.LinkedList;  world shape CollectionExample {   world static void main(String[] args) {    // Collection ArrayList.   Collection<String> c1 = novel ArrayList<String>();   // Add items inwards ArrayList.   c1.add("ArrayList Item 1");   c1.add("ArrayList Item 2");   c1.add("ArrayList Item 3");   c1.add("ArrayList Item 4");   // Get collection Items list.   System.out.println("Collection Items are : " + c1);   // Get size of collection.   System.out.println("Size of collection is : " + c1.size());   // Remove exceptional from collection.   c1.remove("ArrayList Item 3");   System.out.println("Collection Items are : " + c1);    System.out.println();   System.out.println();           // Collection LinkedList.   Collection<String> c2 = novel LinkedList<String>();   // Add items inwards LinkedList.   c2.add("LinkedList Item 1");   c2.add("LinkedList Item 2");   c2.add("LinkedList Item 3");   c2.add("LinkedList Item 4");   // Get collection Items list.   System.out.println("Collection Items are : " + c2);   // Get size of collection.   System.out.println("Size of collection is : " + c2.size());   // Remove exceptional from collection.   c2.clear();   System.out.println("Collection Items are : " + c2);  } }

Output :
Collection Items are : [ArrayList Item 1, ArrayList Item 2, ArrayList Item 3, ArrayList Item 4] Size of collection is : four Collection Items are : [ArrayList Item 1, ArrayList Item 2, ArrayList Item 4]   Collection Items are : [LinkedList Item 1, LinkedList Item 2, LinkedList Item 3, LinkedList Item 4] Size of collection is : four Collection Items are : []


So this is basic regard of rootage collection interface. We volition larn dissimilar nestling interfaces of collection interface i past times i to buy the farm regard nearly how collection interface works.
Java Listing Interface

Java Listing Interface

  • List interface inwards coffee is ordered collection then it tin shop elements inwards sequence.
  • List interface is sub interface too it extends to Collection interface.
  • List interface tin concur duplicate elements then you lot tin insert duplicate elements too.
  • You tin access elements from listing past times integer index.
  • Collection framework has many interfaces inwards coffee too listing interface is ane of them.
  • List interface provides ListIterator to traverse through elements listing inwards backward too frontward directions.
List interface inwards coffee is ordered collection then it tin shop elements inwards sequence Java List Interface


Few by too large used methods inwards listing interface are equally bellow.
  • void add(int index,Object element) : It volition insert chemical cistron at specified index seat inwards list.
  • boolean addAll(int index,Collection c) : It volition add together all elements of given collection inwards to the listing at specified index position.
  • object get(int Index ) : It volition render object at specified index seat from list.
  • object set(int index,Object element) : It volition assign object at specified index seat inwards list.
  • object remove(int index) : It volition take away object from specified position.
  • ListIterator listIterator() : It volition returns the listing iterator at start of invoking list.
  • ListIterator listIterator(int x) : It volition returns the listing iterator at specified index seat inwards list.
  • int indexOf(Object obj) : It volition render index of rootage illustration of object from invoking list.
  • int lastIndexOf(Object obj) : It volition render index of concluding illustration of object from invoking list.
List interface is implemented past times many classes similar Arraylist, Linkedlist too Vector.  Here, I accept represented illustration of listing interface using few methods from dissimilar degree implementation.

package JAVAExamples;  import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.ListIterator;  populace degree InterfaceList {   populace static void main(String[] args) {   // Add elements inwards ArrayList.   List al = novel ArrayList();   al.add("New York");   al.add("Delhi");   al.add("Tokyo");      // Print arraylist elements.   System.out.println("ArrayList Elements");   System.out.print(al);   System.out.println();    // Add elements inwards linkedlist.   List ll = novel LinkedList();   ll.add("New York");   ll.add("Delhi");   ll.add("Tokyo");      // Print linkedlist elements.   System.out.println();   System.out.println("LinkedList Elements");   System.out.print(ll);   System.out.println();    // Get too impress arraylist's third element.   System.out.println();   System.out.println("Element at third seat inwards arralist is : " + al.get(2));    // Using ListIterator to traverse through frontward too backward directions inwards arralist.   ListIterator<String> itrtr = al.listIterator();    // Traverse inwards frontward direction.   System.out.println();   System.out.println("Traversing through arralist elements inwards frontward direction...");   piece (itrtr.hasNext()) {    System.out.println(itrtr.next());   }    // Traverse inwards backward direction.   System.out.println();   System.out.println("Traversing through arralist elements inwards backward direction...");   piece (itrtr.hasPrevious()) {    System.out.println(itrtr.previous());   }  } }

Output
ArrayList Elements [New York, Delhi, Tokyo]  LinkedList Elements [New York, Delhi, Tokyo]  Element at third seat inwards arralist is : Tokyo  Traversing through arralist elements inwards frontward direction... New York Delhi Tokyo  Traversing through arralist elements inwards backward direction... Tokyo Delhi New York

In higher upwardly illustration you lot tin encounter how arralist, linkedlist, croak method too listiterator works.
Java Collections Framework

Java Collections Framework

What is Java Collections Framework?
Java Collections Framework is grouping of dissimilar interfaces, classes as well as algorithms. In uncomplicated word, Collection agency unmarried unit of measurement of objects as well as framework agency skilful architecture. It is best architecture to shop grouping of reusable objects as well as manipulate them. Collection framework has many dissimilar interfaces, their sub interfaces as well as classes, their classes as well as sub classes. All the interfaces as well as classes has dissimilar purpose. You tin perform add, edit, delete, search, sort, etc operations on grouping of objects equally per class's methods as well as implementation.

 Java Collections Framework is grouping of dissimilar interfaces Java Collections Framework

Main Advantages Of Collection Framework
  • Main payoff of collection framework is it volition reduces your programming efforts equally at that spot are stimulate made interfaces, classes as well as their methods which you lot tin utilisation straight inwards your programme to perform performance on object collection. 
  • Collection is good construction framework as well as then you lot code lineament as well as speed volition hold out increased.
  • Anyone tin utilisation collection real easily equally it's interfaces, classes as well as methods real tardily to sympathize as well as use.
Interfaces as well as Classes of Collection Framework
Collection, Set, List, Queue, Deque, SortedSet, Map as well as SortedMap are essence interfaces of collection framework. Detailed description of collection framework's interfaces as well as it's classes is given on below given pages.


Monday, August 20, 2018

Java Arraylist Class

Java Arraylist Class

  • ArrayList course of teaching is sub course of teaching of collection interface which implements to List interface.
  • ArrayList course of teaching provides resizable-array thus it tin grow automatically every bit per requirement which resolves fixed Array limitation where you lot convey to pre-define the size of array.
  • It tin agree zippo elements too.
  • It is non synchronized implementation thus if multiple threads accessing it concurrently in addition to whatsoever ane from them modifies the entry in addition to then it must live synchronized externally.
  • Also it tin agree duplicate elements.
ArrayList course of teaching is sub course of teaching of collection interface which implements to List interface Java ArrayList Class

Important methods of ArrayList course of teaching are every bit below.
  • boolean add(E e) : It volition append given chemical component division at the cease of this ArrayList.
  • void add(int index, due east element) : It volition add together specified chemical component division at the given index inwards list.
  • boolean addAll(Collection<? extends E> c) : It volition append all the elements of given collection at the cease of array list. Appending fellowship volition live every bit per returned yesteryear collection's iterator.
  • boolean addAll(int index, Collection<? extends E> c) : It volition insert all elements of specified collection inwards ArrayList starting from given index.
  • void clear() : It volition clear List yesteryear removing all elements from it.
  • Object clone() : It volition render shallow re-create of ArrayList.
  • boolean contains(Object o) : It volition render truthful if given chemical component division is acquaint inwards list.
  • void ensureCapacity(int minCapacity) : It volition increases the capacity of array listing if required to brand certain that it tin shop the elements every bit per given minCapacity argument.
  • E get(int index) : It volition decease chemical component division from specified inced from array list.
  • int indexOf(Object o) : It volition render the index of specified object(first occurrence) from array list.
  • boolean isEmpty() : It volition banking concern jibe in addition to render truthful if array listing is empty.
  • Iterator<E> iterator() : It volition render an iterator over the elements from array list.
  • int lastIndexOf(Object o) : It volition render index of terminal occurrence of specified element.
  • ListIterator<E> listIterator() : It volition render the listing iterator over the elements of array list.
  • ListIterator<E> listIterator(int index) : It volition render the listing iterator over the elements of array listing starting from specified index.
  • E remove(int index) : It volition take chemical component division from array listing which is stored at specified index.
  • boolean remove(Object o) : It volition take specified element(first occurrence) from array list.
  • boolean removeAll(Collection<?> c) : It volition take all elements from listing which are specified inwards given collection.
  • protected void removeRange(int fromIndex, int toIndex) : It volition take elements from listing starting from fromIndex to toIndex.
  • boolean retainAll(Collection<?> c) : It volition retain exclusively those elements inwards array listing which are specified inwards given collection.
  • E set(int index, due east element) : It volition supervene upon given chemical component division inwards array listing at specified position.
  • int size() : It volition render size of array list.
  • List<E> subList(int fromIndex, int toIndex) : It volition render sub listing of elements from this listing starting from fromIndex(inclusive) to toIndex(exclusive).
  • Object[] toArray() :It volition render proper sequential array of all array listing elements.
  • void trimToSize() :It volition cut size of array listing every bit per it's electrical flow size.
Below given event volition demo you lot exercise of ArrayList class's unlike methods.

ArrayListExample
package JAVAExamples;  import java.util.ArrayList; import java.util.Iterator;  populace course of teaching ArrayListClassExample {   populace static void main(String[] args) {   // Create array list.   ArrayList<String> a = novel ArrayList<String>();   // Add elements inwards array list.   a.add("one");   a.add("two");   a.add("three");   a.add("four");   a.add("one");   a.add("six");   a.add("seven");   a.add("eight");    //Iterating over the arraylist elements.   Iterator itr = a.iterator();   System.out.println("ArrayList items are : ");   piece (itr.hasNext()) {    System.out.println(itr.next());   }      //Add special at specified index.   a.add(5, "nine");   System.out.println("Now ArrayList items are : "+a);      //Get special from specified index.   System.out.println("Item at index 6 is : "+a.get(6));      //Remove special from array list.   a.remove("three");   System.out.println("Now ArrayList items are : "+a);      //Get index of chemical component division from array list.   System.out.println("Index of ane is : "+a.indexOf("one"));      //Get terminal index of chemical component division from array list.   System.out.println("Last Index of ane is : "+a.lastIndexOf("one"));  } }

Output :
ArrayList items are :  ane ii iii iv ane 6 7 8 Now ArrayList items are : [one, two, three, four, one, nine, six, seven, eight] Item at index 6 is : 6 Now ArrayList items are : [one, two, four, one, nine, six, seven, eight] Index of ane is : 0 Last Index of ane is : 3

Thursday, February 25, 2016

Vector Cast Inwards Java

Vector Cast Inwards Java

  • Vector cast inwards coffee implements List interface of collection framework.
  • Vector cast is synchronized.
  • If you lot don't know size of array therefore you lot tin piece of employment vector cast every bit size of vector tin grow together with shrink every bit per adding together with removing items.
  • As vector cast is synchronized, It volition rank pitiable performance on add, delete, update together with search operations.
  • Elements of vector tin live accessed using it's integer index.
Vector cast inwards coffee implements List interface of collection framework Vector Class In Java

Important method of vector class
  • void addElement(Object element) : It volition add together specified chemical component at destination of vector.
  • int capacity() : It volition supply the electrical flow capacity of vector.
  • int size() : This method volition supply electrical flow size of vector.
  • void setSize(int size) : It volition gear upwards size of vector using given size value.
  • boolean contains(Object element) : It volition supply truthful if specified chemical component acquaint inwards vector. Else it volition supply false.
  • boolean containsAll(Collection c) : It volition supply truthful if vector contains all values of given collection c.
  • Object elementAt(int index) : It volition supply chemical component which is located at specified index of vector.
  • Object firstElement() : It volition supply get-go chemical component of vector.
  • Object lastElement() : It volition supply concluding chemical component of vector.
  • Object get(int index) : It volition supply chemical component located at the specified index of vector.
  • boolean isEmpty() : It volition supply truthful if vector is empty.
  • boolean removeElement(Object element) : It volition take given chemical component from vector.
  • boolean removeAll(Collection c) : It volition take all elements of collection c from vector.
  • void setElementAt(Object element, int index) : It volition gear upwards specified chemical component at given index of vector.
Bellow given sample plan volition demo you lot how to piece of employment vector cast together with it's unlike methods.

Vector cast example
package JAVAExamples;  import java.util.Enumeration; import java.util.Vector;  populace cast VectorExample {  populace static void main(String args[]) {   //Initial vector capacity is 2. Increment it past times 2 when required.   Vector 5 = novel Vector(2,2);   System.out.println("Initial capacity of vector : "+v.capacity());   v.addElement("one");   v.addElement("two");   v.addElement("three");     System.out.println("Capacity of vector subsequently adding 3 elements inwards vector : "+v.capacity());    //Get size of vector.   System.out.println("Size of vector : "+v.size());    //Get get-go chemical component of vector.   System.out.println("First chemical component of vector : "+v.firstElement());      //Get concluding chemical component of vector.   System.out.println("Last chemical component of vector : "+v.lastElement());      //Add novel chemical component inwards vector.   v.add(2, "New Element");      //Print all elements of vector using Enumeration.   Enumeration vEnum = v.elements();   System.out.print("Current vector elements : ");   while(vEnum.hasMoreElements()){    System.out.print(vEnum.nextElement() + ", ");   }      System.out.println();      //Check if vector is empty.   System.out.println("Vector is empty? : "+v.isEmpty());    } }

Output :
Initial capacity of vector : 2 Capacity of vector subsequently adding 3 elements inwards vector : four Size of vector : 3 First chemical component of vector : 1 Last chemical component of vector : 3 Current vector elements : one, two, New Element, three,  Vector is empty? : false

Above instance shows you lot usage of by together with large used methods of vector class. http://www.feedbooks.com/user/4455042/profile
Stack Degree Inwards Coffee Example

Stack Degree Inwards Coffee Example

  • Stack bird inward coffee is i of the collection interface bird which is subclass of Vector class.
  • Stake bird supports park force together with popular operations.
  • In contrast to queue, Stack bird has last-in first-out(LIFO) information structure. So detail which is inserted at hap volition live available first.
  • Stack bird extends Vector bird of List interface therefore it includes all methods of vector bird together with likewise it has it's ain several methods to perform force together with popular operations similar looking hap detail from stack, removing hap detail from stack, force novel detail at hap of stack, searching detail from stack together with cheque whether stack is empty.
Stack bird inward coffee is i of the collection interface bird which is subclass of Vector c Stack Class inward Java Example



Important Methods of Stack Class inward Java


  • boolean empty() : This method of stack bird volition manage y'all to cheque if stack is empty.
  • Object peek( ) : It volition expect together with provide hap chemical subdivision of stack. It volition non take away chemical subdivision from stack.
  • Object pop( ) : It volition take away detail from hap of the stack together with provide the value.
  • Object push(E item) : It volition force an detail on hap of the stack
  • int search(Object o) : It volition lift one's heed if object is be inward stack. If be together with then it volition provide item's index. Top detail of stack has index = 1.
Below given practical instance of coffee stack bird volition present y'all how to a higher house methods work.

package JAVAExamples;  import java.util.Stack;  world bird JavaStackExample {  world static void main(String args[]) {   //Create stc stack.   Stack stc = novel Stack();   //Initially stack volition live empty.   System.out.println("Stack is empty? : "+stc.empty());   //Push items inward stack.   stc.push("Item1");   stc.push("Item2");   stc.push("Item3");   //Now stack if filled.   System.out.println("Now stack empty? : "+stc.empty());   //Print stack items.   System.out.println("stack Ietms : " + stc);   //Get hap detail from stack.   System.out.println("Top detail inward stack is : "+stc.peek());   //Print stack items afterwards peek.   System.out.println("stack Ietms afterwards peek : " + stc);   //Get together with take away hap detail from stack.   System.out.println("Get hap detail from stack together with removed it from stack : "+stc.pop());   //Print stack items afterwards pop.   System.out.println("stack Ietms afterwards popular : " + stc);   //Push novel detail inward stack.   stc.push("Item4");   //Print stack afterwards inserting novel item.   System.out.println("Now stack Ietms are : " + stc);   //Search detail from stack which is available.   System.out.println("Search Item1 inward stack is at index : "+stc.search("Item1"));   //Search detail from stack which is non available inward stack.   System.out.println("Search Item7 inward stack which is non available is : "+stc.search("Item7"));  } }


Output :
Stack is empty? : truthful Now stack empty? : faux stack Ietms : [Item1, Item2, Item3] Top detail inward stack is : Item3 stack Ietms afterwards peek : [Item1, Item2, Item3] Get hap detail from stack together with removed it from stack : Item3 stack Ietms afterwards popular : [Item1, Item2] Now stack Ietms are : [Item1, Item2, Item4] Search Item1 inward stack is at index : iii Search Item7 inward stack which is non available is : -1

Now i intend y'all volition empathize how together with when to role stack bird inward java.
Java Queue Interface

Java Queue Interface

  • If you lot hold back at collection hierarchy, Queue extends collection interface.
  • Main operate of collection is to concur the elements prior to processing.
  • Queue interface provides to a greater extent than or less additional operations similar insertion, inspection too extraction too collection interface operations.
  • All these 3 operations be inwards 2 dissimilar forms. One render exceptional value(null or faux depending on the operation) if functioning fails else throws an exception if functioning fails.
  • Queue follows FIFO(first-in-first-out) too then it volition insert chemical component subdivision at the terminate of the queue when you lot insert novel chemical component subdivision too chemical component subdivision volition endure removed from the kickoff of the queue when you lot withdraw element.
  • As Queue interface is sub type of collection interface, all methods of collection interface are also available in Queue interface.
Main operate of collection is to concur the elements prior to processing Java Queue Interface


 Basic methods of Queue interface
Few of the basic Queue interface methods are every bit below.
  • boolean add(E e) : It volition insert an chemical component subdivision inwards queue if infinite is available inwards queue. Else it volition render IllegalStateException.
  • E element() : It volition render head(first element) of queue if queue inwards non empty. It volition render NoSuchElementException exception if Queue is empty.
  • boolean offer(E e) : It volition insert chemical component subdivision inwards queue if infinite is available inwards queue.
  • E peek() : It volition render head(first element) of queue if queue inwards non empty. Else it volition render null.
  • E poll() : It volition think too withdraw head(first element) of the element. Return cipher if queue is empty.
  • E remove() : It volition think too withdraw head(first element) of the element. It volition render NoSuchElementException exception if Queue is empty.
I accept prepared instance on basic Queue interface methods to present demo how they works.

Queue Interface Example
package JAVAExamples;  import java.util.LinkedList; import java.util.Queue;  world course of teaching QueueExample {   world static void main(String[] args) {   //Creating queue.   Queue q = novel LinkedList();      //Inserting elements inwards queue.   q.add("three");   q.add("two");   q.add("one");     q.add("four");   q.add("six");   q.add("seven");   q.add("five");      //Print queue elements.   System.out.println("Queue elements are : "+q);      //Removing firstly inserted chemical component subdivision from queue using withdraw method.   q.remove();   System.out.println("Queue elements afterward chemical component subdivision removal using withdraw method : "+q);      //retrieve caput of queue (first element) using chemical component subdivision method.   System.out.println("Now firstly chemical component subdivision inwards queue is : "+q.element());      //Insert chemical component subdivision inwards queue using offering method.    q.offer("eight");   System.out.println("Queue elements afterward inserting chemical component subdivision using offering method : "+q);      //Removing specific chemical component subdivision from queue.   q.remove("six");   System.out.println("Queue elements afterward removing half dozen chemical component subdivision : "+q);    //Removing caput of queue (first element) using poll method.   q.poll();   System.out.println("Queue elements afterward removing chemical component subdivision using poll method : "+q);      //retrieve caput of queue (first element) using peek method.   System.out.println("Now firstly chemical component subdivision inwards queue is : "+q.peek());    } }

Output :
Queue elements are : [three, two, one, four, six, seven, five] Queue elements afterward chemical component subdivision removal using withdraw method : [two, one, four, six, seven, five] Now firstly chemical component subdivision inwards queue is : 2 Queue elements afterward inserting chemical component subdivision using offering method : [two, one, four, six, seven, five, eight] Queue elements afterward removing half dozen chemical component subdivision : [two, one, four, seven, five, eight] Queue elements afterward removing chemical component subdivision using poll method : [one, four, seven, five, eight] Now firstly chemical component subdivision inwards queue is : one

Java Priorityqueue Class

Java Priorityqueue Class

  • PriorityQueue Class is 1 of the collection framework degree which implements Queue, Serializable, Iterable too Collection interfaces.
  • It is using natural ordering to lodge thus elements of PriorityQueue.Or elements volition hold out ordered based on Comparator provided at queue construction fourth dimension depending on which constructor is used.
  • PriorityQueue is based on a priority heap. Heap is tree based information construction where all the nodes of tree are inward a specific order. Please read almost Heap to larn how it works.
  • PriorityQueue does non permit non-comparable objects too it may throw ClassCastException if you lot volition endeavour to create so.
  • PriorityQueue does non permit zippo elements every bit well.
  • PriorityQueue implementation is non synchronized. So multiple threads should non access it concurrently.
PriorityQueue Class is 1 of the collection framework degree which implements Queue Java PriorityQueue Class

Few methods which are used alongside PriorityQueue are every bit below.
  • boolean add(E e) : It volition insert specified chemical constituent inward PriorityQueue.
  • void clear() : It volition take away all elements from it to clear PriorityQueue.
  • boolean contains(Object o) : It volition banking corporation lucifer too render truthful if specified chemical constituent is available inward PriorityQueue. Else it volition render false.
  • boolean offer(E e) : It volition insert specified chemical constituent inward PriorityQueue.
  • E peek() : It volition render head(first) of the chemical constituent from queue. If PriorityQueue is empty thus it volition render null.
  • E poll() : It volition take away head(first) of the chemical constituent from queue. If PriorityQueue is empty thus it volition render null.
  • boolean remove(Object o) : It volition take away specified element's unmarried event from PriorityQueue if it is present.
  • int size() : It volition render size of PriorityQueue.
  • Object[] toArray() : It volition render elements array from queue.
  • <T> T[] toArray(T[] a) : It volition render elements array from queue. The runtime type of the returned array is that of the specified array.
Below given instance volition demo you lot how to operate PriorityQueue methods.

PriorityQueueExample :
package JAVAExamples;  import java.util.PriorityQueue;  populace degree PriorityQueueExample {   populace static void main(String[] args) {   //Create PriorityQueue.   PriorityQueue q=new PriorityQueue();   //Add items to PriorityQueue.   q.add("one");   q.add("two");   q.add("three");   q.add("four");   q.add("five");   q.add("six");   q.add("seven");      //Get size of PriorityQueue.   System.out.println("Size of PriorityQueue is : "+q.size());      //Print values of PriorityQueue.   System.out.println("PriorityQueue elements are : "+q);      //Get caput of PriorityQueue.   System.out.println("Head of PriorityQueue is : "+q.peek());      //Remove caput of PriorityQueue.   q.poll();   System.out.println("Now PriorityQueue elements are : "+q);    } }

Output :
Size of PriorityQueue is : 7 PriorityQueue elements are : [five, four, seven, two, one, three, six] Head of PriorityQueue is : 5 Now PriorityQueue elements are : [four, one, seven, two, six, three]

Java Deque Interface

Java Deque Interface

  • Deque is 1 of the collection interface which extends Queue interface.
  • Deque is linear collection which allow us to add together together with take chemical cistron from both ends of the queue.
  • It is "double ended queue" that's why it is called Deque together with commonly pronounced equally "deck".
  • Deque has dissimilar methods to perform insert, delete together with bear witness the chemical cistron inward queue.
  • Each of these methods convey 2 dissimilar forms. One volition throw an exception if it fails during performance together with other volition furnish a particular value similar naught or false.
Deque is 1 of the collection interface which extends Queue interface Java Deque Interface

Different methods of Deque interface are described below.

  • boolean add(E e) : It volition insert specified chemical cistron at the tail of this deque if infinite is currently available. Return truthful if performance is success or throw an IllegalStateException if fails to perform operation.
  • void addFirst(E e) : It volition insert specified chemical cistron at the get-go of the queue.
  • void addLast(E e) : It volition insert specified chemical cistron at the destination of the queue.
  • boolean contains(Object o) : It volition furnish truthful if specified chemical cistron is available inward queue.
  • E element() : It volition yell back head(first) chemical cistron from queue.
  • E getFirst() : It volition yell back inaugural of all chemical cistron from queue.
  • E getLast() : It volition yell back concluding chemical cistron from queue.
  • boolean offer(E e) : It volition insert specified chemical cistron at the tail of this deque. It volition furnish truthful if success together with faux if performance fails.
  • boolean offerFirst(E e) : It volition insert specified chemical cistron at the get-go of queue if infinite is available.
  • boolean offerLast(E e) : It volition insert specified chemical cistron at the destination of queue if infinite is available.
  • E peek() : It volition yell back head(first) chemical cistron from queue. Returns naught if queue is empty.
  • E peekFirst() : It volition yell back inaugural of all chemical cistron from queue. Returns naught if queue is empty.
  • E peekLast() : It volition yell back concluding chemical cistron from queue. Returns naught if queue is empty.
  • E poll() : It volition take head(first) chemical cistron from queue. Returns naught if queue is empty.
  • E pollFirst() : It volition take inaugural of all chemical cistron from queue. Returns naught if queue is empty.
  • E pollLast() : It volition take concluding chemical cistron from queue. Returns naught if queue is empty.
  • E pop() : It volition pops the chemical cistron from queue.
  • void push(E e) : It volition pushes the chemical cistron onto queue. It volition furnish truthful on success together with throe an exception if performance fails.
  • E remove() : It volition take inaugural of all chemical cistron from queue.
  • boolean remove(Object o) : It volition take inaugural of all occurrence of the given chemical cistron from queue.
  • E removeFirst() : It volition take inaugural of all chemical cistron from queue.
  • boolean removeFirstOccurrence(Object o) : It volition take inaugural of all occurrence of the given chemical cistron from queue.
  • E removeLast() : It volition take concluding chemical cistron from queue.
  • boolean removeLastOccurrence(Object o) : It volition take concluding occurrence of the given chemical cistron from queue.
  • int size() : It volition furnish size of queue.
I convey prepared representative of ArrayDeque to explore you lot how dissimilar methods of Deque works. Also you lot tin work LinkedList() using Deque.

Deque Example

package JAVAExamples;  import java.util.ArrayDeque; import java.util.Deque;  world flat DequeExample {   world static void main(String[] args) {   //Create ArrayDeque.   Deque<String> d = novel ArrayDeque<String>();   //Add elements inward Deque.   d.add("one");   d.add("two");   d.add("three");   d.add("four");   d.add("five");   d.add("four");   d.add("six");   //Print Deque elements.   System.out.println("Deque elements are : "+d);      //Add chemical cistron at inaugural of all of Deque.   d.addFirst("First Added");   System.out.println("Deque elements afterwards addFirst are : "+d);      //Add chemical cistron at concluding of Deque.   d.addLast("Last Added");   System.out.println("Deque elements afterwards addLast are : "+d);      //Get inaugural of all chemical cistron from Deque.   System.out.println("First chemical cistron inward deque is : "+d.getFirst());      //Get concluding chemical cistron from Deque.   System.out.println("Last chemical cistron inward deque is : "+d.getLast());      //Get inaugural of all chemical cistron from Deque using peek.   System.out.println("First chemical cistron inward deque using peek is : "+d.peek());      //Get concluding chemical cistron from Deque using peekLast.   System.out.println("Last chemical cistron inward deque using peekLast is : "+d.peekLast());      //Remove inaugural of all chemical cistron from deque.   d.removeFirst();   System.out.println("Deque elements afterwards removeFirst are  : "+d);      //Using pop.   d.pop();   System.out.println("Deque elements afterwards popular are  : "+d);      //Using push.   d.push("First");   System.out.println("Deque elements afterwards force are  : "+d);      //Remove LastOccurrence of chemical cistron from deque using removeLastOccurrence.   d.removeLastOccurrence("four");   System.out.println("Deque elements afterwards removeLastOccurrence are  : "+d);      //Get size of deque.   System.out.println("Size of Deque is  : "+d.size());  } }

Output :
Deque elements are : [one, two, three, four, five, four, six] Deque elements afterwards addFirst are : [First Added, one, two, three, four, five, four, six] Deque elements afterwards addLast are : [First Added, one, two, three, four, five, four, six, Last Added] First chemical cistron inward deque is : First Added Last chemical cistron inward deque is : Last Added First chemical cistron inward deque using peek is : First Added Last chemical cistron inward deque using peekLast is : Last Added Deque elements afterwards removeFirst are  : [one, two, three, four, five, four, six, Last Added] Deque elements afterwards popular are  : [two, three, four, five, four, six, Last Added] Deque elements afterwards force are  : [First, two, three, four, five, four, six, Last Added] Deque elements afterwards removeLastOccurrence are  : [First, two, three, four, five, six, Last Added] Size of Deque is  : 7

Java Arraydeque Class

Java Arraydeque Class

  • ArrayDeque is ane of the collection framework fellow member which implements Deque, Cloneable together with Serializable interfaces.
  • ArrayDeque provides Resizable-array implementation thus it has no capacity restrictions together with it volition grow every bit per requirement.
  • In the absence of external synchronization, they are non thread safe. So they produce non permit multiple threads to access it concurrently.
  • Also ArrayDequenot permit to shop zip elements.
  • ArrayDeque Class is faster than LinkedList when used every bit a queue together with faster than Stack when used every bit a stack.
  • If ArrayDeque is modified afterward the iterator creation together with if it is non modified past times iterators ain method together with then it volition throw ConcurrentModificationException. So inwards concurrent modification, Iterator volition fail.
ArrayDeque is ane of the collection framework fellow member which implements Deque Java ArrayDeque Class

Important methods of ArrayDeque Class
  • boolean add(E e) : It volition insert given chemical share inwards ArrayDeque.
  • void addFirst(E e) : It volition add together given chemical share at the kickoff of ArrayDeque.
  • void addLast(E e) : It volition add together given chemical share at the destination of ArrayDeque.
  • void clear() : It volition take away all elements from ArrayDeque to clear it.
  • ArrayDeque<E> clone() : It volition render re-create of ArrayDeque.
  • boolean contains(Object o) : It volition render truthful if specified chemical share available inwards ArrayDeque.
  • Iterator<E> descendingIterator() : It volition render iterator inwards contrary sequential social club over the elements of ArrayDeque.
  • E element() : It volition croak head(first) chemical share from this ArrayDeque.
  • E getFirst() : It volition croak head(first) chemical share from this ArrayDeque.
  • E getLast() : It volition croak concluding chemical share from this ArrayDeque.
  • boolean isEmpty() : It volition render truthful if ArrayDeque is empty.
  • boolean offer(E e) : It volition insert specified chemical share at the destination of this ArrayDeque.
  • boolean offerFirst(E e) : It volition insert specified chemical share at the kickoff of this ArrayDeque.
  • boolean offerLast(E e) : It volition insert specified chemical share at the destination of this ArrayDeque.
  • E peek() : It volition recall together with render caput chemical share from ArrayDeque. Return zip if ArrayDeque is empty.
  • E peekFirst() : It volition recall together with render head(first) chemical share from ArrayDeque. Return zip if ArrayDeque is empty.
  • E peekLast() : It volition recall together with render concluding chemical share from ArrayDeque. Return zip if ArrayDeque is empty.
  • E poll() : It volition take away caput chemical share from ArrayDeque. Return zip if ArrayDeque is empty.
  • E pollFirst() : It volition take away head(first) chemical share from ArrayDeque. Return zip if ArrayDeque is empty.
  • E pollLast() : It volition take away concluding chemical share from ArrayDeque. Return zip if ArrayDeque is empty.
  • E pop() : It volition popular an chemical share from stack represented past times this ArrayDeque.
  • void push(E e) : It volition pushes an chemical share on stack represented past times this ArrayDeque.
  • E remove() : It volition take away caput chemical share of the ArrayDeque.
  • boolean remove(Object o) : It volition take away unmarried illustration of specified object from ArrayDeque.
  • E removeFirst() : It volition take away firstly chemical share from ArrayDeque.
  • boolean removeFirstOccurrence(Object o) : It volition take away firstly occurrence of the chemical share from ArrayDeque.
  • E removeLast() : It volition take away concluding chemical share from ArrayDeque.
  • boolean removeLastOccurrence(Object o) : It volition take away concluding occurrence of the chemical share from ArrayDeque.
  • int size() :It volition render size of ArrayDeque.
  • Object[] toArray() : It volition render elements array from ArrayDeque.
  • <T> T[] toArray(T[] a) : It volition render elements array from ArrayDeque. The runtime type of the returned array is that of the specified array.
Below given illustration volition exhibit you lot demo of higher upwardly ArrayDeque  method's usage.

ArrayDeque Example
package JAVAExamples;  import java.util.ArrayDeque; import java.util.Deque;  populace shape ArrayDequeExample {   populace static void main(String[] args) {   // Create ArrayDeque.   ArrayDeque<String> d = novel ArrayDeque<String>();   // Add elements inwards Deque.   d.add("one");   d.add("two");   d.add("three");   d.add("four");   d.add("five");   d.add("four");   d.add("six");   // Print Deque elements.   System.out.println("Deque elements are : " + d);    // Add chemical share at firstly of Deque.   d.addFirst("First Added");   System.out.println("Deque elements afterward addFirst are : " + d);    // Add chemical share at concluding of Deque.   d.addLast("Last Added");   System.out.println("Deque elements afterward addLast are : " + d);    // Get firstly chemical share from Deque.   System.out.println("First chemical share inwards deque is : " + d.getFirst());    // Get concluding chemical share from Deque.   System.out.println("Last chemical share inwards deque is : " + d.getLast());    // Get firstly chemical share from Deque using peek.   System.out.println("First chemical share inwards deque using peek is : " + d.peek());    // Get concluding chemical share from Deque using peekLast.   System.out.println("Last chemical share inwards deque using peekLast is : " + d.peekLast());    // Remove firstly chemical share from deque.   d.removeFirst();   System.out.println("Deque elements afterward removeFirst are  : " + d);    // Using pop.   d.pop();   System.out.println("Deque elements afterward popular are  : " + d);    // Using push.   d.push("First");   System.out.println("Deque elements afterward force are  : " + d);    // Remove LastOccurrence of chemical share from deque using removeLastOccurrence.   d.removeLastOccurrence("four");   System.out.println("Deque elements afterward removeLastOccurrence are  : "+ d);    // Get size of deque.   System.out.println("Size of Deque is  : " + d.size());  } }

Output :
Deque elements are : [one, two, three, four, five, four, six] Deque elements afterward addFirst are : [First Added, one, two, three, four, five, four, six] Deque elements afterward addLast are : [First Added, one, two, three, four, five, four, six, Last Added] First chemical share inwards deque is : First Added Last chemical share inwards deque is : Last Added First chemical share inwards deque using peek is : First Added Last chemical share inwards deque using peekLast is : Last Added Deque elements afterward removeFirst are  : [one, two, three, four, five, four, six, Last Added] Deque elements afterward popular are  : [two, three, four, five, four, six, Last Added] Deque elements afterward force are  : [First, two, three, four, five, four, six, Last Added] Deque elements afterward removeLastOccurrence are  : [First, two, three, four, five, six, Last Added] Size of Deque is  : 7

Java Gear Upward Interface As Well As Methods

Java Gear Upward Interface As Well As Methods

  • Set is 1 of the collection framework interface which extends origin Collections interface.
  • Set collection tin non comprise duplicate elements.
  • Set interface has all methods which are inherited from Collection interface amongst special restriction of non allowing duplicate elements.
  • If 2 Set interface contains same elements together with then both are equal.
  • Set interface is implemented past times LinkedHashSet, HashSet classes together with extended past times SortedSet interface which is implemented past times TreeSet.
Set is 1 of the collection framework interface which extends origin Collections interface Java Set Interface And Methods
Important Methods of Set Interface
  • boolean add(E e) : It volition insert specified chemical cistron inwards laid if it is non available inwards set.
  • boolean addAll(Collection<? extends E> c) : It volition add together all elements of specified collection inwards laid if non already acquaint inwards set.
  • void clear() : It volition take away all elements from set.
  • boolean contains(Object o) : It volition provide truthful if  given chemical cistron is available inwards set.
  • boolean containsAll(Collection<?> c) : It volition provide truthful if all the elements of given collection are available inwards set.
  • boolean equals(Object o) :It volition compare specified object amongst laid to banking concern jibe equality.
  • boolean isEmpty() : It volition provide truthful if laid is empty.
  • int hashCode() :It volition returns hash code value for this set.
  • Iterator<E> iterator() : It volition provide an iterator over the elements inwards set.
  • boolean remove(Object o) : It volition take away specified chemical cistron from the laid if it is present.
  • boolean removeAll(Collection<?> c) : It volition take away all those elements from laid which are specified inwards collection.
  • int size() : It volition provide size of Set.
Below given Set interface event volition demo y'all demo of Set interface's few of import methods.

Java Set Interface Example
package JAVAExamples;  import java.util.HashSet; import java.util.Iterator; import java.util.Set;  world cast SetInterfaceExample {  world static void main(String args[]) {   Set s1 = novel HashSet();   // Check initial size of set.   System.out.println("Initial size of laid : " + s1.size());   // Add items inwards set. Try to add together duplicate exceptional inwards laid every bit laid tin non convey duplicate items.   s1.add("Item1");   s1.add("Item2");   s1.add("Item1");   s1.add("Item3");   s1.add("Item4");   // Print set. It volition demo solely iv items every bit laid tin non concur duplicate items.   System.out.println("Set items are : " + s1);   // Check if laid is empty. It volition provide false.   System.out.println("Set is empty? : " + s1.isEmpty());    // access laid items through Iterator.   Iterator iterator = s1.iterator();                 System.out.println("Set items are : ");   piece (iterator.hasNext()) {    String chemical cistron = (String) iterator.next();    System.out.println(element);   }    // Remove exceptional from set.   s1.remove("Item3");   // Set items afterwards removing Item3 from set.   System.out.println("Now Set items are : " + s1);   // Get laid size.   System.out.println("Set size is : " + s1.size());  } }

Output :
Initial size of laid : 0 Set items are : [Item1, Item2, Item3, Item4] Set is empty? : imitation Set items are :  Item1 Item2 Item3 Item4 Now Set items are : [Item1, Item2, Item4] Set size is : 3

This way, You tin role coffee laid to shop non duplicate items.

Wednesday, February 24, 2016

Java Linkedhashset Class

Java Linkedhashset Class

  • LinkedHashSet Class extends extends HashSet shape together with Implements Set interface inward collection interface hierarchy.
  • LinkedHashSet implementation differs from HashSet every bit it maintains doubly-linked listing running through all of its entries.
  • It maintains element's entries inward laid upwards every bit per it's insertion order. So it volition permit y'all insertion companionship iteration over the set. So when y'all iterate over LinkedHashSet, In which companionship it volition provide elements? Answer is As per it's insertion order.
  • So y'all volition function out same re-create of elements amongst the companionship how they inserted inward set. 
  • LinkedHashSetis non synchronized.
extends HashSet shape together with Implements Set interface inward collection interface hierarchy Java LinkedHashSet Class



Bellow given example volition demonstrate y'all basic methods of  LinkedHashSet together with it's usage.

package JAVAExamples;  import java.util.Iterator; import java.util.LinkedHashSet;  populace shape LinkedHashSetExample {   populace static void main(String[] args) {   LinkedHashSet<String> lhs = novel LinkedHashSet<String>();   // Add items to LinkedHashSet   lhs.add("one");   lhs.add("two");   lhs.add("three");   lhs.add("four");   // Print LinkedHashSet.   System.out.println(lhs);   // Get size of LinkedHashSet.   System.out.println("LinkedHashSet size Is : " + lhs.size());   // Check if LinkedHashSet is empty?   System.out.println("Is LinkedHashSet emplty? : " + lhs.isEmpty());   // Iterate through LinkedHashSet.   Iterator<String> itr = lhs.iterator();   System.out.println("LinkedHashSet Items are : ");   acre (itr.hasNext()) {    System.out.println(itr.next());   }   // Remove special from LinkedHashSet.   lhs.remove("three");   // Print LinkedHashSet.   System.out.println("Now LinkedHashSet items are : " + lhs);  } }

Output :
[one, two, three, four] LinkedHashSet size Is : 4 Is LinkedHashSet emplty? : fake LinkedHashSet Items are :  i 2 3 4 Now LinkedHashSet items are : [one, two, four]

Hashset Inwards Coffee Collection Example

Hashset Inwards Coffee Collection Example

  • HashSet is a cast of collection framework which extends AbstractSet cast in addition to implements the Set interface.
  • HasSet doesn't guarantee that elements guild volition stay same over the fourth dimension in addition to returned inwards whatever random order.
  • HasSet doesn't permit duplicate values. If you lot endeavour to insert duplicate, It volition overwrite.
  • HasSet allows to shop nothing values.
  • HasSet implementation is non synchronized.
HashSet is a cast of collection framework which extends AbstractSet cast in addition to implements  HashSet inwards Java Collection Example

Important Methods of HashSet
  • boolean add(E e) : To add together elements inwards to laid if it is non already present.
  • void clear() : Remove all entries from set.
  • Object clone() : It volition returns shallow re-create of HasSet instance.
  • boolean contains(Object o) : It volition provide truthful if given values is introduce inwards HashSet.
  • boolean isEmpty() : It volition provide truthful if HasSet is empty.
  • Iterator<E> iterator() : It volition returns an iterator over the elements inwards Set.
  • boolean remove(Object o) : It volition take specified elements from laid if it is available inwards HashSet.
  • int size() : It volition provide size of HashSet.
Bellow given illustration volition demo you lot utilise of unlike HashSet methods.
Example Of HashSet inwards Java
package JAVAExamples;  import java.util.HashSet;  populace cast HashSetJavaExample {  populace static void main(String args[]) {  HashSet hset = novel HashSet();  //Check if HashSet is empty.  System.out.println("HashSet is empty? : "+hset.isEmpty());  //Add elements inwards HashSet.  hset.add("One");  hset.add("Two");  hset.add("Three");  hset.add(null); //HassSet allows nothing values.  hset.add("Four");  //Print HashSet.  System.out.println("HashSet elements are : "+hset);  //Check HashSet size.  System.out.println("Size of HashSet is : "+hset.size());  //Removing chemical cistron from HashSet.  hset.remove("Two");  System.out.println("Now HashSet elements are : "+hset);  } }

Output :
HashSet is empty? : truthful HashSet elements are : [null, One, Four, Two, Three] Size of HashSet is : five Now HashSet elements are : [null, One, Four, Three]

This way, You tin post away role HashSet to shop values inwards random guild including null.

Sunday, February 21, 2016

Java Sortedset Interface

Java Sortedset Interface

  • SortedSet interface extends Set interface inwards collection framework of java.
  • As parent suggest, It provides full ordering on elements of Set.
  • Set elements volition live ordered using their natural ordering or using a comparator interface.
  • All the elements entries of SortedSet must implement Comparable interface.
  • All the elements of SortedSet must live comparable. So e1.compareTo(e2) must non throw ClassCastException.
  • In collection framework, TreeSet cast is an implementation for the SortedSet interface.
SortedSet interface extends Set interface inwards collection framework of coffee Java SortedSet Interface

Few of import as well as useful methods of SortedSet Interface are every bit bellow.
  • Comparator comparator( ) : It volition render the comparator which is used to guild the elements inwards invoking set. If elements of laid upwards are ordered using natural ordering as well as then it volition render null.
  • Object first( ) : It volition render the starting fourth dimension chemical component subdivision from the set.
  • SortedSet headSet(E toElement) : It volition render those elements from SortedSet which are  strictly less than toElement. toElement volition live non included.
  • Object last() : It volition render electrical current concluding chemical component subdivision from the set.
  • SortedSet subSet(E fromElement, eastward toElement) : It volition render laid upwards of elements from SortedSet starting from fromElement to toElement. fromElement volition live included simply toElement volition live excluded.
  • SortedSet tailSet(E fromElement) : It volition render laid upwards of elements starting from fromElement.
Usage of inwards a higher house SortedSet methods is described inwards below given example.

SortedSet Example :
package JAVAExamples;  import java.util.Iterator; import java.util.SortedSet; import java.util.TreeSet;  world cast SortedsetInterface {   world static void main(String[] args) {   // Create the sorted set.   SortedSet<String> s = novel TreeSet<String>();    // Add items to the sorted.   s.add("New York");   s.add("Delhi");   s.add("Tokyo");   s.add("London");   s.add("Mumbai");   s.add("Chennai");    // Iterate over the elements of laid upwards as well as print.   Iterator<String> it = s.iterator();   System.out.println("SortedSet Items are : ");   spell (it.hasNext()) {    Object chemical component subdivision = it.next();    System.out.println(element.toString());   }      //Get starting fourth dimension detail from set.   System.out.println("First detail inwards laid upwards : "+s.first());   //Get concluding detail from set.   System.out.println("Last detail inwards laid upwards : "+s.last());    // Using subSet method to become items from given FROM as well as TO elements of set.   // TO chemical component subdivision volition live excluded.   System.out.println("subSet items betwixt Delhi as well as New York : " + s.subSet("Delhi", "New York"));   // Using headSet method to become heading items from given item.   System.out.println("headSet from London : " + s.headSet("London"));   // Using tailSet method to become tailing items from given item.   System.out.println("tailSet from London : " + s.tailSet("London"));   // Check which comparator is used to variety elements.   // If render nada as well as then it has used natural ordering.   System.out.println(s.comparator());  } }

Output :
SortedSet Items are :  Chennai Delhi London Bombay New York Tokyo First detail inwards laid upwards : Chennai Last detail inwards laid upwards : Tokyo subSet items betwixt Delhi as well as New York : [Delhi, London, Mumbai] headSet from London : [Chennai, Delhi] tailSet from London : [London, Mumbai, New York, Tokyo] null