对ArrayList中的Person对象按照先年龄从大到小,相同年龄的再按照姓名(姓名是英文的)的字母顺序进行排序.
ListDemo2.java
-----------------
package com.fs.test; import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List; /*
* Collections可以针对ArrayList存储基本包装类的元素排序,存储自定义对象可不可以排序呢?
*/
public class Listdemo2 {
public static void main(String[] args) {
// 创建集合对象
List<Person2> list = new ArrayList<Person2>(); Person2 s1 = new Person2("Bobby",18);
Person2 s2 = new Person2("Mike",20);
Person2 s3 = new Person2("Jim",35);
Person2 s4 = new Person2("Zoe",27);
Person2 s5 = new Person2("Dave",18);
Person2 s6 = new Person2("Madison",35);
Person2 s7 = new Person2("Caden",40);
Person2 s8 = new Person2("Ava",23);
Person2 s9 = new Person2("Noah",25);
Person2 s10 = new Person2("Isabella",27);
Person2 s11 = new Person2("Lucas",25); // 添加元素对象
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
list.add(s5);
list.add(s6);
list.add(s7);
list.add(s8);
list.add(s9);
list.add(s10);
list.add(s11); // 排序
// 自然排序
// Collections.sort(list);
// 比较器排序
// 如果同时有自然排序和比较器排序,以比较器排序为主
Collections.sort(list, new Comparator<Person2>() {
@Override
public int compare(Person2 s1, Person2 s2) {
if(s1.getAge()-(s2.getAge())!=0){
return s1.getAge()-(s2.getAge());
}else{
return s1.getName().compareTo(s2.getName());
}
}
}); // 遍历集合
for (Person2 s : list) {
System.out.println(s.getName() + "---" + s.getAge());
}
}
} Person2,java
-------------------package com.fs.test; public class Person2 {
private String name;
private int age; public Person2() {
super();
} public Person2(String name, int age) {
super();
this.name = name;
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
}
}
对ArrayList中的Person对象按照先年龄从大到小,相同年龄的再按照姓名(姓名是英文的)的字母顺序进行排序.的更多相关文章
- 在Main中定义student的结构体,进行年龄从大到小依次排序录入学生信息。(结构体的用法以及冒泡排序)
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...
- ht-8 对arrayList中的自定义对象排序( Collections.sort(List<T> list, Comparator<? super T> c))
package com.iotek.set; import java.util.ArrayList; import java.util.Collections; import java.util.Co ...
- 37-Arrays.sort() 由大到小排序 和 对象数组排序
1. 由大到小排序: 2. 对象数组排序: 1. 由大到小排序: 注意:必需是Integer 类型的数组!!! 方法一: import java.util.Arrays; import java.ut ...
- ArrayList list = new ArrayList()在这个泛型为Integer的ArrayList中存放一个String类型的对象
java面试要点---ArrayList list = new ArrayList(); 在这个泛型为Integer的ArrayList中存放一个String类型的对象. ArrayList list ...
- Java ArrayList中对象的排序 (Comparable VS Comparator)
我们通常使用Collections.sort()方法来对一个简单的数据列表排序.但是当ArrayList是由自定义对象组成的,就需要使用comparable或者comparator接口了.在使用这两者 ...
- Iterator的remove方法可保证从源集合中安全地删除对象(转)
如果对正在被迭代的集合进行结构上的改变(即对该集合使用add.remove或clear方法),那么迭代器就不再合法(并且在其后使用该迭代器将会有ConcurrentModificationExcept ...
- ArrayList中元素去重问题
如题所示,如果一个ArrayList中包含多个重复元素,该如何去重呢? 思路一以及实现: 声明2个ArrayList,分别为listA与listB ,listA为待去重list ,listB 保存去重 ...
- java中对集合对象list的几种循环访问
java中对集合对象list的几种循环访问的总结如下 1 经典的for循环 public static void main(String[] args) { List<String> li ...
- 去除List集合中的重复对象,Map遍历代码
/*** * 去除List<PartsInfoDTO>列表中的重复对象 ~!! * @param list * @return */ public static List<Parts ...
随机推荐
- ILSpy C# language support status
C# language support status Asynchronous methods 已经支持 Generalized async return types 还不支持 Async main ...
- pytest.fixture和普通函数调用
普通函数嗲用def one(): a="aaaaaaaaaaa" return a def test_one(): s=one() print (s) test_one() pyt ...
- LC 562. Longest Line of Consecutive One in Matrix
Given a 01 matrix M, find the longest line of consecutive one in the matrix. The line could be horiz ...
- Centos7.4 yum 安装MariaDB
#系统及版本选择:https://downloads.mariadb.org/mariadb/repositories/#mirror=tunavim /etc/yum.repos.d/MariaDB ...
- MySQL 对 IP 字段的排序问题
MySQL 对 IP 字段的排序问题 问题描述 想对一张带有 IP 字段的表,对 IP 字段进行升序排序,方便查看每个段的 IP 信息. 表结构和表数据如下: SET NAMES utf8mb4; ; ...
- Python——作业12(选做)选中矩阵的每行或每列画出对应的折线图(python programming)
import os import platform import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5 ...
- (“(null)” is of a model that is not supported by this version of Xcode. Please...)
真机测试遇到以下问题: (还以为手机不支持Xcode的版本呢) 解决方法: 发现只要将XCode重启后就可以真机运行了,碰见这个问题的朋友可以试下,我反正是被坑了半小时...
- configmap使用-完整的configmap文档
转发 https://www.jianshu.com/p/cf3e2218f283 转发 https://www.kubernetes.org.cn/3138.html 注意:configmap不用也 ...
- react-redux provider组件
provider组件概念图 react-redux provider组件概念图 provider组件的作用 provider包裹在根组件外层,使所有的子组件都可以拿到state.示例: impor ...
- python-Web-django-自定义标签
简化:@register.simple_tag def current_time(token): return datetime.datetime.now().strftime(str(token)) ...