Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

HashTable highest marks enumeration alternative

Hi,

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;
    }*/
  
    
}

Open in new window

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);

    }

}

Open in new window


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
Avatar of dpearson
dpearson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

b. Making Employee Comparable with marks as the criterion will enable you to just sort a List and get the highest mark a lot more easily

Open in new window


how to do this?
a. custom classes should almost always override hashCode and equals when they are used with collection classes. You got away with not doing here, but you should know collections can end up with unexpected behaviours otherwise.

Open in new window


how to do this?
Avatar of gudii9

ASKER

	for(Map<Integer, Employee>.Entry<Integer, Employee>) x:m) {
		Employee emp=x.getValue();

Open in new window



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>.Entry cannot be qualified with a parameterized type, since it
       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) {
Avatar of gudii9

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);

    }

}

Open in new window


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()) {

Open in new window

Avatar of gudii9

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);

    }

}

Open in new window

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;
    }*/
  
    
}

Open in new window


{5=com.gale.domain.Employee@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 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
Avatar of gudii9

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?
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);

	}

}

Open in new window

{5=com.gale.domain.Employee@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
Avatar of gudii9

ASKER

Open Declaration   Set<Entry<Integer, Employee>> java.util.Hashtable.entrySet()


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);

	}

}

Open in new window



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);