JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Program: How to get max element of a list of user defined objects?


Description:

Collections.max() method returns the maximum element of the given collection, according to the natural ordering of its elements. All elements in the collection must implement the Comparable interface. Furthermore, all elements in the collection must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the collection). Below example shows how to get max salary from the given list of user defined objects.


Code:
package com.java2novice.collections;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MyMaxUDOComparable {

	public static void main(String a[]){
		
		List<Empl> emps = new ArrayList<Empl>();
		emps.add(new Empl(10, "Raghu", 25000));
		emps.add(new Empl(120, "Krish", 45000));
		emps.add(new Empl(210, "John", 14000));
		emps.add(new Empl(150, "Kishore", 24000));
		Empl maxSal = Collections.max(emps);
		System.out.println("Employee with max salary: "+maxSal);
	}
}

class Empl implements Comparable<Empl>{
	
	private int id;
	private String name;
	private Integer salary;
	
	public Empl(int id, String name, Integer sal){
		this.id = id;
		this.name = name;
		this.salary = sal;
	}
	
	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 Integer getSalary() {
		return salary;
	}
	public void setSalary(Integer salary) {
		this.salary = salary;
	}
	@Override
	public int compareTo(Empl emp) {
		
		return this.salary.compareTo(emp.getSalary());
	}
	public String toString(){
		return id+"  "+name+"   "+salary;
	}
}

Output:
Employee with max salary: 120  Krish   45000
<< Previous Program | Next Program >>

List Of All Collections Class Sample Programs:

  1. How to add all elements to the given collection object?
  2. Write an example for Collections.asLifoQueue() method.
  3. How to search user defined object from a List by using binary search?
  4. Write an example for Collections.checkedCollection() method.
  5. Write an example for Collections.checkedList() method.
  6. Write an example for Collections.checkedSet() method.
  7. Write an example for Collections.checkedMap() method.
  8. How to check there in no common element between two list objects by using Collections.disjoint() method?
  9. How to create empty list using Collections class?
  10. How to create empty set using Collections class?
  11. How to create empty map using Collections class?
  12. How to Enumeration for ArrayList object?
  13. How to fill or replace elements of a List or ArrayList?
  14. How to find repeated element cound (frequency) of a given collection?
  15. How to convert Enumeration to List object?
  16. How to get index of a sub list from another list?
  17. How to get last index of a sub list from another list?
  18. How to get max element from the given list?
  19. How to get min element from the given list?
  20. How to get max element of a list of user defined objects?
  21. How to get min element of a list of user defined objects?
  22. How to get max element of a list of user defined objects using Comparator?
  23. How to get min element of a list of user defined objects using Comparator?
  24. How to create multiple copies of a given object?
  25. How to replace all occurances of a given object in the list?
  26. How to rotate elements in the list by specified distance?
  27. How to create synchronized list?
  28. How to create synchronized set?
  29. How to create synchronized map?
Knowledge Centre
Difference between while and do-while
The while statement verifies the condition before entering into the loop to see whether the next loop iteration should occur or not. The do-while statement executes the first iteration without checking the condition, it verifies the condition after finishing each iteration. The do-while statement will always execute the body of a loop at least once.
Famous Quotations
If you don’t make mistakes, you’re not working on hard enough problems. And that’s a big mistake.
-- Frank Wilczek

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.