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
How many threads can I run java?
Is there any tag in htm to upload and download files?
What are the files generated after using IDL to java compiler?
Differentiate between run time error and syntax error.
What is anagram number?
What is :: operator in java 8?
Can you sort a string in java?
Is it possible to cast an int value into a byte variable? What would happen if the value of int is larger than byte?
Write a program to check string is palindrome without using loop?
What is a parameter in a function?
Give reasons supporting that string is immutable.
Which method must be implemented by all threads?
Discuss 2D arrays.
How many types of assembly languages are there?
In a container there are 5 components. I want to display all the component names, how will you do that?