java 不遍历list怎么排序 排序

ㄓㄤㄑㄧㄤ
先定义一个实体类
@AllArgsConstructor
@NoArgsConstructor
public class Human {
下面的操作都基于这个类来进行操作。这里面使用了类库,它用注解的方式实现了基本的get和set等方法,让代码看起来更加的优雅。
JAVA8之前的List排序操作
在Java8之前,对集合排序只能创建一个匿名内部类
new Comparator&Human&() {
public int compare(Human h1, Human h2) {
return h1.getName().compareTo(h2.getName());
下面是简单的对Humans进行排序(按名称正序)
public void testSortByName_with_plain_java() throws Exception {
ArrayList&Human& humans = Lists.newArrayList(
new Human("tomy", 22),
new Human("li", 25)
Collections.sort(humans, new Comparator&Human&() {
public int compare(Human h1, Human h2) {
return h1.getName().compareTo(h2.getName());
Assert.assertThat(humans.get(0), equalTo(new Human("li", 25)));
使用Lambda的List排序
使用JAVA8函数式方式的比较器
(Human h1, Human h2) -& h1.getName().compareTo(h2.getName())
下面是使用JAVA8函数式的比较的例子
public void testSortByName_with_lambda() throws Exception {
ArrayList&Human& humans = Lists.newArrayList(
new Human("tomy", 22),
new Human("li", 25)
humans.sort((Human h1, Human h2) -& h1.getName().compareTo(h2.getName()));
Assert.assertThat("tomy", equalTo(humans.get(1).getName()));
没有类型定义的排序
对于上面的表达式还可以进行简化,JAVA编译器可以根据上下文推测出排序的类型:
(h1, h2) -& h1.getName().compareTo(h2.getName())
简化后的比较器是这样的:
public void testSortByNameSimplify_with_lambda() throws Exception {
ArrayList&Human& humans = Lists.newArrayList(
new Human("tomy", 22),
new Human("li", 25)
humans.sort((h1, h2) -& h1.getName().compareTo(h2.getName()));
Assert.assertThat("tomy", equalTo(humans.get(1).getName()));
使用静态方法引用
JAVA8还可以提供使用Lambda表达式的静态类型引用,我们在Human类增加一个静态比较方法,如下:
public static int compareByNameThenAge(Human h1, Human h2) {
if (h1.getName().equals(h2.getName())) {
return Integer.compare(h1.getAge(), h2.getAge());
return h1.getName().compareTo(h2.getName());
然后就可以在humans.sort使用这个引用
public void testSort_with_givenMethodDefinition() throws Exception {
ArrayList&Human& humans = Lists.newArrayList(
new Human("tomy", 22),
new Human("li", 25)
humans.sort(Human::compareByNameThenAge);
Assert.assertThat("tomy", is(equalTo(humans.get(1).getName())));
使用单独的Comparator
JAVA8已经提供了很多方便的比较器供我们使用,比如Comparator.comparing方法,所以可以使用Comparator.comparing方法来实现根据Human的name进行比较的操作:
public void testSort_with_givenInstanceMethod() throws Exception {
ArrayList&Human& humans = Lists.newArrayList(
new Human("tomy", 22),
new Human("li", 25)
Collections.sort(humans, Comparator.comparing(Human::getName));
Assert.assertThat("tomy", equalTo(humans.get(1).getName()));
JDK8中也提供了一个支持倒序排序的方法方便我们更快的进行倒序
public void testSort_with_comparatorReverse() throws Exception {
ArrayList&Human& humans = Lists.newArrayList(
new Human("tomy", 22),
new Human("li", 25)
Comparator&Human& comparator = (h1, h2) -& h1.getName().compareTo(h2.getName());
humans.sort(comparator.reversed());
Assert.assertThat("tomy", equalTo(humans.get(0).getName()));
使用多个条件进行排序
Lambda提供了更复杂的表达式,还可以先对name排序再根据age进行排序:
public void testSort_with_multipleComparator() throws Exception {
ArrayList&Human& humans = Lists.newArrayList(
new Human("tomy", 22),
new Human("li", 25)
Comparator&Human& comparator = (h1, h2) -& {
if (h1.getName().equals(h2.getName())) {
return Integer.compare(h1.getAge(), h2.getAge());
return h1.getName().compareTo(h2.getName());
humans.sort(comparator.reversed());
Assert.assertThat("tomy", equalTo(humans.get(0).getName()));
使用多个条件进行排序-组合的方式
Comparator对这种组合的排序有更优雅实现,从JDK8开始,我们可以使用链式操作进行复合操作来构建更复杂的逻辑:
public void testSort_with_multipleComparator_composition() throws Exception {
ArrayList&Human& humans = Lists.newArrayList(
new Human("tomy", 22),
new Human("tomy", 25)
humans.sort(Comparator.comparing(Human::getName).thenComparing(Human::getAge));
Assert.assertThat(humans.get(0), equalTo(new Human("tomy", 22)));
JDK8真的是一个非常值得我们学习的版本,它提供了Lambda表达式,带来了函数式编程的理念,让JAVA代码更优雅。所有的完整的代码在
原文:http://www.cnblogs.com/tomyLi/p/JAVA8rang-dai-ma-geng-you-ya-zhiList-pai-xu.html
阅读(...) 评论()
哈哈,页脚部分。*&根据order对User排序
public&class&User&implements&Comparable&User&{
&&&&private&String&
&&&&private&Integer&
&&&&public&String&getName()&{
&&&&&&&&return&
&&&&public&void&setName(String&name)&{
&&&&&&&&this.name&=&
&&&&public&Integer&getOrder()&{
&&&&&&&&return&
&&&&public&void&setOrder(Integer&order)&{
&&&&&&&&this.order&=&
&&&&public&int&compareTo(User&arg0)&{
&&&&&&&&return&this.getOrder().compareTo(arg0.getOrder());
测试一下:
public&class&Test{
&&&&public&static&void&main(String[]&args)&{
&&&&&&&&User&user1&=&new&User();
&&&&&&&&user1.setName("a");
&&&&&&&&user1.setOrder(1);
&&&&&&&&User&user2&=&new&User();
&&&&&&&&user2.setName("b");
&&&&&&&&user2.setOrder(2);
&&&&&&&&List&User&&list&=&new&ArrayList&User&();
&&&&&&&&//此处add&user2再add&user1
&&&&&&&&list.add(user2);
&&&&&&&&list.add(user1);
&&&&&&&&Collections.sort(list);
&&&&&&&&for(User&u&:&list){
&&&&&&&&&&&&System.out.println(u.getName());
输出结果如下
第二种方法是根据Collections.sort重载方法来实现,例如:
*&根据order对User排序
public&class&User&{&//此处无需实现Comparable接口
&&&&private&String&
&&&&private&Integer&
&&&&public&String&getName()&{
&&&&&&&&return&
&&&&public&void&setName(String&name)&{
&&&&&&&&this.name&=&
&&&&public&Integer&getOrder()&{
&&&&&&&&return&
&&&&public&void&setOrder(Integer&order)&{
&&&&&&&&this.order&=&
主类中这样写即可:
public&class&Test{
&&&&public&static&void&main(String[]&args)&{
&&&&&&&&User&user1&=&new&User();
&&&&&&&&user1.setName("a");
&&&&&&&&user1.setOrder(1);
&&&&&&&&User&user2&=&new&User();
&&&&&&&&user2.setName("b");
&&&&&&&&user2.setOrder(2);
&&&&&&&&List&User&&list&=&new&ArrayList&User&();
&&&&&&&&list.add(user2);
&&&&&&&&list.add(user1);
&&&&&&&&Collections.sort(list,new&Comparator&User&(){
&&&&&&&&&&&&public&int&compare(User&arg0,&User&arg1)&{
&&&&&&&&&&&&&&&&return&arg0.getOrder().compareTo(arg1.getOrder());
&&&&&&&&&&&&}
&&&&&&&&});
&&&&&&&&for(User&u&:&list){
&&&&&&&&&&&&System.out.println(u.getName());
输出结果如下
前者代码结构简单,但是只能根据固定的属性排序,后者灵活,可以临时指定排序项,但是代码不够简洁
择优用之。
不错啊`~~学习
Collections这个工具类,很强大,写的不错,支持你
搞个可构造方法就方便看
简之。。。List&User& list = Lists.newArrayList(new User(1,&a&),new User(2,&b&));Collections.sort(list,(User a,User b)-&(a.getOrder().compareTo(b.getOrder()));print(list);
分Collections.sort()
接口可以被实例化????
@任起东 实际上是java生成了一个类出来。java ArrayList 降序排序问题_百度知道
java ArrayList 降序排序问题
题目描述:
利用ArrayList动态数组完成N个学生数据(学号是字符串类型、成绩是整型)的输入、输出。
输入有N+1行:
第一行的整数N表示学生数目;
以下N行是N个学生的数据,每行中第一个是表示学号的字符串,第二个是表示学生成绩的整数。
我有更好的答案
public static void main(String[] args) {
ArrayList&Integer& arr=new ArrayList&&();
arr.add(89);
arr.add(78);
arr.add(56);
arr.add(92);
arr.add(76);
//升序排列;
Collections.sort(arr);
//逆序输出
Collections.reverse(arr);
for(int i=0;i&arr.size();i++){
//这就是你要的效果
System.out.println(&0&+i+&
&+arr.get(i));
采纳率:22%
java.util.collections下有一个sort方法可以对list集合排序,collections就是一个帮助类,lz可以去了解下!
为您推荐:
其他类似问题
arraylist的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。}

我要回帖

更多关于 arraylist倒序排序 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信