How to find maximum value in ArrayList
**Sir i am writing below code but its not working.i don't know how to implement logic my requirement.My requirement is find maximum value in userdefined objects and dispaly that object values only.This object store in another collection object(list,set)
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));
System.out.println(EmpList.getList(list));
}
}
Exectep OutPut Is: 1 naresh 900 print this object only
My Employe class:
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;
}
}
Please help any one how to write above logic
View Answers
October 19, 2012 at 10:47 AM
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);
}
}
Ads
Related Tutorials/Questions & Answers:
How to find maximum value in ArrayList
How to
find maximum value in ArrayList **Sir i am writing below code but its not working.i don't know
how to implement logic my requirement.My requirement is
find maximum value in userdefined objects and dispaly that object
How to find maximum value in ArrayList
How to
find maximum value in ArrayList **Sir i am writing below code but its not working.i don't know
how to implement logic my requirement.My requirement is
find maximum value in userdefined objects and dispaly that object
Advertisements
How to find maximum value in ArrayList
How to
find maximum value in ArrayList **Sir i am writing below code but its not working.i don't know
how to implement logic my requirement.My requirement is
find maximum value in userdefined objects and dispaly that object
How to find maximum value in ArrayList
How to
find maximum value in ArrayList **Sir i am writing below code but its not working.i don't know
how to implement logic my requirement.My requirement is
find maximum value in userdefined objects and dispaly that object
Find max and min value from Arraylist
Find max and min
value from
Arraylist
In this tutorial, you will learn
how to
find the
maximum and minimum
value
element from the
ArrayList. Java provides direct methods to get
maximum and
minimum
value from any collection class i.e
maximum size of arraylist - java
maximum size of
arraylist - java 1.what is the
maximum size of
arraylist?
2.what is the drawback of arralist?
2.what is the drawback of JDBC... on size of RAM. The theoretical
maximum number of elements in an
ArrayList is 2
how to display each arraylist value on new page in jsp
how to display each
arraylist value on new page in jsp hi!!!!
i want to display each
arraylist value on new page in jsp?????
and also want to access the
arraylist values using next button.....
any help would be appreciated
find largest value
find largest value (Assignment 1 - LargestValue)The process of finding the largest
value (i.e., the
maximum of a group of values) is used frequently... to count to 10 (i.e., to keep track of
how many numbers have been input
Find winner of local election using ArrayList
Find winner of local election using
ArrayList
Here is an example that allow the user to enter the last names of five
candidates in a local election... into
ArrayList. The program output each
candidate's name, the votes received
How to Convert ArrayList to Array?
How to Convert
ArrayList to Array? Hi,
I am trying to learn to Convert
ArrayList to Array.
How it is possible?
How to Convert
ArrayList to Array?
Thanks
Hi,
To Convert
ArrayList to array ArrayList.toArray
How to show the maximum and the minimum in array...
How to show the
maximum and the minimum in array... I have a bookstore application, but I cannot show the
maximum and the minimum price of the book... to SAJID's Book Shop");
System.out.print("
How many book would you like to store
How to show the maximum and the minimum in array...
How to show the
maximum and the minimum in array... I have a bookstore application, but I cannot show the
maximum and the minimum price of the book... to SAJID's Book Shop");
System.out.print("
How many book would you like to store
MySql Absolute Value
MySql Absolute
Value
This example illustrates
how to
find the absolute
value of the table.
In this example we use abs keywords to
find the absolute
value in the sql
query. In the given table the column emp_
value define float type when
How to convert Arraylist into String Array Java
information with example. You can
find the online example of
how to convert
arraylist...
How to convert
Arraylist into String Array Java Hi,
I am beginners of Java programming. Can somebody Suggest me
how to convert
arraylist to string
Find Value of Column
Find Value of Column
In this program we are going to
find the
value of row from... example.xls
String table
value 0 Name Of Example
String table
value 1
arraylist
arraylist Hi
How can we eliminet duplicate element from
arraylist?
How can we
find highest salary from
arraylist ?
How can we highest key
value pair from map?
Thanks
Kalins Naik
Remove duplicates from
Arraylist
how to archive cllocation value
how to archive cllocation value hi,
Am working with location based application, need to store cllocation
value in array, for that i need to arhive it using NSKeyarchiver...
haved searche not net but didnt
find anything on
how
how to archive cllocation value
how to archive cllocation value hi,
Am working with location based application, need to store cllocation
value in array, for that i need to arhive it using NSKeyarchiver...
haved searched on net but didnt
find anything on
how
How to display nested ArrayList in JSF
How to display nested
ArrayList in JSF I have a class... to display it in jsf but i dont know
how and i am getting mad.
I have nested... very well
how it works.
Please, I need help.
Thank you very much
How to create new arraylist using Java
How to create new
arraylist using Java hi,
I want to develop an small application where i trying to create new
arraylist inside the
arraylist. Can someone provides online example
how to create new
arraylist in java programming