The java.util package contains all the classes and interfaces of Collection Framework.
Two Main root interfaces : Collection & Map
Parent Interface of Collection is Interable
Iterable Interface :-The Iterable interface is the root interface for all the collection classes and all the collection class implement the Iterable interface.
Iterator<T> iterator();
// Returns true if the iteration has more elements
public boolean hasNext();
// Returns the next element in the iteration
// It throws NoSuchElementException if no more
// element present
public Object next();
// Remove the next element in the iteration
// This method can be called only once per call
// to next()
public void remove();
interface Collection<E> extends Iterable<E>
interface Map<K,V>
Collection : Root interface with basic methods like add(), remove(), contains(), isEmpty(), addAll(), ... etc.
interface are HashSet (Hashing based) and TreeSet (balanced
BST based). Note that TreeSet implements SortedSet.
implementations are LinkedList (linked list based) and
like PriorityQueue.
both LIFO and FIFO.
The difference between Set and Map interface is that in Set we
have only keys, whereas in Map, we have key, value pairs.
Array List :-
The important points about Java ArrayList class are:
- Java ArrayList class can contain duplicate elements.
- Java ArrayList class maintains insertion order.
- Java ArrayList class is non synchronized.
- Java ArrayList allows random access because array works at the index basis.
- In Java ArrayList class, manipulation is slow because a lot of shifting needs to occur if any element is removed from the array list
ArrayList<String> list=new ArrayList<String>();//Cr
- Iterator itr=list.iterator();
- while(itr.hasNext()){
- System.out.println(itr.next());
- }
- Second Way : For Each
- for(String obj:al)
- System.out.println(obj);
- }
Linked List :-
The important points about Java LinkedList are:
- Java LinkedList class can contain duplicate elements.
- Java LinkedList class maintains insertion order.
- Java LinkedList class is non synchronized.
- In Java LinkedList class, manipulation is fast because no shifting needs to occur.
- Java LinkedList class can be used as a list, stack or queue.
Java ListIterator Interface
Set in Java
- Set is an interface which extends Collection. It is an unordered collection of objects in which duplicate values cannot be stored.
- Basically, Set is implemented by HashSet, LinkedHashSet or TreeSet (sorted representation).
- Set has various methods to add, remove clear, size, etc to enhance the usage of this interface
HashSet :-
- HashSet stores the elements by using a mechanism called hashing.
- HashSet contains unique elements only.
- HashSet allows null value.
- HashSet class is non synchronized.
- HashSet doesn't maintain the insertion order. Here, elements are inserted on the basis of their hashcode.
- HashSet is the best approach for search operations.
- The initial default capacity of HashSet is 16, and the load factor is 0.75.
Internal working of a HashSet
All the classes of Set interface internally backed up by Map. HashSet uses HashMap for storing its object internally. You must be wondering that to enter a value in HashMap we need a key-value pair, but in HashSet we are passing only one value.
Storage in HashMap
Actually the value we insert in HashSet acts as key to the map Object and for its value java uses a constant variable. So in key-value pair all the keys will have same value.
Implementation of HashSet in java doc:
private transient HashMap map;
// Constructor - 1
// All the constructors are internally creating HashMap Object.
public HashSet()
{
// Creating internally backing HashMap object
map = new HashMap();
}
// Constructor - 2
public HashSet(int initialCapacity)
{
// Creating internally backing HashMap object
map = new HashMap(initialCapacity);
}
// Dummy value to associate with an Object in Map
private static final Object PRESENT = new Object();
LinkedHashSet :-
When iterating through a HashSet the order is unpredictable, while a LinkedHashSet lets us iterate through the elements in the order in which they were inserted. When cycling through LinkedHashSet using an iterator, the elements will be returned in the order in which they were inserted.
HashSet : Maintains no order
LinkedHashSet : Maintains insertion order.
TreeSet :-
- Objects in a TreeSet are stored in a sorted and ascending order.
Two things must be kept in mind while creating and adding elements into a TreeSet:
- Firstly, insertion of null into a TreeSet throws NullPointerException because while insertion of null, it gets compared to the existing elements and null cannot be compared to any value.
- Secondly, if we are depending on default natural sorting order, compulsory the object should be homogeneous and comparable otherwise we will get
HashSet Vs TreeSet
Java Map Interface :-
A map contains values on the basis of key, i.e. key and value pair. Each key and value pair is known as an entry. A Map contains unique keys. A Map is useful if you have to search, update or delete elements on the basis of a key.
A Map doesn't allow duplicate keys, but you can have duplicate values. HashMap and LinkedHashMap allow null keys and values, but TreeMap doesn't allow any null key or value.
Hash Map :-
- Java HashMap class contains values based on the key.
- Java HashMap class contains only unique keys.
- Java HashMap class may have one null key and multiple null values.
- Java HashMap class is non synchronized.
- Java HashMap class maintains no order.
- The initial default capacity of Java HashMap class is 16 with a load factor of 0.75.
Linked Hash Map :-
- Java LinkedHashMap contains values based on the key.
- Java LinkedHashMap contains unique elements.
- Java LinkedHashMap may have one null key and multiple null values.
- Java LinkedHashMap is non synchronized.
- Java LinkedHashMap maintains insertion order.
- The initial default capacity of Java HashMap class is 16 with a load factor of 0.75.
TreeMap :-
The important points about Java TreeMap class are:
- Java TreeMap contains values based on the key. It implements the NavigableMap interface and extends AbstractMap class.
- Java TreeMap contains only unique elements.
- Java TreeMap cannot have a null key but can have multiple null values.
- Java TreeMap is non synchronized.
- Java TreeMap maintains ascending order.
OOPs Concepts :-
Static Block :- Used for memory management
The static can be:
- Variable (also known as a class variable)
- Method (also known as a class method)
- Block
- Nested class
The static variable can be used to refer to the common property of all object
Memory : Class Arrea
Example : In API framework : Base URL is static
Static Method :- No Need to create object to call method Belongs to class & can change the value of static variable.
Static Block :-
- Initialize the static variable.
- It is executed before the main method at the time of classloading
- static blocks are executed before constructors.
Constructor :-
- A constructor in Java can not be abstract, final, static and Synchronized.
- A constructor is invoked at the time of object or instance creation.
- default constructor: A constructor that has no parameter is known as default constructor. If we don’t define a constructor in a class, then compiler creates default constructor(with no arguments) for the class
- Parameterized Constructor: A constructor that has parameters is known as parameterized constructor. If we want to initialize fields of the class with your own values, then use a parameterized constructor.
Constructor Overloading :-
- Like methods, we can overload constructors for creating objects in different ways. Compiler differentiates constructors on the basis of numbers of parameters, types of the parameters and order of the parameters.
Here is given the 6 usage of java this keyword.
- this can be used to refer current class instance variable.
- this can be used to invoke current class method (implicitly)
- this() can be used to invoke current class constructor.
- this can be passed as an argument in the method call.
- this can be passed as argument in the constructor call.
- this can be used to return the current class instance from the method.
Encapsulation :-
Abstraction
Hide internal implementation and just highlight the set of services, is called abstraction.
Example : ATM Machine & get() DriverManager API.
Two ways to achieve abstraction in java
- Abstract class (0 to 100%)
- Interface (100%)
Abstract Class :-A class which is declared as abstract is known as an abstract class
- An abstract class must be declared with an abstract keyword.
- It can have abstract and non-abstract methods.
- It cannot be instantiated.
- It can have constructors and static methods also.
- It can have final methods which will force the subclass not to change the body of the method.
- A method defined abstract must always be redefined in the subclass,thus making overriding compulsory OR either make subclass itself abstract.
- Avoids code duplication and increases reusability.
- Helps to increase security of an application or program as only important details are provided to the user.
- No. its not possible to declare abstract method with private .
- No. its not possible to declare abstract method with final keyword.
- No. its not possible to declare abstract method with static keyword.
- Yes we can declare abstract methods as protected.
- public, protected and default are valid.
- static, final and private are invalid.
Live Example :- Common functions class hold -> like scroll ,click,wait and extend Pages class
InterFace
Interfaces specify what a class must do and not how.
- it is used to achieve total abstraction.
- Since java does not support multiple inheritance in case of class, but by using interface it can achieve multiple inheritance .
- It is also used to achieve loose coupling.
- Interfaces are used to implement abstraction. So the question arises why use interfaces when we have abstract classes?
The reason is, abstract classes may contain non-final variables, whereas variables in interface are final, public and static.
New Feature Added in JDK 8 :-
- Default Method
- Static methods
OverLoading & OverRiding :-
OverLoading (Compile time Polymorphism)-> Happen within class & increases the readability of the program.
There are two ways to overload the method in java
- By changing number of arguments
- By changing the data type
Example :- Expectedcondition -> Class in Selenium
OverLoaded Functions :
- An expectation for checking an element is visible and enabled such that you can click it.
- An expectation for checking an element is visible and enabled such that you can click it.
Overriding :- Happen in case of Inheritance
- Method overriding is used to provide the specific implementation of a method which is already provided by its superclass.
- Method overriding is used for runtime polymorphism
Example : Click function present in helper class need to change body in page class
Super :-
- super can be used to refer immediate parent class instance variable.
- super can be used to invoke immediate parent class method.
- super() can be used to invoke immediate parent class constructor.
Final Keyword :-
Final Variable :- If you make any variable as final, you cannot change the value of final variable(It will be constant).
Final Method : (To Stop overriding )If you make any method as final, you cannot override it.
Final Class : (To Stop Inheritance) If you make any class as final, you cannot extend it.
Is final method inherited? : Yes, final method is inherited but you cannot override it.
Can we initialize blank final variable: Only in constructor
Access Modifiers in Java
- Private: The access level of a private modifier is only within the class. It cannot be accessed from outside the class.
- Default: The access level of a default modifier is only within the package. It cannot be accessed from outside the package. If you do not specify any access level, it will be the default.
- Protected: The access level of a protected modifier is within the package and outside the package through child class. If you do not make the child class, it cannot be accessed from outside the package.
- Public: The access level of a public modifier is everywhere. It can be accessed from within the class, outside the class, within the package and outside the package.
If you make any class constructor private, you cannot create the instance of that class from outside the class.
Exception
Exception : Parent class : Throwable
Throwable - > Error & Exception
Checked Exception : Checked at compile time.The classes which directly inherit Throwable class except RuntimeException and Error are known as checked exceptions e.g. IOException, SQLException etc
Unchecked Exception : Classes which extends RuntimeException are known as unchecked exceptions
Eg. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc.
- void m()throws ArithmeticException{
- throw new ArithmeticException("sorry");
- }
Comments
Post a Comment