gudii9
asked on
HashTable highest marks enumeration alternative
Hi,
i got below output
I would like to know how to get maximum marks of the employee using iterator/ListIterator and entrySet instead of enumeraor.
when to use each
please advise
public class Employee {
int id;
public String name;
public int mark;
public Employee(int i, String names, int marks) {
// TODO Auto-generated constructor stub4
id=i;
name=names;
mark=marks;
}
/* public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}*/
}
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map.Entry;
import java.util.Set;
public class HashTableExBridged {
public static void main(String[] args) {
// TODO Auto-generated method stub
Hashtable<Integer, Employee> m = new Hashtable();
Employee e1 = new Employee(1, "sam", 90);
Employee e2 = new Employee(2, "mark", 91);
Employee e3 = new Employee(3, "lara", 92);
Employee e4 = new Employee(4, "alex", 93);
Employee e5 = new Employee(5, "bk", 94);
m.put(1, e1);
m.put(2, e2);
m.put(3, e3);
m.put(4, e4);
m.put(5, e5);
System.out.println(m);
Set<Entry<Integer, Employee>> s = m.entrySet();
for (Entry<Integer, Employee> x : s) {
System.out.println("key is" + x.getKey());
System.out.println("key is" + x.getValue().name);
System.out.println("key is" + x.getValue().mark);
}
Enumeration n = m.keys();
int id, big = 0, bigId = 0;
while (n.hasMoreElements()) {
id = (int) n.nextElement();
Employee emp = m.get(id);
if (big < emp.mark) {
big = emp.mark;
bigId = id;
}
}
Employee emp_marks=m.get(bigId);
System.out.println("name is"+emp_marks.name);
//System.out.println("dept is"+emp_marks.dept);
System.out.println("mark is"+emp_marks.mark);
}
}
i got below output
I would like to know how to get maximum marks of the employee using iterator/ListIterator and entrySet instead of enumeraor.
when to use each
please advise
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
for(Map<Integer, Employee>.Entry<Integer, Employee>) x:m) {
Employee emp=x.getValue();
Multiple markers at this line
- Can only iterate over an array or an instance of java.lang.Iterable
- Syntax error on token ")", delete this token
- The member type Map<Integer,Employee>.Entr
is static. Remove arguments from qualifying type Map<Integer,Employee>
what it means by above error
how above differet from below
for (Map.Entry<Integer, Employee> x : s) {
ASKER
package for_loops;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class HashTableExBridged {
public static void main(String[] args) {
// TODO Auto-generated method stub
Hashtable<Integer, Employee> m = new Hashtable();
Employee e1 = new Employee(1, "sam", 90);
Employee e2 = new Employee(2, "mark", 91);
Employee e3 = new Employee(3, "lara", 92);
Employee e4 = new Employee(4, "alex", 93);
Employee e5 = new Employee(5, "bk", 94);
m.put(1, e1);
m.put(2, e2);
m.put(3, e3);
m.put(4, e4);
m.put(5, e5);
System.out.println(m);
Set<Entry<Integer, Employee>> s = m.entrySet();
for (Entry<Integer, Employee> x : s) {
System.out.println("key is" + x.getKey());
System.out.println("key is" + x.getValue().name);
System.out.println("key is" + x.getValue().mark);
}
/* Enumeration n = m.keys();
int id, big = 0, bigId = 0;
while (n.hasMoreElements()) {
id = (int) n.nextElement();
Employee emp = m.get(id);
if (big < emp.mark) {
big = emp.mark;
bigId = id;
}
}*/
int big=0,bigId=0;
for(Map.Entry<Integer, Employee> x:m) {
Employee emp=x.getValue();
if(big<emp.mark){
big=emp.mark;
bigId=x.getKey();
}
}
Employee emp_marks=m.get(bigId);
System.out.println("name is"+emp_marks.name);
//System.out.println("dept is"+emp_marks.dept);
System.out.println("mark is"+emp_marks.mark);
}
}
as above gives error as below
Can only iterate over an array or an instance of java.lang.Iterable
at line 52
please advise
Should be
for(Map.Entry<Integer, Employee> x : m.entrySet()) {
ASKER
//for(Map.Entry<Integer, Employee> x:m) {
for(Map.Entry<Integer, Employee> x : m.entrySet()) {
what is difference between above commented line and below your line.
now it worked and gave below output
package com.gale.domain;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class HashTableExBridged {
public static void main(String[] args) {
// TODO Auto-generated method stub
Hashtable<Integer, Employee> m = new Hashtable();
Employee e1 = new Employee(1, "sam", 90);
Employee e2 = new Employee(2, "mark", 91);
Employee e3 = new Employee(3, "lara", 92);
Employee e4 = new Employee(4, "alex", 93);
Employee e5 = new Employee(5, "bk", 94);
m.put(1, e1);
m.put(2, e2);
m.put(3, e3);
m.put(4, e4);
m.put(5, e5);
System.out.println(m);
Set<Entry<Integer, Employee>> s = m.entrySet();
for (Entry<Integer, Employee> x : s) {
System.out.println("key is" + x.getKey());
System.out.println("key is" + x.getValue().name);
System.out.println("key is" + x.getValue().mark);
}
/* Enumeration n = m.keys();
int id, big = 0, bigId = 0;
while (n.hasMoreElements()) {
id = (int) n.nextElement();
Employee emp = m.get(id);
if (big < emp.mark) {
big = emp.mark;
bigId = id;
}
}*/
int big=0,bigId=0;
//for(Map.Entry<Integer, Employee> x:m) {
for(Map.Entry<Integer, Employee> x : m.entrySet()) {
Employee emp=x.getValue();
if(big<emp.mark){
big=emp.mark;
bigId=x.getKey();
}
}
Employee emp_marks=m.get(bigId);
System.out.println("name is"+emp_marks.name);
//System.out.println("dept is"+emp_marks.dept);
System.out.println("mark is"+emp_marks.mark);
}
}
package com.gale.domain;
public class Employee {
int id;
public String name;
public int mark;
public Employee(int i, String names, int marks) {
// TODO Auto-generated constructor stub4
id=i;
name=names;
mark=marks;
}
/* public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}*/
}
{5=com.gale.domain.Employe
key is5
key isbk
key is94
key is4
key isalex
key is93
key is3
key islara
key is92
key is2
key ismark
key is91
key is1
key issam
key is90
name isbk
mark is94
ASKER
why are we putting entrySet() return value to Map to find maximum marks?
Employee emp_marks = m.get(bigId);
what is meaning of above line?
e@15db9742 , 4=com.gale.domain.Employee @6d06d69c, 3=com.gale.domain.Employee @7852e922, 2=com.gale.domain.Employee @4e25154f, 1=com.gale.domain.Employee @70dea4e}
key is-->5
name is-->bk
marks are==>94
key is-->4
name is-->alex
marks are==>93
key is-->3
name is-->lara
marks are==>92
key is-->2
name is-->mark
marks are==>91
key is-->1
name is-->sam
marks are==>90
maximum marks person name is-->bk
maximum marks are--->94
i moved
Hashtable<Integer, Employee> m = new Hashtable();
to line 22 after employee declaration tp be more clear
Employee emp_marks = m.get(bigId);
what is meaning of above line?
package com.gale.domain;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class HashTableExBridged {
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee e1 = new Employee(1, "sam", 90);
Employee e2 = new Employee(2, "mark", 91);
Employee e3 = new Employee(3, "lara", 92);
Employee e4 = new Employee(4, "alex", 93);
Employee e5 = new Employee(5, "bk", 94);
Hashtable<Integer, Employee> m = new Hashtable();
m.put(1, e1);
m.put(2, e2);
m.put(3, e3);
m.put(4, e4);
m.put(5, e5);
System.out.println(m);
Set<Entry<Integer, Employee>> s = m.entrySet();
for (Entry<Integer, Employee> x : s) {
System.out.println("key is-->" + x.getKey());
System.out.println("name is-->" + x.getValue().name);
System.out.println("marks are==>" + x.getValue().mark);
}
/*
* Enumeration n = m.keys();
*
* int id, big = 0, bigId = 0; while (n.hasMoreElements()) { id = (int)
* n.nextElement();
*
* Employee emp = m.get(id); if (big < emp.mark) { big = emp.mark; bigId
* = id; } }
*/
int big = 0, bigId = 0;
// for(Map.Entry<Integer, Employee> x:m) {
for (Map.Entry<Integer, Employee> x : m.entrySet()) {
Employee emp = x.getValue();
if (big < emp.mark) {
big = emp.mark;
bigId = x.getKey();
}
}
Employee emp_marks = m.get(bigId);
System.out.println("maximum marks person name is-->" + emp_marks.name);
// System.out.println("dept is"+emp_marks.dept);
System.out.println("maximum marks are--->" + emp_marks.mark);
}
}
{5=com.gale.domain.Employekey is-->5
name is-->bk
marks are==>94
key is-->4
name is-->alex
marks are==>93
key is-->3
name is-->lara
marks are==>92
key is-->2
name is-->mark
marks are==>91
key is-->1
name is-->sam
marks are==>90
maximum marks person name is-->bk
maximum marks are--->94
i moved
Hashtable<Integer, Employee> m = new Hashtable();
to line 22 after employee declaration tp be more clear
ASKER
Open Declaration Set<Entry<Integer, Employee>> java.util.Hashtable.entryS et()
Note: The attached Javadoc could not be retrieved as the specified Javadoc location is either wrong or currently not accessible.
entrySEt() gives more like row of each record with Set Entry of Integer as a key and Employee as value
we are assuming bigMark as 0
and bigIdPerson 0
now as we go each row set entry getting highest mark and correspondign Id
int bigMark = 0, bigIdOfPerson = 0;
// for(Map.Entry<Integer, Employee> x:m) {
for (Map.Entry<Integer, Employee> x : m.entrySet()) {
Employee emp = x.getValue();
if (bigMark < emp.mark) {
bigMark = emp.mark;
bigIdOfPerson = x.getKey();
}
once we get that ID
getting that employee with highest marks and his name
Employee employeeHighestMarks = m.get(bigIdOfPerson);
System.out.println("maximu m marks person name is-->" + employeeHighestMarks.name) ;
// System.out.println("dept is"+emp_marks.dept);
System.out.println("maximu m marks are--->" + employeeHighestMarks.mark) ;
Note: The attached Javadoc could not be retrieved as the specified Javadoc location is either wrong or currently not accessible.
entrySEt() gives more like row of each record with Set Entry of Integer as a key and Employee as value
we are assuming bigMark as 0
and bigIdPerson 0
now as we go each row set entry getting highest mark and correspondign Id
int bigMark = 0, bigIdOfPerson = 0;
// for(Map.Entry<Integer, Employee> x:m) {
for (Map.Entry<Integer, Employee> x : m.entrySet()) {
Employee emp = x.getValue();
if (bigMark < emp.mark) {
bigMark = emp.mark;
bigIdOfPerson = x.getKey();
}
once we get that ID
getting that employee with highest marks and his name
package com.gale.domain;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class HashTableExBridged {
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee e1 = new Employee(1, "sam", 90);
Employee e2 = new Employee(2, "mark", 91);
Employee e3 = new Employee(3, "lara", 92);
Employee e4 = new Employee(4, "alex", 93);
Employee e5 = new Employee(5, "bk", 94);
Hashtable<Integer, Employee> m = new Hashtable();
m.put(1, e1);
m.put(2, e2);
m.put(3, e3);
m.put(4, e4);
m.put(5, e5);
System.out.println(m);
/*Set<Entry<Integer, Employee>> s = m.entrySet();
for (Entry<Integer, Employee> x : s) {
System.out.println("key is-->" + x.getKey());
System.out.println("name is-->" + x.getValue().name);
System.out.println("marks are==>" + x.getValue().mark);
}*/
/*
* Enumeration n = m.keys();
*
* int id, big = 0, bigId = 0; while (n.hasMoreElements()) { id = (int)
* n.nextElement();
*
* Employee emp = m.get(id); if (big < emp.mark) { big = emp.mark; bigId
* = id; } }
*/
int bigMark = 0, bigIdOfPerson = 0;
// for(Map.Entry<Integer, Employee> x:m) {
for (Map.Entry<Integer, Employee> x : m.entrySet()) {
Employee emp = x.getValue();
if (bigMark < emp.mark) {
bigMark = emp.mark;
bigIdOfPerson = x.getKey();
}
}
Employee employeeHighestMarks = m.get(bigIdOfPerson);
System.out.println("maximum marks person name is-->" + employeeHighestMarks.name);
// System.out.println("dept is"+emp_marks.dept);
System.out.println("maximum marks are--->" + employeeHighestMarks.mark);
}
}
Employee employeeHighestMarks = m.get(bigIdOfPerson);
System.out.println("maximu
// System.out.println("dept is"+emp_marks.dept);
System.out.println("maximu
ASKER
Open in new window
how to do this?
Open in new window
how to do this?