how to get the max salary and name of employee from
arraylist without using the Comperator or even Comparable
interface?

Answer Posted / sitaram

package Sample;

import java.util.*;

class Emp
{

int salary;
String name;
Emp(int i,String g)
{
this.salary=i;
this.name=g;

}

}

class MaxSalaryTest
{

public static void main(String []args)
{
ArrayList<Emp> a = new ArrayList<Emp>();
a.add(new Emp(100,"javed"));
a.add(new Emp(500,"apporva"));
a.add(new Emp(250,"sumit"));
a.add(new Emp(2500,"itika"));
a.add(new Emp(90,"latika"));
a.add(new Emp(3200,"jatin"));
a.add(new Emp(340,"nitin"));
a.add(new Emp(2300,"linda"));

Iterator<Emp> i = a.iterator();
int maxsalary=0;
int sal = 0;
while(i.hasNext()){
Emp e = i.next();
sal = e.salary;
if(sal > maxsalary){
maxsalary= sal;
}
}
System.out.println("maximum salary..."+maxsalary);
}

}

Is This Answer Correct ?    15 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What does || || mean in math?

515


What is a parameter example?

531


What is the purpose of sizeof operator?

532


How do you compare two strings lexicographically?

538


Tell us something about an iterator.

550






What is comparator in java?

557


How many days will it take to learn java?

519


Is java platform independent?

570


What is a ?

755


Why there is no call by reference in java?

496


Why are there no global variables in java?

587


What is a singleton class? Give a practical example of its usage.

591


Write a code to create a trigger to call a stored procedure

535


How does linkedhashmap work in java?

519


Why pass by reference is not possible in java?

497