Thursday, 19 December 2013

ArrayList Without Duplicate java

Hi this is a  sample example of java how we can write a ArrayList and eliminate duplicates in it .Below is the code snippet that gives you an idea on how to eliminate duplicates from arraylist

import java.util.ArrayList;
public class ArrayListWithoutDuplicates {
     
 public static void main(String args[]){

  ArrayList<String> original = new ArrayList<String>();
  ArrayList<String> eliminateDuplicate= new ArrayList<String>();

  original.add("Naveen");
  original.add("Rajesh");
  original.add("Pradeep");
  original.add("Rajesh");


  for (String dupWord : original) {
      if (!eliminateDuplicate.contains(dupWord)) {
          eliminateDuplicate.add(dupWord);
      }
  }
  System.out.println(eliminateDuplicate);
 }

}
Output : [Naveen, Rajesh, Pradeep]

Wednesday, 4 December 2013

Collections Sort a HashMap Based On Values Java

Hi Viewers  I'm going to give you an example to sort an unsorted HashMap using Comparator in java.
Comparator can be used to sort the classes that implement sorted maps and sorted sets .As per the java docs we can pass Comparator ,map or sorted map . So here in below code snippet we pass object of class to TreeMap's constructor in which we implemented Comparator . It then creates a new TreeMap which is sorted as per Comparator ans displays the desired result.
Below is the code snippet that gives a clear example of how to sort HashMap based on its value objects
  
import java.util.Comparator;
import java.util.HashMap;
import java.util.TreeMap;

public class SortHashMap {
 public SortHashMap() {
  
 }
 public static void main(String args[]){
  HashMap<String,String> mp=new HashMap<String,String>();
  mp.put("1","America");
  mp.put("2", "Florida");
  mp.put("3", "England");
  mp.put("4", "Dallas");
  CheckComparator cc=new CheckComparator(mp);
  TreeMap<String,String> tm=new TreeMap<String,String>(cc);
  tm.putAll(mp);
  System.out.println(tm);
 
 }
}
class CheckComparator implements Comparator{
 private HashMap<String, String> mp;

 public CheckComparator(HashMap<String, String> mp) {
  this.mp=mp;
 }

 @Override
 public int compare(Object o1, Object o2) {
  if(mp.get(o1).toString().compareTo(mp.get(o2).toString())>0){
   return 1;  
  }else if(mp.get(o1).toString().compareTo(mp.get(o2).toString()<0){
  return -1;
  }else{
   return 0;
  }
}
Output:
{1=America, 4=Dallas, 3=England, 2=Florida}

Collections Comparator Interface with example java

Comparator Interface with example:

Comparator Interface belongs to java.utill package. Comparator is also used to compare the two objects by overriding its inherited int compare(Object o1,Object o2) method.Here I'm going to give example to understand well about Comparator Interface in java.
Create an Employee bean class Employee.java :
 public class Employee{
 private int eid;
 private String ename;
 private int age;
 
 public Employee(int eid,String ename,int age) {
this.eid=eid;
this.ename=ename;
this.age=age;
 }
 public int getEid() {
  return eid;
 }
 public void setEid(int eid) {
  this.eid = eid;
 }
 public String getEname() {
  return ename;
 }
 public void setEname(String ename) {
  this.ename = ename;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 
}
Now create a class EmployeeAgeComparator.java to compare and sort based on ages as below
public class EmplyeeAgeComparator implements Comparator<Employee>
{

 @Override
 public int compare(Employee o1, Employee o2) {
  if(o1.getAge()>o2.getAge())
   return 1;
  if(o1.getAge()<o2.getAge())
   return -1;
  if(o1.getAge()==o2.getAge())
   return 0;
  return 0;
 }

}
Create an employee comparator class and call Collections.sort() method to sort values based on age and print values
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.interview.comparatortest.Employee;
public  class EmployeeComparator {
 public static void main(String args[]){
  List<Employee> emp=new ArrayList<Employee>();
  emp.add(new Employee(1,"Alfred", 23));
  emp.add(new Employee(2,"Ion Bell", 21));
  emp.add(new Employee(3,"Clarke",30));
  Collections.sort(emp,new EmplyeeAgeComparator());
  for(Employee test:emp){
   System.out.println("age=="+test.getAge()+"\tname=="+test.getEname()+"\teid=="+test.getEid());
  }

 }

}
Output :
age==21 name==Ion Bell eid==2
age==23 name==Alfred eid==1
age==30 name==Clarke eid==3

Tuesday, 3 December 2013

Comparable Interface with Example Java

Comparable Interface with Example:

Hi in this post i would like to give you an explanation regarding comparable interface example in java.
Comparable is an interface which belong to java.lang package .Comparable interface helps in ordering of the objects for a class which implements this interface.Here I'm giving you a scenario which explains the comaparable


Q): Using ArrayList take the values of an employee like his name,age,eid and i wanted to get the list to be printed in sorted order as per their names.

Solution: First i created a Employee bean class having getters and setters in it as below.
and implement the Employee class with Comparable interface with Employee as generic type .Implementing comparable will insist you to override the int compareTo(Employee o) method in Employee class

Below is the code snippet Employee.java:
  public class Employee implements Comparable<Employee>{
 private int eid;
 private String ename;
 private int age;
 
 public Employee() {
 }
 public int getEid() {
  return eid;
 }
 public void setEid(int eid) {
  this.eid = eid;
 }
 public String getEname() {
  return ename;
 }
 public void setEname(String ename) {
  this.ename = ename;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 @Override
 public int compareTo(Employee o) {
       
  if(this.ename.compareTo(o.ename)>0)
      
   return 1;
  else if(this.ename.compareTo(o.ename)<0)
   return -1;
  else
   return 0;
 }
Now create a SortEmployee class  where we get the final result of sorted employee details with names. Below is the code snippet SortEmployee.java:  
import java.util.ArrayList;

import java.util.Collections;

public class SortEmployee {
 public static void main(String args[]){
 
     Employee naveen=new Employee();
  naveen.setAge(24);
  naveen.setEid(1);
  naveen.setEname("naveen");
  Employee vivek=new Employee();
  vivek.setAge(32);
  vivek.setEid(2);
  vivek.setEname("vivek");
  Employee check=new Employee();
  check.setAge(44);
  check.setEid(3);
  check.setEname("piyush");
 ArrayList<Employee> emps=new ArrayList<Employee>();

 emps.add(naveen);
 emps.add(vivek);
 emps.add(check);

  Collections.sort(emps);
  
  for(Employee emp1:emps){
   System.out.println(emp1.getEname()+">>>>"+emp1.getAge()+">>>>"+emp1.getEid());
  }
 }
}

Now if we run the SortEmployee.java we get the desired output as below:
naveen>>>>24>>>>1
piyush>>>>44>>>>>3
vivek>>>>32>>>>2

To sort the employee details based on age change compareTo method as below and re run the program :
 @Override
 public int compareTo(Employee o) {
  
  if(this.age>o.age)
   return 1;
  else if(this.age<o.age)
   return -1;
  else
   return 0;
 }
output:
naveen>>>>24>>>>1
vivek>>>>32>>>>2
piyush>>>>44>>>>3

Sunday, 1 December 2013

UBUNTU- Errors Encountered While Processing google-chrome-stable

Errors Encountered While Processing google-chrome-stable

Today when i tried installing google-chrome in my ubuntu machine ,i was stuck with this error in my terminal .After searching for some thing to make it work out in i found the below solution for this

Step 1: Do a sudo apt-get install -f in your terminal It will install some dependencies in your machine.

Step2 : now install your chrome .deb file using command 
                  sudo dpkg -i google-chrome-stable_current_amd64.deb

and here their it ends with successful installation of chrome browser in your machine


Thanks and Reagrds
Naveen