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

Answer Posted / amit singh

import java.util.*;
class Emp
{

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

}

}




class Manhattan
{

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(100,"itika"));
a.add(new Emp(90,"latika"));
a.add(new Emp(67,"jatin"));
a.add(new Emp(340,"nitin"));
a.add(new Emp(2300,"linda"));


Iterator<Emp> i = a.iterator();
int maxsalary=0;
String name = null;
if(i.hasNext())
{
Emp e=i.next();
maxsalary=e.salary;
}

Iterator<Emp> i1 = a.iterator();
while(i1.hasNext())
{

Emp e1 = i1.next();
if(maxsalary<=e1.salary)
{
maxsalary=e1.salary;
name=e1.name;
}
//System.out.println(maxsalary);

}

System.out.println("he person name is " + name + " whose
havineg the max salary " + maxsalary);
}




}

Is This Answer Correct ?    19 Yes 4 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How to perform quicksort in java?

569


What are the loops in java?

526


What is meant by data hiding in java?

636


If I only change the return type, does the method become overloaded?

537


Explain about serializable interface in java?

584






Is java util regex pattern thread safe?

516


What is nan in java?

520


What is a file pointer?

516


What are white spaces in java?

531


What is a boolean flag in java?

563


What is the full form of jpeg?

522


How do I compare two strings in word in java?

541


Which collection is ordered in java?

533


What happens if an exception is throws from an object's constructor?

627


What is a qualifier in a sentence?

528