Skip to main content

Posts

Reversing the order of the elements in the specified list.

package  collection.demos; import  java.util. ArrayList ; import  java.util. Collections ; public   class  ReversingListElements  {      public   static   void   main ( String []  args )   {          ArrayList < String >  activities = new   ArrayList < String > () ;         activities. add ( "analysis" ) ;         activities. add ( "design" ) ;         activities. add ( "development" ) ;         activities. add ( "testing" ) ;         activities. add ( "support" ) ;          System .out. println ( "               List :...

Collections : Searching the specified list for the specified object using the binary search algorithm

Searches the specified list for the specified object using the binary search algorithm. The list must be sorted into ascending order according to the natural ordering of its elements (as by the sort(List) method prior to making this call. package  collection.demos; import  java.util. ArrayList ; import  java.util. Collections ; import  java.util. Comparator ; class  MyComparator  implements   Comparator < String > {      public   int   compare ( String  o1,  String  o2 )   {          return  o1. compareTo ( o2 ) ;      } } public   class  BinarySearchDemo  {      public   static   void   main ( String []  args )   {          ArrayList < String >  names = new   ArrayList < String > () ;  ...

The Hashtable works like HashMap. Only diffrence is that, all methods are synchronized

package  collection.demos; import  java.util. Collection ; import  java.util. Hashtable ; import  java.util. Map ; import  java.util. Set ; public   class  HashTableDemo  {      public   static   void   main ( String []  args )   {          Map < String , Long >  phoneNumbers = new   Hashtable < String , Long > () ;          //Putting values in HashMap         phoneNumbers. put ( "Abc" ,  12345674L ) ;         phoneNumbers. put ( "xyz" ,  453453344L ) ;         phoneNumbers. put ( "Shoye" ,  23423433L ) ;         phoneNumbers. put ( "Jusd" ,  656565653L ) ;       ...

TreeSet with custom comparable.

 Using comparable to set the order of Objects in TreeSet  * Here we are creating a class Contact and each object of this class will represent contact of a person.  * In TreesetDemo1 class, we will create multiple objects of this class with different values of variables.  * After creating objects, we will add them to TreeSet and test their order.  *  * Here we are implementing Com parable interface to the class that will provide object to be placed into ordered way into TreeSet. * It has only single method compareTo(OtherObject obj) that will return int value. * The Object that implements Comparable interface properly, is placed into ordered way automatically. package  collection.demos; import  java.util. TreeSet ; class  Contact  implements   Comparable < Contact > {      String  name;      String  email;      long  phone; ...

TreeSet using Comparator

Using Comparator to set the order of Objects in TreeSet. There may be condition that your are not allowed to implements Comparable interface to class being used to create objects those will be sorted by TreeSet. Some time we requre to provide the sorting criteria on the fly or may be changed at any time according to requirements. We may need to provides different criteria for the same object for ordering on different places. In this situation, we have only one solution that, Create your own comparator. You can create your own comparator by implementing Comparator interface. Here you can provide you own implemetnation for compare method that wil be used by TreeSet to provide ordering for elements. package  collection.demos; import  java.io. Serializable ; import  java.util. TreeSet ; import  java.util. Comparator ; class  Contact  {      String  name;      String  email;   ...

Linked List

Linked list allows us to arrange elements into order. It maintain the order of elements in which they have added. Its iterator traverse on element into same order in they have added. It has all common operations related to linked list in our data structure like insert, remove, traverse etc. LinkedList class is an implementation of the List interface. It implements all optional list operations, and permits all elements. In addition to implementing the List interface, the LinkedList class provides uniformly named methods to get, remove and insert an element at the beginning and end of the list. These operations allow linked lists to be used as a stack, queue, or double-ended queue. package  collection.demos; import  java.util. Iterator ; import  java.util. List ; import  java.util. LinkedList ; import  java.util. ListIterator ; public   class  LinkedListDemo1  {      public   static   void  ...

TreeMap

It is NavigatableMap implemetnation based on Re-Black tree concept. It map is sorted according to the natural ordering of its keys, or by a Comparator provided at the time of creation. Note that this implementation is not synchronized. package  collection.demos; import  java.util. Map ; import  java.util.NavigableMap; import  java.util. TreeMap ; public   class  TreeMapDemo1  {      public   static   void   main ( String []  args )   {          TreeMap < String ,  Long >  phoneNumbers  =   new   TreeMap < String ,  Long > () ;          //Putting values in TreeMap         phoneNumbers. put ( "Abc" ,  12345674L ) ;         phoneNumbers. put ( "xyz" ,  45345334...