Home Answers Viewqa Java-Beginners How to find maximum value for userdefined objects in ArrayList

 
 


sontinaresh
How to find maximum value for userdefined objects in ArrayList
1 Answer(s)      7 months ago
Posted in : Java Beginners

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 Pages:
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
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
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
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
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
Objects
Objects       Objects In this section we are going to illustrate how the real life objects are the key... to explain, how to represent objects in Java using classes, methods
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
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
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.
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
Counting Objects Clandestinely - Java Tutorials
(); counting = true; } } You can now know exactly how many objects were... this and that you cannot see where objects were created from. I don't know how... then analyse the stack traces and find out exactly where the objects are constructed
Serializing Objects Into Database - java tutorials
", where I look at how we can serialize objects into a database using JDBC. Thank... was released with an improved JDBC-ODBC bridge, was how to serialize objects...? Before we look at how we can write binary objects into a database table, what
Array - Maximum
it isn't the maximum value that is desired, but the index of the maximum... Java NotesArray - Maximum Finding the maximum is basically the same as finding the minimum; remember the first value, then look through all the other
Maximum Solutions - Specialists in Object Orientation
that means, but I am overwhelmed that I had been noticed, considering how small... at the Java In Action conference, and will demonstrate how dynamic... whizz to Java performance fundi and has some great insight into how to make
ArrayList
of a ArrayList is that it holds only Objects and not primitive types (eg, int... Java Notes: ArrayList java.util.ArrayList allows for expandable arrays, and is the Collections replacement for the older Vector class. An ArrayList has
ArrayList
of a ArrayList is that it holds only Objects and not primitive types (eg, int... Java Notes: ArrayList java.util.ArrayList allows for expandable arrays, and is the Collections replacement for the older Vector class. An ArrayList has
Counting Objects Clandestinely - Java Tutorials
: While the code you wrote helps find how many objects are created, your friend...Counting Objects Clandestinely 2001-12-28 The Java Specialists' Newsletter [Issue 038b] - Counting Objects Clandestinely - Follow-up Author: Dr. Heinz M
objects
objects  account a=new account(100); value 100 is used for what purpose of the object
ArrayList programe
ArrayList programe  How to write a java program to accept an array list of Employee objects. search,delete and modify a particular Employee based on Id Number (like ID,Name&Address
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
ArrayList in java
ArrayList in java  sir i have a class Student.it contains id,name,marks of students and a parameteraised constructor.now i created an arraylist for storing the objects of Student class.i.e Student s1=new student(1,"ram",23
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
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 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
arraylist
% *(noOfYearService/2). Store each employee information in different arrayList depending on his.....and can help me..give some clue in coding..how to start this program.. View Answers
ArrayList
ArrayList  How to increment in ArryList
java arraylist
need to create arraylist whose name is same as type value(e.g : list1,list2... help how to handle this through java arraylist. xml : <?xml version='1.0... which is having type list1 in the arraylist named list1. the problem here
arraylist
arraylist  create a class employee having instance variable id,name,age,salary.now , when u run the program it must be ask u to 1.add employee 2.view employee 3.exit if u enter value other than 1,2,3 program should say
arrayList
arrayList  how do i print a single index have multiple values String dist = "select distcode,distname from iwmpdistrict where stcode=29... in dist_name ,How I print them . myList.add(dist_name); printing only last district
Are objects passed by value or by reference?
Are objects passed by value or by reference?  Are objects passed by value or by reference
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
arraylist of an arraylist
arraylist of an arraylist  Can anyone suggest me how to use arraylist of an arraylist?? how to put data into it and get data from it???? becoz i want to make rows and column dynamic which can grow as per requirement?????/ plz
Core Java-ArrayList
Core Java-ArrayList  How do i find duplicates in ArrayList. First i add some elements to ArrayList, then how do i find the duplicates and show the duplicate elements. Give an example
select maximum time entry row - JDBC
select maximum time entry row  Hi fried you given ans this Sql Query for select maximum value max() function use. "select max(fieldname) from tablename" but this query only for find a max value of that column
Remove multiple elements in arraylist
Remove multiple elements in arraylist   how can we remove multiple values in arrayList? only remove method we used for single remove.but i want to delete multiple value in arrayList
Apache POI Excel Maximum Row - JSP-Servlet
Apache POI Excel Maximum Row  I am using Apache POI lib for export jsp results to excel file. I am getting error : Row number must be between 0... row value ? and How can I change that ? Thanks in advance Regards
find the factorial
find the factorial  how to find the factorial through the while loop...) { int value = 5, factorial = 1, temp = value; while...--; } System.out.println("The factorial of " + value + " is " + factorial
ArrayList<E>
of contacts. Objects only. A possible disadvantage of ArrayList is that it holds... class. An ArrayList has these characteristics: An ArrayList automatically expands as data is added. Access to any element of an ArrayList is O(1). Insertions
Insert an array list of objects with addAll(ArrayList) method
Insert an array list of objects with addAll(ArrayList) method In this section you will learn to insert an array list of objects to another list...; with an ArrayList . Populate it with the object using  the addAll(ArrayList
Any Link Between ArrayList and HashMap - Java Interview Questions
Any Link Between ArrayList and HashMap  Hi Friends, Can u give few interview questions which relates ArrayList and Hashmap. I mean how to link... of objects while HashMap is a collection of key and value pairs while 2
Mapping objects in XML - Spring
Mapping objects in XML   Hello, I´ve read that you can define... Ok, but, if I want to set a different value for "name" each time I create the object... How can I define the XML? I don´t understand the utility about
ArrayList problem - JSP-Servlet
ArrayList problem  Hi, How can I add elements of an ArrayList to HTML..." name="MYLIST"> <% ArrayList a_list = new ArrayList... = (MyClass)it.next (); %><option value="<%=obj.getId()%>"><
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
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
Avanced objects - Java Beginners
of the human and should be represented by separate objects. All of your objects should... like you. Finally produce a UML class diagram that explains how your classes... a private map with rating as the key and minimum age as the value, populate this map

Ask Questions?

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.