How to find maximum value for userdefined objects in ArrayList

How to find maximum value for userdefined objects in ArrayList

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;
}

}

View Answers

October 19, 2012 at 10:46 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);
   }
}









Related Tutorials/Questions & Answers:
How to find maximum value for userdefined objects in ArrayList
How to find maximum value for userdefined objects in ArrayList  Hi sir,I have requirement i.e finding maximum value in userdefined objects... value but can't store that object into another object.Please help me how
How to find maximum value for userdefined objects in ArrayList
How to find maximum value for userdefined objects in ArrayList  Hi sir,I have requirement i.e finding maximum value in userdefined objects... value but can't store that object into another object.Please help me how
Advertisements
How to find maximum value for userdefined objects in ArrayList
How to find maximum value for userdefined objects in ArrayList  Hi sir,I have requirement i.e finding maximum value in userdefined objects... value but can't store that object into another object.Please help me how
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
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 check if an arraylist contains a value in Java
How to check if an arraylist contains a value in Java  Hi, I have... to do this? How to check if an arraylist contains a value in Java? Thanks  ... of ArrayList class is the easiest method to find if an object/string/value contains
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
To find first two maximum numbers in an array
To find first two maximum numbers in an array  Java program to find first two maximum numbers in an array,using single loop without sorting array
Java arraylist of objects
In arrayList object is added by default This can be object of String, Integer or any Wrapper class Example of Java Arraylist of Objects import java.util.ArrayList; import java.util.List; public class
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
Are objects passed by value or by reference?
Are objects passed by value or by reference?  Are objects passed by value or by reference
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 find [count the number of integers whose value is less than the average value of the integers]
how to find [count the number of integers whose value is less than the average value of the integers]  Construct an algorithm that will prompt... of integers whose value is less than the average value of the integers. Your
Get Duplicate Objects from an ArrayList of Objects
Get Duplicate Objects from an ArrayList of Objects  public class Order { private String portId; private String action; private String idType; private String id; private BigDecimal amount; public String getPortId
Getting the Maximum Size of a Preference Key and Value
Getting the Maximum Size of a Preference Key and Value       This section demonstrates you to get the maximum... the maximum length of string for a key and MAX_VALUE_LENGTH provides the maximum length
How to obtain the selected data item from a list box when listbox contains objects wrapped under Arraylist?
How to obtain the selected data item from a list box when listbox contains objects wrapped under Arraylist?  I have a jsp page containing a list box... page. The listbox gets populated by the data contained in the arraylist. Now, I
retrieve value from database on the basis of maximum id number
retrieve value from database on the basis of maximum id number  hi, i want to retrieve value of maximum id number from the database and show that value in jTextField.when the user clicks on the button that maximum id number
ModuleNotFoundError: No module named 'value_objects'
'value_objects' How to remove the ModuleNotFoundError: No module named 'value_objects' error? Thanks   Hi, In your python...ModuleNotFoundError: No module named 'value_objects'  Hi, My
com.dnbcloud - dnbcloud-rest-value-objects version 0.2.3 Maven dependency. How to use dnbcloud-rest-value-objects version 0.2.3 in pom.xml?
-value-objects in pom.xml? How to use dnbcloud-rest-value-objects version 0.2.3...com.dnbcloud  - Version 0.2.3 of dnbcloud-rest-value-objects Maven... com.dnbcloud  - Version 0.2.3 of dnbcloud-rest-value-objects in project
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
write a program to create an arraylist and listeterator.and value should be enter through keyboard.
write a program to create an arraylist and listeterator.and value should be enter through keyboard.  write a program to create an arraylist and listeterator.and value should be enter through keyboard
write a program to create an arraylist and listeterator.and value should be enter through keyboard.
write a program to create an arraylist and listeterator.and value should be enter through keyboard.  write a program to create an arraylist and listeterator.and value should be enter through keyboard
userdefined package
userdefined package  package javap; class HelloWorld { public static void main(String args[]) { System.out.println("hai"); } } i...) at java.lang.ClassLoader.loadClass(Unknown Source) Could not find the main class: javap.HelloWorld.
To find first two maximum numbers in an array,using single loop without sorting array.
To find first two maximum numbers in an array,using single loop without sorting array.  Java program to find first two maximum numbers in an array,using single loop without sorting array
To find first two maximum numbers in an array,using single loop without sorting array.
To find first two maximum numbers in an array,using single loop without sorting array.  Java program to find first two maximum numbers in an array,using single loop without sorting array
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
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
Version of com.transficc>portal-value-objects dependency
List of Version of com.transficc>portal-value-objects dependency
arraylist
arraylist   Using arraylist class , what type of exception are throws, how can write a java programe   An ArrayList can throw... ArraylistException { void buildAlphabet() { ArrayList list = new ArrayList
Find Cylinder area By inputting value from user
Find Cylinder area By inputting value from user  Hello Sir I need the code for calculating the area of a cylinder in which it asks radius value from user and then calculate area using this radius value. Please provide me
Version of com.dnbcloud>dnbcloud-rest-value-objects dependency
List of Version of com.dnbcloud>dnbcloud-rest-value-objects dependency
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
ArrayList
ArrayList  import java.util.*; class ArrayListDemo2 { public static void main(String args[]) { ArrayList al=new ArrayList(); al.add("one... for this output? How to get my expected output
Maven Repository/Dependency: com.transficc | portal-value-objects
Maven Repository/Dependency of Group ID com.transficc and Artifact ID portal-value-objects. Latest version of com.transficc:portal-value-objects dependencies...? How to create Maven Web Application in Eclipse? Maven 3 Tutorial Convert
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
write a program to create an arraylist with string(add,remove) operation.and value should be enter through keyboard.
write a program to create an arraylist with string(add,remove) operation.and value should be enter through keyboard.  write a program to create an arraylist with string(add,remove) operation.and value should be enter through
write a program to create an arraylist with string(add,remove) operation.and value should be enter through keyboard.
write a program to create an arraylist with string(add,remove) operation.and value should be enter through keyboard.  write a program to create an arraylist with string(add,remove) operation.and value should be enter through
arraylist
arraylist  Hi i have class A , i have added employee name and id in arraylist, then how can i find out all infomation of class A using emplyee... data into an arraylist and display the data of the particular employee according
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
arraylist
arraylist  Hi how can we eliminet duplicate element from arraylist in java? how can we achieve data abstrcation and encapulation in java? how many type of modifier are there in java? Thanks kalins anik   Remove
How to sort ArrayList in java
How to sort ArrayList in java In this section you will learn how to sort ArrayList in java. ArrayList support dynamic array that can grow as needed. Array.... In this tutorial  we will sort the ArrayList using sort method of collection
Maven dependency for com.dnbcloud - dnbcloud-rest-value-objects version 0.2 is released. Learn to use dnbcloud-rest-value-objects version 0.2 in Maven based Java projects
and Gradle. How to use  com.dnbcloud - dnbcloud-rest-value-objects version...-value-objects released The developers of   com.dnbcloud - dnbcloud-rest-value-objects project have released the latest version of this library on 09
Maven dependency for com.dnbcloud - dnbcloud-rest-value-objects version 0.2.13 is released. Learn to use dnbcloud-rest-value-objects version 0.2.13 in Maven based Java projects
-rest-value-objects released The developers of   com.dnbcloud - dnbcloud-rest-value-objects project have released the latest version of this library...-value-objects library is 0.2.13. Developer can use this version ( com.dnbcloud
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. ADS_TO_REPLACE_1 Please, I need help

Ads