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
Can we override tostring method in java?
What are recursive functions?
Explain the importance of import keyword in java?
What is a dynamic array in java?
Can static methods access instance variables in java?
Can we have more than one package statement in source file ?
Explain the available thread states in a high-level?
What about interthread communication and how it takes place in java?
What is a nested list?
Does sprintf add a null terminator?
What are design patterns and please explain?
What is the string function?
What does exclamation mean in java?
Is a class subclass of itself?
What is a protected method?