7 ways of reading HashMap in JAVA

package me.equals;

import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class HashmapReading {
public static void main(String[] args) {
Map<String, String> mapActors=new HashMap<String, String>();
mapActors.put(“tolly2”, “PawanKalyan”);mapActors.put(“tolly2”, “P1”);mapActors.put(“tolly1”, “Chiranjeevi”);
mapActors.put(“tolly3”, “Venkatesh”);mapActors.put(“tolly4”, “NagaChaitanya”);mapActors.put(“tolly5”, “Nani”);
mapActors.put(“tolly7”, “PawanKalyan”);mapActors.put(“tolly8”, null);mapActors.put(“tolly9”, null);

//Keys cannot be duplicate
//value duplicatews are allwoed
//null allowed

System.out.println(“———-forEach()——————–“);
for(String string:mapActors.keySet()){
System.out.println(string+”——-“+mapActors.get(string));
}

System.out.println(“———-keySet-Iterator——————–“);
Set<String> strings=mapActors.keySet();
Iterator<String> iterator=strings.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next()+”——–“+mapActors.get(iterator.next()));
}

System.out.println(“———-EntrySet Foreach()———————“);
Set<Entry<String, String>> strings2=mapActors.entrySet();
for(Entry<String, String> entry:strings2){
System.out.println(entry.getKey()+”——–“+entry.getValue());
}

System.out.println(“———-EntrySet-Iterator——————–“);
Set<Entry<String, String>> strings3=mapActors.entrySet();
Iterator<Entry<String, String>> iterator2=strings3.iterator();
while(iterator2.hasNext()){
System.out.println(iterator2.next());
}

System.out.println(“———-Enumeration——————–“);
Enumeration keys = Collections.enumeration(mapActors.keySet());
while( keys.hasMoreElements() ) {
Object key = keys.nextElement();
Object value = mapActors.get(key);
System.out.println(key+”——–“+value);
}

System.out.println(“———-Stream ——————–“);
mapActors.keySet().stream().forEach(key ->
System.out.println(“key: ” + key + ” value: ” + mapActors.get(key)));

System.out.println(“——–direct SOP———–“);
System.out.println(mapActors.values());

}
}

 

———-forEach()——————–
tolly1——-Chiranjeevi
tolly3——-Venkatesh
tolly2——-P1
tolly5——-Nani
tolly4——-NagaChaitanya
tolly7——-PawanKalyan
tolly9——-null
tolly8——-null
———-keySet-Iterator——————–
tolly1——–Venkatesh
tolly2——–Nani
tolly4——–PawanKalyan
tolly9——–null
———-EntrySet Foreach()———————
tolly1——–Chiranjeevi
tolly3——–Venkatesh
tolly2——–P1
tolly5——–Nani
tolly4——–NagaChaitanya
tolly7——–PawanKalyan
tolly9——–null
tolly8——–null
———-EntrySet-Iterator——————–
tolly1=Chiranjeevi
tolly3=Venkatesh
tolly2=P1
tolly5=Nani
tolly4=NagaChaitanya
tolly7=PawanKalyan
tolly9=null
tolly8=null
———-Enumeration——————–
tolly1——–Chiranjeevi
tolly3——–Venkatesh
tolly2——–P1
tolly5——–Nani
tolly4——–NagaChaitanya
tolly7——–PawanKalyan
tolly9——–null
tolly8——–null
———-Stream ——————–
key: tolly1 value: Chiranjeevi
key: tolly3 value: Venkatesh
key: tolly2 value: P1
key: tolly5 value: Nani
key: tolly4 value: NagaChaitanya
key: tolly7 value: PawanKalyan
key: tolly9 value: null
key: tolly8 value: null
——–direct SOP———–
[Chiranjeevi, Venkatesh, P1, Nani, NagaChaitanya, PawanKalyan, null, null]

equal() in JAVA and its equivalence relation on non-null object references

Refer

 

package com.me;

public class Object_Equal_Method {
public static void main(String[] args) {
//The equals method implements an equivalence relation on non-null object references:
//for objects it will chek by == operator
//for string literals n wrappers it checks like equal() content comparision i.e Integer etc….
//doesnt appplicable for primitiveds like int,char
Info info=new Info(7563399125L, “John”, “NewJersy”);
Info info2=new Info(7563399125L, “John”, “NewJersy”);

String aString=”India”,bString=”India”;
String cString=new String(“India”),dString=new String(“India”);
Integer a=123,b=123;
//Relfexive a==a true
System.out.println(info.equals(info));
System.out.println(“——————————–“);
//Symmetric a==b then b==a
System.out.println(info.equals(info2));
System.out.println(info2.equals(info));
System.out.println(info.hashCode());
System.out.println(info2.hashCode());
//here hashcode is diffetent
System.out.println(“——————————–“);
System.out.println(aString.equals(bString));
System.out.println(bString.equals(aString));
System.out.println(a.equals(b));
System.out.println(b.equals(a));
System.out.println(aString.hashCode());
System.out.println(bString.hashCode());
System.out.println(a.hashCode());
System.out.println(b.hashCode());
System.out.println(“——————————–“);
System.out.println(aString.equals(cString));
System.out.println(bString.equals(dString));
System.out.println(cString.equals(dString));
System.out.println(cString.hashCode());
System.out.println(dString.hashCode());
System.out.println(“——————————–“);

//Transitive a==b b==c then c==a
System.out.println(aString.equals(bString));
System.out.println(bString.equals(aString));
System.out.println(cString.equals(aString));
System.out.println(“——————————–“);
System.out.println(aString.equals(null));
}
}

class Info{
private long phoneNo;
private String address,name;
public Info(Long phoneno,String name,String Address){
this.address=Address;
this.name=name;
this.phoneNo=phoneNo;
}
}

 

 

true
——————————–
false
false
366712642
1829164700
——————————–
true
true
true
true
70793495
70793495
123
123
——————————–
true
true
true
70793495
70793495
——————————–
true
true
true
——————————–
false

Encapsulation in JAVA

Simply Integrating data and methods together as shown below.

 

public class Encapsulation
{
//It violates Encapsulation..it should be private
public String string;
private int no;

public static void main(String[] args)
{
Encapsulation encapsulation=new Encapsulation();
encapsulation.no=123;
encapsulation.string=”JohnCena”;

System.out.println(encapsulation.getNo()+”———-“+encapsulation.getString());
}

public String getString() {
return string;
}

public void setString(String string) {
this.string = string;
}

public int getNo() {
return no;
}

public void setNo(int no) {
this.no = no;
}
}

 

123———-JohnCena

 

Var-args in JAVA

  • When to use:

Varargs can be used when we are unsure about the number of arguments to be passed in a method. It creates an array of parameters of unspecified length in the background and such a parameter can be treated as an array in runtime.

If we have a method which is overloaded to accept different number of parameters, then instead of overloading the method different times, we can simply use varargs concept.

  • Program illustrates Var-args functionality:

public class Varargs {

//Introduced java 5
//3 dots i.e String…a
//int…b
static void display(String…strs)
{
for(String string:strs)
{
System.out.println(string);
}
}

//we can’t declare 2 xct ()’s
/*static void display(String…strs)
{
for(String string:strs)
{
System.out.println(string);
}
}*/

static void display(int…intrs)
{
for(Integer o:intrs)
{
System.out.println(o);
}
}

static void display(int a,float b,String…strs)
{
System.out.println(a);
System.out.println(b);
for(String string:strs)
{
System.out.println(string);
}
}

/*//args param shoukld be last param of ()
static void display(String…strs,int b)
{
for(String string:strs)
{
System.out.println(string);
}
}*/

/*//CE
static void display(int…a,String…strs)
{
for(String string:strs)
{
System.out.println(string);
}
}
*/

public static void main(String[] args)
{
display(“Keria”,”Jack”,”Nolan”,”Depp”,”Somer”);
display(1213,3245,325,325);
display(123, 52.79f, “You are my idiot”,”Still living”,”ooops”);
}
}

 

 

Keria
Jack
Nolan
Depp
Somer
1213
3245
325
325
123
52.79
You are my idiot
Still living
ooops

 

 

Types of inheritance in JAVA

Go through Once……………….

 

Source code: Explains all java supported inheritance types i.e Single,Multi level,Hierarchical,Hybrid,Multiple(partially supported by JAVA) .

 

 

package me.inheritence;
class Ra{
public Ra(){
System.out.println(“Ra()”);
}
}
class Rb extends Ra{
public Rb(){
System.out.println(“Rb()”);
}
}
//
//a–>b single inheritence
class Rc extends Rb{
public Rc(){
System.out.println(“Rc()”);
}
}

class Rd extends Rc{
public Rd(){
System.out.println(“Rd()”);
}
}
//
//a-.—>b—->.c——.>d multi level

class Re {
public Re(){
System.out.println(“Re()”);
}
}

class Rf extends Re{
public Rf(){
System.out.println(“Rf()”);
}
}
class Rg extends Re{
public Rg(){
System.out.println(“Rg()”);
}
}
class Rh extends Re{
public Rh(){
System.out.println(“Rh()”);
}
}

//e—>f
//e—>g
//e—–>h heirarchical inheritence

//through clasees hybrid inheritence is not posiible
//
class Ri{
public Ri(){
System.out.println(“Ri()”);
}
}

class Rj extends Ri{
public Rj(){
System.out.println(“Rj()”);
}
}
class Rk extends Ri{
public Rk(){
System.out.println(“Rk()”);
}
}
class Rl extends Ri{
public Rl(){
System.out.println(“Rl()”);
}
}

/*class Rm extends Rj,Rk,Rl{
public Rm(){
System.out.println(“Rm()”);
}
}

//Exception in thread “main” java.lang.Error: Unresolved compilation problem:
// at me.inheritence.Rajini.main(Rajini.java:84)
*
*/
//Through interfaces hybrid inheritence is possible
//
interface n{
public void nMethod();
}

interface o extends n{
public void oMethod();
}

interface p extends n{
public void pMethod();
}

class Rq implements n,o,p{
@Override
public void nMethod() {
System.out.println(“n-Rq()”);
}
@Override
public void pMethod() {
System.out.println(“p-Rq()”);
}
@Override
public void oMethod() {
System.out.println(“p-Ro()”);
}
}

//o,p——->n
//rq——>n,o,p
//through classes Multiple inheritence is not posiible
//
class Rr{
public Rr(){
System.out.println(“Rr()”);
}
}

class Rs{
public Rs(){
System.out.println(“Rs()”);
}
}

/*class Rw extends Rr,Rs{
public Rw(){
System.out.println(“Rw()”);
}
}

Exception in thread “main” java.lang.Error: Unresolved compilation problem:

at me.inheritence.Rajini.main(Rajini.java:140)
*/
//Through interfaces multiple iheritence is possible
//
interface Rx{
public void RxMethod();
}

interface Ry{
public void RyMethod();
}

class Rz implements Rx,Ry{
@Override
public void RyMethod() {
System.out.println(“Rz-Ry() imple”);
}

@Override
public void RxMethod() {
System.out.println(“Rz-Rx() imple”);
}
}

public class Rajini {
public static void main(String[] args) {
Rb rb=new Rb();
System.out.println(“——-single———“);
Rd rs=new Rd();
System.out.println(“———multi level——“);
Rf rf=new Rf();
Rg rg=new Rg();
Rh rh=new Rh();
System.out.println(“———heirarchical——-“);
System.out.println(“through classes hybrid inheritence is not possible ,through interfaces”
+ “it is possible”);

Rq rq=new Rq();
rq.oMethod();
rq.pMethod();
rq.nMethod();
System.out.println(“————-Hybrid inheritence————“);
System.out.println(“through classes multiple inheritence is not possible ,through interfaces”
+ “it is possible”);

Rz rz=new Rz();
rz.RxMethod();
rz.RyMethod();
System.out.println(“————Multi level Inheritence———–“);

}
}

 

output:

Ra()
Rb()
——-single———
Ra()
Rb()
Rc()
Rd()
———multi level——
Re()
Rf()
Re()
Rg()
Re()
Rh()
———heirarchical——-
through classes hybrid inheritence is not possible ,through interfacesit is possible
p-Ro()
p-Rq()
n-Rq()
————-Hybrid inheritence————
through classes multiple inheritence is not possible ,through interfacesit is possible
Rz-Rx() imple
Rz-Ry() imple
————Multi level Inheritence———–

Exception with method overriding in JAVA

Rule–1:
If Superclass doesn’t throws any Exception type then Subclass should throw
only Uncatched Exceptions and shoudn’t throw checked Exceptions.

Unchecked :
ArrayIndexOutOfBoundsException
ClassCastException
IllegalArgumentException
IllegalStateException
NullPointerException
NumberFormatException
AssertionError
ExceptionInInitializerError
StackOverflowError
NoClassDefFoundError

Checked:
Exception
IOException
FileNotFoundException
ParseException
ClassNotFoundException
CloneNotSupportedException
InstantiationException
InterruptedException
NoSuchMethodException
NoSuchFieldException

 

The unchecked exceptions classes are the class RuntimeException and its subclasses, and the class Error and its subclasses.
All other exception classes are checked exception classes.

 

———————————————————
package com.me.me;
class B{
void M1() {
System.out.println(“ghgh”);
}
}
public class Va extends B{
public static void main(String[] args) {
B a=new Va();
a.M1();
}
public void M1() throws Exception{
System.out.println(“ghgh”);
}
// If the superclass method does not declare an exception,
//subclass overridden method cannot declare the checked exception.
}

Exception in thread “main” java.lang.Error: Unresolved compilation problem:
Exception Exception is not compatible with throws clause in B.M1()

 

——————————————————————–

at com.me.me.Va.M1(Va.java:12)
at com.me.me.Va.main(Va.java:10)
package com.me.me;
class B1{
void M1() {
System.out.println(“ghgh”);
}
}
public class Vaa extends B1{
public static void main(String[] args) {
B1 a=new Vaa();
a.M1();
}
public void M1() throws ArithmeticException{
System.out.println(“ghgh”);
}
// If the superclass method does not declare an exception,
//subclass overridden method cannot declare the checked exception
//but can declare unchecked exception.
}

ghgh

—————————————————————-
If the superclass method declares an exception,
subclass overridden method can declare same,
subclass exception or no exception but cannot declare parent exception.
package com.me.me;
class parent{
void Method() throws ArithmeticException{
System.out.println(“Parent-()”);
}
}
public class Va3 extends parent {
public static void main(String[] args) throws Exception {
parent p=new Va3();
p.Method();
}
public void Method() throws Exception{
System.out.println(“child-()”);
}
}

Exception in thread “main” java.lang.Error: Unresolved compilation problem:
Exception Exception is not compatible with throws clause in parent.Method()

at com.me.me.Va3.Method(Va3.java:12)
at com.me.me.Va3.main(Va3.java:10)

———————————————————————–
package com.me.me;
class parent{
void Method() throws Exception{
System.out.println(“Parent-()”);
}
}
public class Va3 extends parent {
public static void main(String[] args) throws Exception {
parent p=new Va3();
p.Method();
}
public void Method() throws ArithmeticException{
System.out.println(“child-()”);
}
}
child-()

———————————————————————
package com.me.me;
class parent{
void Method() {
System.out.println(“Parent-()”);
}
}
public class Va3 extends parent {
public static void main(String[] args) throws Exception {
parent p=new Va3();
p.Method();
}
public void Method() throws ArithmeticException{
System.out.println(“child-()”);
}
}

child-()

———————————————————————-