Monday, January 12, 2015

Get min and Get Max in Java



How to get min element from the given list

package com.java2novice.collections;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MyMinNumber
{
public static void main(String a[])
{
        List li = new ArrayList();
        li.add(23);
        li.add(44);
        li.add(12);
        li.add(45);
        li.add(2);
        li.add(16);
        System.out.println("Minimum element from the list: "+Collections.min(li));
    }
}

How to get max element of a list of user defined objects

package com.java2novice.collections;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MyMaxUDOComparable
{
 public static void main(String a[])
{
        List emps = new ArrayList();
        emps.add(new Empl(10, "Raghu", 25000));
        emps.add(new Empl(120, "Krish", 45000));
        emps.add(new Empl(210, "John", 14000));
        emps.add(new Empl(150, "Kishore", 24000));
        Empl maxSal = Collections.max(emps);
        System.out.println("Employee with max salary: "+maxSal);
    }
}
class Empl implements Comparable
{
    private int id;
    private String name;
    private Integer salary;
 public Empl(int id, String name, Integer sal)
{
        this.id = id;
        this.name = name;
        this.salary = sal;
  }
 public int getId()
 {
        return id;
    }
    public void setId(int id)
   {
        this.id = id;
    }
    public String getName()
   {
        return name;
    }
    public void setName(String name)
   {
        this.name = name;
    }
    public Integer getSalary()
   {
        return salary;
    }
    public void setSalary(Integer salary)
   {
        this.salary = salary;
    }
    public int compareTo(Empl emp)
   {
         return this.salary.compareTo(emp.getSalary());
    }
    public String toString()
   {
        return id+"  "+name+"   "+salary;
    }
}

How to get min element of a list of user defined objects

package com.java2novice.collections;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MyEmpMinSalary
public static void main(String a[])
{
        List emps = new ArrayList();
        emps.add(new Empl(10, "Raghu", 25000));
        emps.add(new Empl(120, "Krish", 45000));
        emps.add(new Empl(210, "John", 14000));
        emps.add(new Empl(150, "Kishore", 24000));
        Empl minSal = Collections.min(emps);
        System.out.println("Employee with min salary: "+minSal);
    }
}
class Empl implements Comparable
{
    private int id;
    private String name;
    private Integer salary;
 public Empl(int id, String name, Integer sal)
{
        this.id = id;
        this.name = name;
        this.salary = sal;
  }
  public int getId()
  {
        return id;
   }
   public void setId(int id)
   {
        this.id = id;
    }
    public String getName()
    {
        return name;
    }
    public void setName(String name)
   {
        this.name = name;
    }
    public Integer getSalary()
   {
        return salary;
    }
    public void setSalary(Integer salary)
    {
        this.salary = salary;
    }
    public int compareTo(Empl emp)
    {
        return this.salary.compareTo(emp.getSalary());
    }
    public String toString()
   {
        return id+"  "+name+"   "+salary;
    }
}

No comments:

Post a Comment