Wednesday, December 14, 2016

set interface - interface in java used list object

        A set is an interface in java which is used to maintain the list of an object is a sequence


The main difference between List and Set interface is set interface always contains unique value no any duplicate value in set interface.
 interface in java used list object

This Set Interface is implemented by HashSet class and LinkedHashSet Class and extended by SortedSet interface. The set interface doesn’t have own method for implementation but it uses the its parent Collection interface method and provide some restriction so that it doesn’t contain any duplicate value. Order in the Set interface is implementation dependent. Set contain only one null element.
 interface in java used list object

HasSet Class
The HashSet Class is the implementation of Set interface. This class will contain only unique values. It allows only one null element. Internally HashSet use HashMap to stores the objects. the element will we store in the HashSet object it will be stored as a HashMap means it will contain the key and value. The key itself is a HashMap and it value is constant which is also called present.
  interface in java used list object
Example
 interface in java used list object
Note: in above example I trying to insert the duplicate value but it doesn’t insert and also it just includes one null value. It doesn’t have any order to insert the elements.

LinkedHashSet Class

LinkedHashSet Class is implement the Set Interface. But this class will insert the element in ordered base. This class is extending the HashSet Class. Internally this class useLinkedHashMap class to sotre the elements in an object.
  interface in java used list object
Example
 interface in java used list object

SortedSet Interface
The sortedSet Interface is implements the Set interface. This interface is used to sort the elements in ascending order. It provides the several methods to get the advantage of sorting. This interface cannot have null element and also cannot have duplicate element. This interface also sorts the element according to given comparator. This interface is extending by the NavigableSet Interface than that NavigableSet interface is implements by TreeSet Class. interface in java used list object

TreeSet Class
The TreeSet class is implement the NavigableSet interface and this interface extends the SortedSet Interface. This class will sort the element according to natural ascending order or according to given comparator.

Example
 interface in java used list object
      
          Map Interface
Map interface is the interface of Java Collections Framework. Like List, Set and Queue interface it cannot inherit the Collection interface. Map Interface itself maintain the hierarchy like Collection Interface. This Map interface stores each value as a key in an object. Every single value has a unique Key. HashMap class and LinkedHashMap Class implement the Map Interface and SortedMap extend the Map Interface.

  o   Map interface is a part of Java Collection Framework, but it doesn’t inherit Collection Interface
  o   Map interface stores the data as a key-value pairs where each key is associated with a value.
  o   A map cannot have duplicate keys but can have duplicate values.
  o   Each key at most must be associated with one value.
  o   Each key-value pairs of the map are stored as Map.Entry objects. Map.Entry is an inner interface of     Map interface.
  o   The common implementations of Map interface are HashMap, LinkedHashMap and TreeMap.
  o   Order of elements in map is implementation dependent. HashMap doesn’t maintain any order of elements.LinkedHashMap maintains insertion order of elements. Whereas TreeMap places the elements according to supplied Comparator.
  o   The Map interface provides three methods, which allows map’s contents to be viewed as a set of keys(keySet() method), collection of values (values() method), or set of key-value mappings (entrySet() method).

HashMapClass
The HashMap class implement the Map interface. This class store unique elements with key and pair. The key is always unique. The class contain one null key and multiple null values and this class has no order of maintenance.

Example
 interface in java used list object
LinkedHashMap Class
The LinkedHashMap class implement the Map interface. This class store unique elements with key and pair. The key is always unique. The class contain one null key and multiple null values and this class has order of maintenance.

Example:
 interface in java used list object
SortedMap Interface
The sortedMap Interface is implements the Map interface. This interface is used to sort the elements in ascending order. It provides the several methods to get the advantage of sorting. This interface cannot have null element and also cannot have duplicate element. The elements will store as a Key and Value base. 

TreeMap Class
The TreeMap class is implement the NavigableMap interface and this interface extends the SortedMap Interface. This class store unique elements with key and pair. The key is always unique. The class contain one null key and multiple null values and this class maintain the order according to the natural ascending order.

Example
 interface in java used list object
Note:
In collection framework, the collections class provide the static method for sorting the element of collection but that static method will only work with Set Interface implement class TreeSet and we cannot sort the element of List interface. So, in order to possess the sorting of List interface,we will use Collections Class method for sort the List. In order to maintain the order of any Collection interface than the java provide us two types of interfaces.

  o   Collections Class
  o   Comparable Interface
  o   Comparator Interface

         Collections Class

As in above we discuss some collection framework. Now Collection framework provide some built-in algorithms which are commonly used in Java. These algorithms are implements by Collections Class. Collections class implement lot of methods but we are going to provide you some common and useful methods with implementation.
  
Example
 interface in java used list object



Comparable Interface
This interface is used to maintain the order of user defined class. This interface contains one method which is “compareTo (Object object)”. This method compares the current object value with specified object value. This interface sort the sequence of Any Collections class with single data member.

Example
In this example we created two classes

EmployeeDetail.java
 interface
employee.java
 interface  used list object
    
       Comparator Interface
This interface is used to maintain the order of user defined class. This interface contains two methodsin which one is “compare (Object object1, Object object2)” and second is “equals (Object object)”. The compare method compares the both objects value each other. This interface sort the sequence of Any Collections class with multiple data member.

         Generics.............
Java provide one of the power mechanism knowns as Generics. Generics provides the concept of objects safe type. Which means the generics force the programmers to specify the suitable Object type. By using generics then we no need of Type Casting. interface in java used list object

Syntax
                ClassOrInterface<type>

As you notice in above program we created two arrayList one is without generic and second is with generic type so it is possible to get error at run time during execution of collection with generic because we don’t know the type of object. interface in java used list object

Generic Class
In this section we will learn that how to create own generic class.

Syntax
class  ClassName<T1, T2, T3, …. ,Tn>{

}
T1, T2, T3, ….. , Tn here T stand for Type.

Example with Generic Class
 interface in java used list object

Now in above we create a simple Generic Class Type parameter. This class contain one Type data member, one constructor and the setter and getter method that Type data member. As we create the instance of above class then we specify the Type parameter, once you specify the Type parameter then it will support only that type of data otherwise it will throw exception. For example, if you create the Instance of above class with String type than it will only support String value.
 interface in java used list object

 interface in java used list object

Generic work with only Derived type if you put any primitive type in parameter type it will give you compile time error.


Example with Two Type Parameter
 interface in java used list object
When you want only some part of any class or interface to be a generic rather than the whole class or interface to be a generic than generic methods or constructors are the best solution of this type of thing.
You can create both method and constructor in non-generic class, there is no any restriction of that. You can also create static and non-static methods.

Example
 interface in java used list object
 interface in java used list object
Now in above as we notice that you do not need to use Drive class forcefully, you can also use primitive data type Instead of Derives datatypes.

Generic Naming Convention
As you notice that in above I always using T for generic. Actually it is not necessary that for generic you can always write T. You just have to provide a Generic with any Letter but letter should be a Capital here in above we specify T which means Type so its easily for reading that’s why I specify it.

For example, if you want to create Map <Key, Value> so its generic will be look like this Map <K, V>. now this is what say K means Key and V Means Value.

Generic Interface
Like generic class you can create generic interface.

Syntax
                Interface interfaceName<type> {
                                // implements method declaration         
}

Rules
 o   To implement the generic interface,you must have generic class.
 o   Normal class does not implement the generic interface. if you trying to implement the generic interface non-generic class you will get compile time error.
 o   Normal class can implement the interface if and only if the generic interface has wrapper class like: Integer, Double, Float, Long etc.
 o   The generic class that implement the generic interface that at least must has same type of parameter and same number of parameter.For example: your generic interface is “interface myInterfce<T1, T2, T3>” then implement class must have same number of parameter and also same type of parameter “class myClass<T1, T2, T3> implements myInterface<T1, T2, T3>” or “class myClass<T1, T2, T3, T4> implements myInterface<T1, T2, T3>”. But if you write this “class myClass<T1, T3, T5> implements myInterface<T1, T2, T3>” then, you will get an error because the number of parameter are same but type of parameter is different.

 interface in java used list object
                                                   interface in java used list object




       Java Date
In java following classes are able to represent the date and time in java.
 o   Java.util.Date class
 o   Java.sql.Date class
 o   Java .text class

Java.util.Date class
In java the java.util.Date class used to represent the date and time. It provides some constructor and methods 

Constructor
No.
Constructor
Description
1)
Date(long milliseconds)
Creates a sql date object for the given milliseconds since January 1, 1970, 00:00:00 GMT.

Methods
No.
Method
Description
1)
void setTime(long time)
changes the current sql date to given time.
2)
Instant toInstant()
converts current sql date into Instant object.
3)
LocalDatetoLocalDate()
converts current sql date into LocalDate object.
4)
String toString()
converts this sql date object to a string.
5)
static Date valueOf(LocalDate date)
returns sql date object for the given LocalDate.
6)
static Date valueOf(String date)
returns sql date object for the given String.
Example:


Java.text class
In Java the java.text is a package which contains lot of class and interface and this package provides the two different classes for date and time. Which are the following
 o   DateFormat class
 o   SimpleDateFormat class

DateFormat Class
This class provide the different type of method to format the date and time





DateFormat Fields
 o   protected Calendar calendar  
 o   protected NumberFormat numberFormat  
 o   public static final int ERA_FIELD  
 o   public static final int YEAR_FIELD  
 o   public static final int MONTH_FIELD  
 o   public static final int DATE_FIELD  
 o   public static final int HOUR_OF_DAY1_FIELD  
 o   public static final int HOUR_OF_DAY0_FIELD  
 o   public static final int MINUTE_FIELD  
 o   public static final int SECOND_FIELD  
 o   public static final int MILLISECOND_FIELD  
 o   public static final int DAY_OF_WEEK_FIELD  
 o   public static final int DAY_OF_YEAR_FIELD  
 o   public static final int DAY_OF_WEEK_IN_MONTH_FIELD  
 o   public static final int WEEK_OF_YEAR_FIELD  
 o   public static final int WEEK_OF_MONTH_FIELD  
 o   public static final int AM_PM_FIELD  
 o   public static final int HOUR1_FIELD  
 o   public static final int HOUR0_FIELD  
 o   public static final int TIMEZONE_FIELD  
 o   public static final int FULL  
 o   public static final int LONG  
 o   public static final int MEDIUM  
 o   public static final int SHORT  
o    public static final int DEFAULT



Methods
No.
Public Method
Description
1)
final String format(Date date)
converts given Date object into string.
2)
Date parse(String source)throws ParseException
converts string into Date object.
3)
static final DateFormatgetTimeInstance()
returns time formatter with default formatting style for the default locale.
4)
Static final DateFormatgetTimeInstance(int style)
returns time formatter with the given formatting style for the default locale.
5)
static final DateFormatgetTimeInstance(int style, Locale locale)
returns time formatter with the given formatting style for the given locale.
6)
static final DateFormatgetDateInstance()
returns date formatter with default formatting style for the default locale.
7)
static final DateFormatgetDateInstance(int style)
returns date formatter with the given formatting style for the default locale.
8)
static final DateFormatgetDateInstance(int style, Locale locale)
returns date formatter with the given formatting style for the given locale.
9)
static final DateFormatgetDateTimeInstance()
returns date/time formatter with default formatting style for the default locale.
10)
static final DateFormatgetDateTimeInstance(intdateStyle,inttimeStyle)
returns date/time formatter with the given date formatting style and time formatting style for the default locale.
11)
static final DateFormatgetDateTimeInstance(intdateStyle, inttimeStyle, Locale locale)
returns date/time formatter with the given date formatting style and time formatting style for the given locale.
12)
static final DateFormatgetInstance()
returns date/time formatter with short formatting style for date and time.
13)
static Locale[] getAvailableLocales()
returns an array of available locales.
14)
Calendar getCalendar()
returns an instance of Calendar for this DateFormat instance.
15)
NumberFormatgetNumberFormat()
returns an instance of NumberFormat for this DateFormat instance.
16)
TimeZonegetTimeZone()
returns an instance of TimeZone for this DateFormat instance.

No comments:

Post a Comment