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


Please Help Members By Posting Answers For Below Questions

What is api data?

637


What is regex in java?

600


What are the concepts of 'OOPS'?

690


What does int [] mean in java?

636


Describe 2 different ways to concatenate two strings.

766






Is java written in c?

628


If a variable is declared as private, where may the variable be accessed?

643


What is object data type?

647


What does java edition mean?

619


What is an inner class in java?

605


what is encapsulation in java? Explain

742


How to Sort Strings which are given in List and display in ascending order without using java api.

3920


How are this() and super() used with constructors in java programming?

660


What is the difference between the font and fontmetrics classes in java programming?

583


How do you reverse sort in java?

598