在Java学习和使用里,工具类与算法类(collections和Arrays)也是我们使用比较多的,在它们里面就包含了comparable与comparator这两种比较器。

一.比较器的分类与概念

  1.comparable内部比较器:这是元素自身以来就有先人规定好的默认比较规则;

  2.comparator外部比较器:这是我们根据具体需要而自定义的第三方比较规则;(当有外部比较器时元素的自带内部比较器就会被取代)

注:放回的正数负数,依赖于根据比较规则两个元素位置的差。

二.使用内部比较器与外部比较器(排序,只能操作List)

  例:

设置的外部比较器:

package com.lovo.bean;

import java.util.Comparator;

public class StudentComparator implements Comparator<StudentBean>{

    public int compare(StudentBean o1, StudentBean o2) {

        if(o1.getScore() > o2.getScore()){//设置根据成绩进行排序大的在前面
return -1;
}else if(o1.getScore() < o2.getScore()){
return 1;
}
return 0;
} }

主函数

import java.util.Collections;
import com.lovo.bean.StudentBean;
import com.lovo.bean.StudentComparator; public class TestCollections { public static void main(String[] args) {//比较器专用例子
ArrayList<StudentBean> lst = new ArrayList<StudentBean>();
lst.add(new StudentBean("zhang3",30,74));
lst.add(new StudentBean("li4",22,67));
lst.add(new StudentBean("wang5",23,67));
lst.add(new StudentBean("zhao6",24,80));
lst.add(new StudentBean("chen7",26,56)); Collections.sort(lst);//自带内部比较器
System.out.println(Collections.max(lst));//求最大
System.out.println(Collections.min(lst));//求最小
Collections.reverse(lst);//反转
Collections.shuffle(lst);//混排--随机打乱排序
Collections.sort(lst,new StudentComparator());//提供外部比较器
for(StudentBean stu : lst){
System.out.println(stu);
}
}
}

比较器comparable与comparator的使用的更多相关文章

  1. 比较器Comparable和Comparator

    在java中要实现自定义类的比较,提供了以下两个接口: Comparable(内部排序) int compareTo(Object obj);返回值为int,默认升序排序 Comparator(外部排 ...

  2. Java的比较器Comparable与Comparator

    在Java中有两个比较器:Comparable.Comparator 对于Integer.Double等等类型,可以直接对他们进行比较,因为已经实现了比较的方式,然而在平时常常会面临需要对集合进行排序 ...

  3. Java原来如此-比较器(Comparable、Comparator)

    有时候需要对Collection或者不为单一数字的Array进行比较,有两种方法,1是实现Comparable接口,2是实现Comparator接口. 1.ComParable接口 Comparabl ...

  4. 比较器 comparable与comparator用法

    comparable 接口 Comparable<T> 类型参数:T - 可以与此对象进行比较的那些对象的类型 public interface Comparable<T> 此 ...

  5. 小白养成记——Java比较器Comparable和Comparator

    一.使用情景 1.  调用Arrays.sort()方法或Collections.sort()方法对自定义类的对象排序 以Arrays.sort()为例.假定有如下自定义的Person类 1 publ ...

  6. Comparable和Comparator的区别

    Comparable Comparable可以认为是一个内比较器,实现了Comparable接口的类有一个特点,就是这些类是可以和自己比较的,至于具体和另一个实现了Comparable接口的类如何比较 ...

  7. comparable和comparator

    Comparable Comparable可以认为是一个内部比较器,实现了Comparable接口的类有一个特点,就是这些类是可以和自己比较的,在compareTo方法中指定具体的比较方法. comp ...

  8. 对象比较器:Comparable和Comparator

    在进行对象数组排序的过程中需要使用到比较器,比较器有两个:Comparable和Comparator ①.java.lang.Comparable:是在类定义是时候默认实现好的接口,里面提供有一个co ...

  9. java中Comparable和Comparator两种比较器的区别

    Comparable和Comparator接口都是为了对类进行比较,众所周知,诸如Integer,double等基本数据类型,java可以对他们进行比较,而对于类的比较,需要人工定义比较用到的字段比较 ...

随机推荐

  1. Servlet和JAVA BEAN 分析探讨

    在JSP中调用JAVA类和使用JavaBean有什么区别? 可以像使用一般的类一样使用JavaBean,Bean只是一种特殊的类.特殊在可以通过<jsp:useBean   />调用Jav ...

  2. 转:ASP.NET MVC中Unobtrusive Ajax的妙用

    Unobtrusive Javascript有三层含义:一是在HTML代码中不会随意的插入Javsscript代码,只在标签中加一些额外的属性值,然后被引用的脚本文件识别和处理:二是通过脚本文件所增加 ...

  3. win7x64下的redis安装与使用

    先引用百度百科的一段话吧,具体可以到百科查看吧. Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年 ...

  4. poj 1330 Nearest Common Ancestors(LCA:最近公共祖先)

    多校第七场考了一道lca,那么就挑一道水题学习一下吧= = 最简单暴力的方法:建好树后,输入询问的点u,v,先把u全部的祖先标记掉,然后沿着v->rt(根)的顺序检查,第一个被u标记的点即为u, ...

  5. [Sciter系列] MFC下的Sciter–5.Sciter中GUI线程研究

    [Sciter系列] MFC下的Sciter–5.Sciter中GUI线程研究,目前MFC存在问题,win32没问题. 本系列文章的目的就是一步步构建出一个功能可用,接口基本完善的基于MFC框架的Sc ...

  6. error LNK2019: 无法解析的外部符号 __imp___CrtDbgReportW

    error LNK2005 and error LNK2019 error LNK2019: unresolved external symbol __imp___CrtDbgReportW refe ...

  7. Java [Leetcode 225]Implement Stack using Queues

    题目描述: Implement the following operations of a stack using queues. push(x) -- Push element x onto sta ...

  8. 【C#学习笔记】读SQL Server2008

    using System; using System.Data.SqlClient; namespace ConsoleApplication { class Program { static voi ...

  9. memcachedd基础

    系列文章导航: memcached完全剖析–1. memcached的基础 memcached全面剖析–2. 理解memcached的内存存储 memcached全面剖析–3. memcached的删 ...

  10. JavaScript在IE和Firefox(火狐)的不兼容问题解决方法小结 【转】http://blog.csdn.net/uniqer/article/details/7789104

    1.兼容firefox的 outerHTML,FF中没有outerHtml的方法. 代码如下: if (window.HTMLElement) { HTMLElement.prototype.__de ...