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
What is difference between equals and hashcode method?
What is ph and buffers?
Is null == null in java?
What are functions in java?
Can we print null in java?
What is oop principle in java?
What is difference between ++ I and I ++ in java?
What is close method? How it's different from Finalize & Dispose?
Can we have this () and super () together?
How do you use find and replace?
What do you mean by stack?
What is the purpose of abstract class?
What are basic keywords?
What is the use of keywords in java?
What is udp in java?