Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


how to get the max salary and name of employee from
arraylist without using the Comperator or even Comparable
interface?

Answers were Sorted based on User's Feedback



how to get the max salary and name of employee from arraylist without using the Comperator or even ..

Answer / 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

how to get the max salary and name of employee from arraylist without using the Comperator or even ..

Answer / 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

More Core Java Interview Questions

Can we override protected method in java?

0 Answers  


What is the use of a copy constructor?

0 Answers   Flextronics,


Explain about the main() method in java?

0 Answers  


What is the difference between array and arraylist? what is the difference between arraylist and linkedlist?

8 Answers   EDS, Matrix,


Is multiple inheritance supported by java?

0 Answers  


What is java and its types?

0 Answers  


What’s a deadlock?

0 Answers  


what is the full form of java

3 Answers  


What is a private class in java?

0 Answers  


What are exceptions

0 Answers   Futures First,


Enlist diffrent types of inheritance supported by java?

0 Answers  


Memory management in JAVA

3 Answers   Tech Mahindra, Wipro,


Categories