Getting the minimum element of the given collection, according to the order induced by the specified comparator. All elements in the collection must be mutually comparable by the specified comparator (that is, comp.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the collection).
Getting the maximum element of the given collection, according to the order induced by the specified comparator. All elements in the collection must be mutually comparable by the specified comparator (that is, comp.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the collection).
package collection.demos;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
class Emp{
String name;
int salary;
public Emp(String name, int salary) {
this.name = name;
this.salary = salary;
}
@Override
public String toString() {
return "Emp [" +name + ", "+ salary + "]";
}
}
class MyComparator1 implements Comparator<Emp>{
public int compare(Emp o1, Emp o2) {
return o1.salary-o2.salary;
}
}
public class GettingMinMaxEelementUsingComparator {
public static void main(String[] args) {
ArrayList<Emp> list=new ArrayList<Emp>();
list.add(new Emp("ABC",345345));
list.add(new Emp("Syndi",454544));
list.add(new Emp("Upal",332222));
list.add(new Emp("Tryno",875454));
System.out.println("Element of Min salary : "+Collections.min(list, new MyComparator1()));
System.out.println("Element of Max salary : "+Collections.max(list, new MyComparator1()));
}
}
/*
OUTPUT :
Element of Min salary : Emp [Upal, 332222]
Element of Max salary : Emp [Tryno, 875454]
*/
Subscribe to:
Post Comments (Atom)
Popular Posts
-
HSQLDB is a portable RDBMS implemented in pure java. It can be embedded with your application as well as can be used separately. It is very ...
-
If you want to use the database into your web application, you can use the HSQLDB in In_Process mode. In this mode, you can embed the HSQLD...
-
HTML Page < html > < head > < title > welcome </ title > < script language = "javascript"...
-
Spring MVC provides support for processing the form as well as server side validation. It maps request parameters to form backing bean and ...
-
Some time you will see the form containing the button " Add More " . This facility is provided for the user to get the values ...
-
JSP code <%@page import="java.sql.*"%> <%! int prod_id=1; String prod_name="Laptop"; int qty=2; float p...
-
In web application ApplicationContext is created using Context Loaders. there are two implementations of context loader. ContextLoaderLis...
-
Hibernate ORM framework does not provide any approch to connect to MS Access database. Unfortunately, Access is not supported officially...
-
package process; import javax.swing. * ; import javax.swing.table. * ; import java.sql. * ; /** * This class create JTable from Da...
-
Using " mappedBy " attribute of mapping annotations(like @OneToOne, @OneToMany, @ManyToMany) for bi-directional relationship. This...

No comments:
Post a Comment