
Hi sir,I have requirement i.e finding maximum value in userdefined objects and display that object only and store another collecton object.For example i taken for employe class and write for below sample code ,in this code i can findout maximum value but can't store that object into another object.Please help me how to write logic .
My sample code : package com.naresh;
import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set;
public class EmpList { public static Set getList(List<Employe> list){ Employe emp=null;
Set s = new HashSet<Employe>();
for(Employe e:list){ //outer
for(Employe e1:list){ //inner
if(e.getEno()==e1.getEno()&&e.getName().equals(e1.getName())) {
if(e.getSalary()>e1.getSalary()) {
s.add(e);
}
}
}
}
return s;
}
public static void main(String[] args) { List<Employe> list = new ArrayList<Employe>(); list.add(new Employe(1,"naresh", 200)); list.add(new Employe(1, "naresh", 400)); list.add(new Employe(1, "naresh", 500)); list.add(new Employe(1, "naresh", 900)); list.add(new Employe(2, "kumar", 600)); list.add(new Employe(2, "kumar", 900));
System.out.println(EmpList.getList(list));
} }
My Excepted output :1 naresh 900 2 kumar 900
My Employe package com.naresh;
import java.util.Comparator;
public class Employe{
int eno;
String name;
double salary;
public int getEno() {
return eno;
}
public void setEno(int eno) {
this.eno = eno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public Employe(int eno , String name , double salary) {
this.eno = eno;
this.name = name;
this.salary = salary;
}
public String toString(){
return eno+" "+name+" "+salary;
}
}

Here is a code that calculates the highest salary from the list of employees.
import java.io.*;
import java.util.*;
class Employee{
public int salary;
public String name;
public String address;
public static int count = 0;
public Employee(){}
public Employee(String name,String address,int salary) {
super();
this.name = name;
this.address=address;
this.salary = salary;
count++;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public int getSalary() {
return salary;
}
}
class SalaryComparator implements Comparator{
public int compare(Object emp1, Object emp2){
int sal1 = ((Employee)emp1).getSalary();
int sal2 = ((Employee)emp2).getSalary();
if(sal1 > sal2)
return 1;
else if(sal1 < sal2)
return -1;
else
return 0;
}
}
public class EmployeeSalary {
public static void main(String[] args) throws Exception {
List<Employee> list = new ArrayList<Employee>();
list.add(new Employee("A","Delhi",10000));
list.add(new Employee("B","Mumbai",20000));
list.add(new Employee("C","Chennai",15000));
list.add(new Employee("D","Kolkata",12000));
System.out.println(" ");
int count=0;
int salary=0;
Collections.sort(list,new SalaryComparator());
for(Employee data: list){
salary=data.getSalary();
}
System.out.println("Highest salary is: "+salary);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.