- 浏览: 133330 次
- 性别:
- 来自: 未来
文章分类
- 全部博客 (174)
- Eclispe (3)
- javaScript (33)
- SVG学习 (22)
- Java (21)
- 网站 (12)
- learn English (1)
- 文档 (25)
- 常用网站收录 (11)
- struts (6)
- 常用API (1)
- html (2)
- jsp (2)
- spring (2)
- IDE (1)
- 数据结构 (1)
- JDBC (2)
- html + css (1)
- xml (3)
- 心声 (1)
- Axis2 (1)
- svg (4)
- webService (2)
- unix (2)
- c/c++ (3)
- html5 (1)
- Android (2)
- Jet标签 (1)
- oracle (2)
- 工具 (1)
- ideas (0)
- lean plan (1)
- java技术网站,信息收集综合及分类 (0)
最新评论
-
calosteward:
感谢博主对svg convert to PDF资源的分享。我也 ...
How to Convert a SVG File to PDF Format -
long316:
fhhhh
WinArchiver 2.7 -
long316:
ddddd
WinArchiver 2.7
http://www.javadeveloper.co.in/java-example/java-comparator-example.html
Submitted by Andr on Tue, 10/26/2010 - 20:12
Java Examples
package examples;
/*
Java Comparator example.
This Java Comparator example describes how java.util.Comparator
interface is implemented to compare Java custom class objects.
This Java Comparator is passed to Collection's sorting method
(for example Collections.sort method) to perform sorting of Java custom
class objects.
*/
import java.util.*;
/*
java.util.Comparator interface declares two methods,
1) public int compare(Object object1, Object object2) and
2) boolean equals(Object object)
*/
/*
We will compare objects of the Employee class using custom comparators
on the basis of employee age and name.
*/
class Employee{
private int age;
private String name;
public void setAge(int age){
this.age=age;
}
public int getAge(){
return this.age;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
}
/*
User defined Java comparator.
To create custom java comparator, implement Comparator interface and
define compare method.
The below given comparator compares employees on the basis of their age.
*/
class AgeComparator implements Comparator{
public int compare(Object emp1, Object emp2){
/*
* parameter are of type Object, so we have to downcast it
* to Employee objects
*/
int emp1Age = ((Employee)emp1).getAge();
int emp2Age = ((Employee)emp2).getAge();
if(emp1Age > emp2Age)
return 1;
else if(emp1Age < emp2Age)
return -1;
else
return 0;
}
}
/*
The below given comparator compares employees on the basis of their name.
*/
class NameComparator implements Comparator{
public int compare(Object emp1, Object emp2){
//parameter are of type Object, so we have to downcast it to Employee objects
String emp1Name = ((Employee)emp1).getName();
String emp2Name = ((Employee)emp2).getName();
//uses compareTo method of String class to compare names of the employee
return emp1Name.compareTo(emp2Name);
}
}
/*
This Java comparator example compares employees on the basis of
their age and name and sort them in that order.
*/
public class JavaComparatorExample{
public static void main(String args[]){
//Employee array which will hold employees
Employee employee[] = new Employee[2];
//set different attributes of the individual employee.
employee[0] = new Employee();
employee[0].setAge(40);
employee[0].setName("Joe");
employee[1] = new Employee();
employee[1].setAge(20);
employee[1].setName("Mark");
System.out.println("Order of employee before sorting is");
//print array as is.
for(int i=0; i < employee.length; i++){
System.out.println( "Employee " + (i+1) + " name :: " + employee[i].getName() + ", Age :: " + employee[i].getAge());
}
/*
Sort method of the Arrays class sorts the given array.
Signature of the sort method is,
static void sort(Object[] object, Comparator comparator)
IMPORTANT: All methods defined by Arrays class are static. Arrays class
serves as a utility class.
*/
//Sorting array on the basis of employee age by passing AgeComparator
Arrays.sort(employee, new AgeComparator());
System.out.println("\n\nOrder of employee after sorting by employee age is");
for(int i=0; i < employee.length; i++){
System.out.println( "Employee " + (i+1) + " name :: " + employee[i].getName() + ", Age :: " + employee[i].getAge());
}
//Sorting array on the basis of employee Name by passing NameComparator
Arrays.sort(employee, new NameComparator());
System.out.println("\n\nOrder of employee after sorting by employee name is");
for(int i=0; i < employee.length; i++){
System.out.println( "Employee " + (i+1) + " name :: " + employee[i].getName() + ", Age :: " + employee[i].getAge());
}
}
}
/*
OUTPUT of the above given Java Comparable Example would be :
Order of employee before sorting is
Employee 1 name :: Joe, Age :: 40
Employee 2 name :: Mark, Age :: 20
Order of employee after sorting by employee age is
Employee 1 name :: Mark, Age :: 20
Employee 2 name :: Joe, Age :: 40
Order of employee after sorting by employee name is
Employee 1 name :: Joe, Age :: 40
Employee 2 name :: Mark, Age :: 20
*/
发表评论
-
JAVA Servlet 动态加载配置文件.properties
2013-11-22 15:53 620JAVA Servlet 可以定义.properties文件对 ... -
常用的java类库
2013-11-14 15:03 502常用的java类库 http://www.programcr ... -
8种常见的Java不规范代码
2013-11-05 12:06 376http://www.oschina.net/question ... -
Java中String字符串的常见10个问题
2013-11-04 17:45 516Java中String字符串的常见10个问题 1:怎么比较字 ... -
10 Java Regular Expression Examples You Should Know
2013-09-29 18:28 616转自 http://www.mkyong.com/regula ... -
JAVA核心技术8
2012-12-23 23:35 528JAVA核心技术8 -
Effective Java (目录)
2012-07-20 00:06 612http://www.cnblogs.com/stephen- ... -
json
2012-07-17 17:09 537http://564090701.iteye.com/blog ... -
java学习笔记
2012-07-15 14:34 573java学习笔记 -
Java内部类
2012-05-31 00:39 494Java内部类 从Java1.1开始引入了内部类以来,它就引起 ... -
Java String toUpperCase(Locale locale)Example
2012-05-31 00:34 1732http://www.codingdiary.com/deve ... -
NoClassDefFoundError
2012-04-25 15:34 529http://wolfdream.iteye.com/blog ... -
session持久化异常
2012-03-15 11:02 896http://www.iteye.com/topic/4128 ... -
session超时处理
2012-03-09 16:51 1262http://shmily2038.iteye.com/blo ... -
System.getProperty(String name)方法用于得到系统的属性.
2012-02-27 17:55 793http://www.iteye.com/topic/1029 ... -
J2Me,J2Se开发
2012-01-06 00:19 542J2ME手机开发基础 http://java.chinai ... -
java6 API
2011-12-20 11:49 883java6 API http://docs.oracle.c ... -
Json入门
2011-12-14 12:34 354http://blog.csdn.net/xiazdong/a ... -
java编程那些事
2011-10-21 16:57 548java编程那些事 http://blog.csdn.net/ ... -
==与equals的区别
2011-08-25 09:25 477http://12345678.iteye.com/blog/ ...
相关推荐
java Comparator 用法 例子java Comparator 用法 例子java Comparator 用法 例子java Comparator 用法 例子
java排序Comparator和Comparable
java的Comparator和Comparable
Java-中的-Comparator-和-Comparable.md
主要介绍了JAVA使用Comparator接口实现自定义排序,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
本文,先介绍Comparable 和Comparator两个接口,以及它们的差异;接着,通过示例,对它们的使用方法进行说明
主要介绍了Java Comparator比较器实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Java中Comparator接口的用法 对集合排序的例子 Comparator
主要介绍了Java中实现Comparator接口和用法实例(简明易懂),本文给出实现Comparator接口的实例和使用这个接口的代码实例,需要的朋友可以参考下
主要介绍了java优先队列PriorityQueue中Comparator的用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
1.import java.util.Comparator; 2.import net.sourceforge.pinyin4j.PinyinHelper; 3./** 4. * 汉字按照拼音排序的比较器 5. * @author KennyLee 2009-2-23 10:08:59 6. * 7. */ 8.public class ...
Comparator() { @Override public int compare(String s1, String s2) { return s1.compareTo(s2); } }); Java 8 Collections.sort(names, (s1, s2) -> s1.compareTo(s2)); #Java 8 - Lambda 表达式 句法 parameter ...
主要介绍了java比较器comparator使用示例,需要的朋友可以参考下
Java中Comparable和Comparator的区别共3页.pdf.zip
程序实现时间排序Comparator
主要介绍了Java8 Comparator排序方法实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Comparable与Comparator的区别Java开发Java经验技巧共3页.pdf.zip
像这样的日志代码会更好: if (log.isLoggable(Level.FINE)) { ...假定你已经完成了分析,并且判断出是运行环境中Java 组件的性能需要改善。 ——参考资料:仅个人学习,未详尽测试,请自行调试!!
NULL 博文链接:https://hellogava.iteye.com/blog/1570101
public class SetTest java