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
What is used of static keyword in java?
What is difference between next () and nextline () in java?
What is an empty class? What functionality does it offer in Java?
What is lifetime variable?
Define max and min heap, also the search time of heap.
Is main a keyword in java?
what are synchronized methods and synchronized statements? : Java thread
What is bitwise complement?
What is multi level inheritance in java?
How we can make copy of a java object?
What is a map in java?
What are examples of modifiers?
How many bits are in a sentence?
Can we have any code between try and catch blocks?
How do you escape in java?