Monday, 3 September 2018


                                                 How to remove duplicates from list in java

package govind;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;

public class RemoveDuplicate {

public static void main(String[] args) {
ArrayList<String> al=new ArrayList<>();
al.add("govind");
al.add("ballabh");
al.add("khan");
al.add("java");
al.add("technology");
al.add("govind");
al.add("java");
System.out.println(al);
System.out.println("Remove duplicate using HashSet but order does not maintain ");
HashSet< String> hs=new HashSet<>(al);
System.out.println(hs);
System.out.println("Remove duplicate using LinkedHashSet but order maintain ");
LinkedHashSet< String> lhs=new LinkedHashSet<>(al);
System.out.println(lhs);
System.out.println("Remove duplicate using contains method");
ArrayList<String> ul=new ArrayList<>();
for(String t:al)
{
if(!ul.contains(t))
{
ul.add(t);
}
}

Iterator<String> it = ul.iterator();
while (it.hasNext()) {
String s= (String) it.next();
System.out.println(s);

}





}

}

========================================================================================================================================
o/p:--------------------------------------
[govind, ballabh, khan, java, technology, govind, java]
Remove duplicate using HashSet but order does not maintain
[java, ballabh, govind, khan, technology]
Remove duplicate using LinkedHashSet but order maintain
[govind, ballabh, khan, java, technology]
Remove duplicate using contains method
govind
ballabh
khan
java
technology

Saturday, 1 September 2018


                                               HashMap and WeakHashMap Concept in java


Java.util.HashMap class is a Hashing based implementation. In HashMap, we have a key and a value pair.
Even though the object is specified as key in hashmap, it does not have any reference and it is not
eligible for garbage collection if it is associated with HashMap i.e. HashMap dominates over Garbage Collector.
============================================================================================================================================
WeakHashMap is an implementation of the Map interface. WeakHashMap is almost same as HashMap except in case of WeakHashMap, if object is specified as key doesn’t contain any references-
it is eligible for garbage collection even though it is associated with WeakHashMap. i.e Garbage Collector dominates over WeakHashMap



//HashMap Demonstration

package govind;

import java.util.HashMap;

public class HashMapDemo {

public static void main(String[] args) throws InterruptedException {
HashMap  hmap=new HashMap();
NamePrint np=new NamePrint();
hmap.put(np, "JavaTechnology");
System.out.println(hmap);
np=null;
System.gc();
Thread.sleep(4000);
System.out.println(hmap);

}

}



===========================================================================================================================================

package govind;

public class NamePrint {

public String toString()
{
return "Govind Ballabh Khan";
}

public void finalize()
{
System.out.println("Finalize Method call");
}

}


===========================================================================================================================================
o/p:---------------------------
{Govind Ballabh Khan=JavaTechnology}
{Govind Ballabh Khan=JavaTechnology}


===========================================================================================================================================


// WeakHashMap Demonstration


package govind;

import java.util.WeakHashMap;

public class WeakHashMapDemo {

public static void main(String[] args) throws InterruptedException {
WeakHashMap whm=new WeakHashMap<>();
NamePrint np=new NamePrint();
    whm.put(np, "JavaTechnology");
    System.out.println(whm);
    np=null;
    System.gc();
    Thread.sleep(4000);
    System.out.println(whm);
   
   
}

}
===========================================================================================================================================
package govind;

public class NamePrint {

public String toString()
{
return "Govind Ballabh Khan";
}

public void finalize()
{
System.out.println("Finalize Method call");
}

}


============================================================================================================================================

o/p:---------------------------
{Govind Ballabh Khan=JavaTechnology}
Finalize Method call
{}

Hashmap and IdentityHashMap Example.........

package govind; import java.util.HashMap; import java.util.IdentityHashMap; public class Test { public static void main(String[] args) { Integer i1=new Integer(10); Integer i2=new Integer(10); System.out.println(i1.equals(i2));//true System.out.println(i1==i2);//false HashMap m=new HashMap();//internally equals method use m.put(i1, "govind"); m.put(i2, "khan"); System.out.println(m); IdentityHashMap i=new IdentityHashMap<>();//internally == method use i.put(i1, "govind"); i.put(i2, "khan"); System.out.println(i); } }

//o/p:-------------------------------------
true
false
{10=khan}
{10=govind, 10=khan}

=================================================================================
package govind; import java.util.HashMap; import java.util.IdentityHashMap; public class IdentityHashmapDemo { public static void main(String[] args) { HashMap hmap=new HashMap<>(); IdentityHashMap imap=new IdentityHashMap<>(); hmap.put(new String("Mobile"), "samsung"); hmap.put(new String("Laptop"), "Dell"); hmap.put(new String("Mobile"), "samsung"); System.out.println(hmap); imap.put(new String("Mobile"), "samsung"); imap.put(new String("Laptop"), "Dell"); imap.put(new String("Mobile"), "samsung"); System.out.println(imap); } }


o/p..........................................................................
{Laptop=Dell, Mobile=samsung} {Mobile=samsung, Mobile=samsung, Laptop=Dell}

Thursday, 30 August 2018

                                   Internal working of HashMap through Program in java

package govind;

public class ProductClass {

String ppname;
float pprice;


public ProductClass()
{

}
public ProductClass(String ppname, float pprice) {
super();
this.ppname = ppname;
this.pprice = pprice;
}
@Override
public int hashCode() {

System.out.println("Ïnside hashcode method");
final int prime = 31;
int result = 1;
result = prime * result + ((ppname == null) ? 0 : ppname.hashCode());
result = prime * result + Float.floatToIntBits(pprice);
return result;
}
@Override
public boolean equals(Object obj) {
System.out.println("Inside equals method");
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ProductClass other = (ProductClass) obj;
if (ppname == null) {
if (other.ppname != null)
return false;
} else if (!ppname.equals(other.ppname))
return false;
if (Float.floatToIntBits(pprice) != Float.floatToIntBits(other.pprice))
return false;
return true;
}




}
=======================================================================================================================================

package govind;

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

public class ProductClassMain {

public static void main(String[] args) {
Map< ProductClass, String> map=new HashMap<>();
map.put(new ProductClass("Nokia", 45000.00f),"Mobile");
map.put(new ProductClass("Dell", 50000.00f),"Laptop");
Set<Entry<ProductClass, String>> set = map.entrySet();
Iterator<Entry<ProductClass, String>> it = set.iterator();
while (it.hasNext()) {
Entry<ProductClass, String> kk = it.next();
ProductClass p = kk.getKey();
System.out.println(p.ppname+" "+p.pprice);
System.out.println(kk.getValue());

}

System.out.println("Try to Insert Duplicate key");
map.put(new ProductClass("Nokia", 45000.00f),"Mobile");

}

}


==========================================================================================================================================

o/p:-----------------------------------


Ïnside hashcode method
Ïnside hashcode method
Dell 50000.0
Laptop
Nokia 45000.0
Mobile
Try to Insert Duplicate key
Ïnside hashcode method
Inside equals method



                                                            equals method example  in java
package govind;
public class Product {
private String productName;
private float productPrice;
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public float getProductPrice() {
return productPrice;
}
public void setProductPrice(float productPrice) {
this.productPrice = productPrice;
}

}
=============================================================================================================================
package govind;
public class ProductMain {
public static void main(String[] args) {
Product p1=new Product();
p1.setProductName("Laptop");
p1.setProductPrice(45000.00f);
Product p2=new Product();
p2.setProductName("Laptop");
p2.setProductPrice(45000.00f);
System.out.println(p1.equals(p2));
}
}
===================================================================================================================================
o/p:---------------------------
false
package govind;
public class Product {
private String productName;
private float productPrice;
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public float getProductPrice() {
return productPrice;
}
public void setProductPrice(float productPrice) {
this.productPrice = productPrice;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Product other = (Product) obj;
if (productName == null) {
if (other.productName != null)
return false;
} else if (!productName.equals(other.productName))
return false;
if (Float.floatToIntBits(productPrice) != Float.floatToIntBits(other.productPrice))
return false;
return true;
}
}
========================================================================================================================================================
package govind;
public class ProductMain {
public static void main(String[] args) {
Product p1=new Product();
p1.setProductName("Laptop");
p1.setProductPrice(45000.00f);
Product p2=new Product();
p2.setProductName("Laptop");
p2.setProductPrice(45000.00f);
System.out.println(p1.equals(p2));
}
}
================================================================================================================================================================
o/p:-----------------------
true








































Wednesday, 22 August 2018

3 Ways to check if String is null or empty in Java

if(stirng != null && !string.isEmpty())
{
 System.out.println("String is not null and not empty");
}

====================================================================

if(stirng != null && string.length() > 0)
{
   System.out.println("String is not null and not empty");
}


======================================================================
if(stirng != null && string.trim().length() > 0)
{
System.out.println("String is not null and not empty");
}


Saturday, 18 August 2018

package govind;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;

public class Java8DateTimeExample {

public static void LocalDateTimeApiJava8Concept()
{
 
    // the current date
    LocalDate date = LocalDate.now();
    System.out.println("The current date is "+
                        date);
 
 
    // the current time
    LocalTime time = LocalTime.now();
    System.out.println("The current time is "+
                        time);
     
 
    // will give us the current time and date
    LocalDateTime current = LocalDateTime.now();
    System.out.println("The current date and time : "+
                        current);
 
    // to print in a particular format
    DateTimeFormatter format =
      DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss"); 
      String formatedDateTime = current.format(format); 
      System.out.println("In foramatted manner "+
                        formatedDateTime);
 
 
    // printing months days and seconds
    Month month = current.getMonth();
    int day = current.getDayOfMonth();
    int seconds = current.getSecond();
    System.out.println("Month : "+month+" day : "+
                        day+" seconds : "+seconds);
 
    // printing some specified date
    LocalDate date2 = LocalDate.of(1950,1,26);
    System.out.println("The repulic day :"+date2);
 
    // printing date with current time.
    LocalDateTime specificDate =
        current.withDayOfMonth(24).withYear(2016);

    System.out.println("Specfic date with "+
                       "current time : "+specificDate);
}

   
    public static void main(String[] args)
    {
        LocalDateTimeApiJava8Concept();
    }
}