1. Comparable接口与Comparator接口的对比
comparable指定了比较方法在待比较的类中,直接利用Collections.sort()或Arrays.sort()排序就可以了。
comparator则是指定了比较的行为,可以作为Collections.sort()或Arrays.sort()方法的参数
2.代码:
Comparable
Comparator
Datas
Test
comparable指定了比较方法在待比较的类中,直接利用Collections.sort()或Arrays.sort()排序就可以了。
comparator则是指定了比较的行为,可以作为Collections.sort()或Arrays.sort()方法的参数
2.代码:
Comparable
public class Person implements Comparable<Person>
{
private Integer id;
private String name;
private Integer age;
public Person()
{
}
public Person(Integer id, String name, Integer age)
{
this.id = id;
this.name = name;
this.age = age;
}
public Integer getId()
{
return id;
}
public void setId( Integer id )
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName( String name )
{
this.name = name;
}
public Integer getAge()
{
return age;
}
public void setAge( Integer age )
{
this.age = age;
}
@Override
public int compareTo( Person o )
{
return this.getAge().compareTo( o.getAge() );
}
}
Comparator
import java.util.Comparator;
public class PersonComparetor implements Comparator<Person>
{
@Override
public int compare( Person o1, Person o2 )
{
return o1.getAge().compareTo( o2.getAge() );
}
}
Datas
import java.util.ArrayList;
import java.util.List;
public class PersonsSingle
{
private static List<Person> persons = null;
private PersonsSingle()
{
}
public static List<Person> getPersons(){
persons = new ArrayList<Person>();
persons.add( new Person( 1, "张三",26 ) );
persons.add( new Person( 2, "王五",20 ) );
persons.add( new Person( 3, "赵六",29 ) );
persons.add( new Person( 4, "李四",26 ) );
persons.add( new Person( 5, "周公",50 ) );
return persons;
}
}
Test
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CollectionsTest
{
public void sort(){
double array[] = {112, 111, 23, 456, 231 };
List list = new ArrayList();
for (int i = 0; i < array.length; i++) {
list.add(new Double(array[i]));
}
Collections.sort(list);
for (int i = 0; i < array.length; i++) {
System.out.println(list.get(i));
}
System.out.println();
}
public void sortOne(){
List<Person> list = PersonsSingle.getPersons();
Collections.sort(list);
for (Person p: list) {
System.out.println(p.getId() + "\t" + p.getName() + "\t" + p.getAge());
}
System.out.println();
}
public void sortTwo(){
List<Person> list = PersonsSingle.getPersons();
Collections.sort(list, new PersonComparetor());
for (Person p: list) {
System.out.println(p.getId() + "\t" + p.getName() + "\t" + p.getAge());
}
System.out.println();
}
public void shuffling(){
List<Person> list = PersonsSingle.getPersons();
//Collections.shuffling(list);
Collections.shuffle(list);
for (Person p: list) {
System.out.println(p.getId() + "\t" + p.getName() + "\t" + p.getAge());
}
System.out.println();
}
public void reverse(){
List<Person> list = PersonsSingle.getPersons();
Collections.reverse( list );
for (Person p: list) {
System.out.println(p.getId() + "\t" + p.getName() + "\t" + p.getAge());
}
System.out.println();
}
public void fill(){
List<Person> list = PersonsSingle.getPersons();
Collections.fill( list, new Person(6,"傻根", 34) );
for (Person p: list) {
System.out.println(p.getId() + "\t" + p.getName() + "\t" + p.getAge());
}
System.out.println();
}
public void minOne(){
List<Person> list = PersonsSingle.getPersons();
Person p = Collections.min( list );
System.out.println(p.getId() + "\t" + p.getName() + "\t" + p.getAge());
System.out.println();
}
public void minTwo(){
List<Person> list = PersonsSingle.getPersons();
Person p = Collections.min( list, new PersonComparetor() );
System.out.println(p.getId() + "\t" + p.getName() + "\t" + p.getAge());
System.out.println();
}
public void maxOne(){
List<Person> list = PersonsSingle.getPersons();
Person p = Collections.max( list );
System.out.println(p.getId() + "\t" + p.getName() + "\t" + p.getAge());
System.out.println();
}
public void maxTwo(){
List<Person> list = PersonsSingle.getPersons();
Person p = Collections.max( list, new PersonComparetor() );
System.out.println(p.getId() + "\t" + p.getName() + "\t" + p.getAge());
System.out.println();
}
public void lastIndexOfSubList(){
//int count = Collections.lastIndexOfSubList(list,li);
double array[] = {112, 111, 23, 456, 231 };
List list = new ArrayList();
List li = new ArrayList();
for (int i = 0; i < array.length; i++) {
list.add(new Double(array[i]));
}
double arr[] = {111};
String str[] = {"dd","aa","bb","cc","ee"};
for(int j=0;j<arr.length;j++){
li.add(new Double(arr[j]));
}
int locations = Collections.lastIndexOfSubList (list,li);
System.out.println("====="+ locations);
}
public void indexOfSubList(){
//int count = Collections.indexOfSubList(list,li);
double array[] = {112, 111, 23, 456, 231 };
List list = new ArrayList();
List li = new ArrayList();
for (int i = 0; i < array.length; i++) {
list.add(new Double(array[i]));
}
double arr[] = {111};
String str[] = {"dd","aa","bb","cc","ee"};
for(int j=0;j<arr.length;j++){
li.add(new Double(arr[j]));
}
int locations = Collections.indexOfSubList(list,li);
System.out.println("====="+ locations);
}
public void rotate(){
double array[] = {112, 111, 23, 456, 231 };
List list = new ArrayList();
for (int i = 0; i < array.length; i++) {
list.add(new Double(array[i]));
}
Collections.rotate(list,-1);
for (int i = 0; i <list.size(); i++) {
System.out.println("list[" + i + "]=" + list.get(i));
}
}
/**
* @desc
* @param args
*/
public static void main( String[] args )
{
// TODO Auto-generated method stub
CollectionsTest test = new CollectionsTest();
//排序
test.sort();
test.sortOne();
test.sortTwo();
//混排
test.shuffling();
//反转
test.reverse();
//填充
test.fill();
//最小
test.minOne();
test.minTwo();
//最大
test.maxOne();
test.maxTwo();
//
test.lastIndexOfSubList();
//
test.indexOfSubList();
//
test.rotate();
}
}