java 集合中怎么将元素倒序排列

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 集合中怎么将元素倒序排列相关的知识,希望对你有一定的参考价值。

public class Text

a.add("sui yue weu ");
a.add("guang hui sui yue ");
a.add("hai kuo tian kong");......


怎样倒序排列为 hai kuo tian kong
guang hui sui yue
sui yue weu

参考技术A 方法一:实现Comparable接口排序package collsort.comparable;
package com.cvicse.sort.comparable;

public class Cat implements Comparable<Cat>
private int age;
private String name;

public Cat(int age, String name)
this.age = age;
this.name = name;


public int getAge()
return age;


public void setAge(int age)
this.age = age;

......
public int compareTo(Cat o)
return this.getAge() - o.getAge();

......

通过实现Comparable接口实现个性化排序测试。排序测试,Collection.sort(list)升序排列Collections.sort(list, Collections.reverseOrder());降序排列;Collections.reverse(list);反转排序,先输出列表最后一个元素
public class TestComparable
public static void main(String args[])
test();
test2();

public static void test()
......
List<Cat> listCat1 = new ArrayList<Cat>();
Cat cat1 = new Cat(34, "hehe");
Cat cat2 = new Cat(12, "haha");
Cat cat3 = new Cat(23, "leizhimin");
Cat cat4 = new Cat(13, "lavasoft");
listCat1.add(cat1);
listCat1.add(cat2);
listCat1.add(cat3);
......
System.out.println("调用Collections.sort(List<T> list)listCat2升序排序:");
Collections.sort(listCat1);
System.out.println("降序排列元素:");
Collections.sort(listCat1, Collections.reverseOrder());
System.out.println("Collections.reverse 从列表中最后一个元素开始输出:");
Collections.reverse(listCat1);
......

/**
* 针对数组的排序
*/
public static void test2()
String[] strArray = new String[] "z", "a", "C" ;
System.out.println("数组转换为列表");
List<String> list = Arrays.asList(strArray);
System.out.println("顺序排序列表");
Collections.sort(list);
System.out
.println("按String实现的Comparator对象String.CASE_INSENSITIVE_ORDER排序----");
Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
System.out.println("倒序排序列表");
Collections.sort(list, Collections.reverseOrder());
......


方法二:实现Comparator接口排序
public class Person
private int age;
private String name;
......
public int getAge()
return age;

public void setAge(int age)
this.age = age;

......

实现了Comparator接口,重写了compare方法
import java.util.Comparator;
public class PersonComparator implements Comparator<Person>
public int compare(Person o1, Person o2)
return o1.getAge() - o2.getAge();


测试方法
public class TestComparator
public static void main(String args[])
test1();

public static void test1()
System.out.println("升序排序测试:");
List<Person> listPerson = new ArrayList<Person>();
Person person1 = new Person(34, "lavasoft");
Person person2 = new Person(12, "lavasoft");
Person person3 = new Person(23, "leizhimin");
Person person4 = new Person(13, "sdg");
listPerson.add(person1);
listPerson.add(person2);
listPerson.add(person3);
Comparator<Person> ascComparator = new PersonComparator();
System.out.println("排序后集合为:");
// 利用Collections类静态工具方法对集合List进行排序
Collections.sort(listPerson, ascComparator);
System.out.println("\n降序排序测试:");
// 从升序排序对象产生一个反转(降序)的排序对象
Comparator<Person> descComparator = Collections
.reverseOrder(ascComparator);
System.out.println("利用反转后的排序接口对象对集合List排序并输出:");
Collections.sort(listPerson, descComparator);
outCollection(listPerson);

参考技术B 方法一:实现Comparable接口排序package collsort.comparable;
package com.cvicse.sort.comparable;

public class Cat implements Comparable<Cat>
private int age;
private String name;

public Cat(int age, String name)
this.age = age;
this.name = name;


public int getAge()
return age;
参考技术C import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Demo3

/**
* @param args
*/
public static void main(String[] args)
List list=new ArrayList();
list.add("jack");
list.add("rose");
list.add("lucy");
reverse(list);



public static void reverse(List list)
Collections.reverse(list);
for (int i = 0; i < list.size(); i++)
System.out.println(list.get(i));



参考技术D import java.util.ArrayList;
import java.util.Collections;

public class Test

public static void main(String[] args)

ArrayList<String> a = new ArrayList<String>();

a.add("sui yue weu ");
a.add("guang hui sui yue ");
a.add("hai kuo tian kong");

Collections.reverse(a);// 将ArrayLista中的元素进行倒序

for (String str : a)
System.out.println(str);



第5个回答  2008-07-07 不是有个函数吗?
用数组的啊

将list集合的元素按照添加顺序的倒序进行排列取出

1.方法

Collections.reverse(list);

 

 

2.代码示例

/**
     * 从redis中将现场状态的记录全部取出
     * @param aucId
     * @return
     */
    @RequestMapping(value = "/findAllAuctionLocalStatementRecord", method = RequestMethod.POST)
    @ResponseBody
    public List<Object> findAllAuctionLocalStatementRecord(String aucId) {
        if (logger.isDebugEnabled()) {
            logger.debug("findAllAuctionLocalStatementRecord, aucId:{}", aucId);
        }
        
        List<Object> list = redisTemplateListDTO.opsForValue().get("auctionLocalStatement"+aucId);
        Collections.reverse(list);//将list集合倒序排列取出,时间最新的在最上面
        
        return list;
    }

 

以上是关于java 集合中怎么将元素倒序排列的主要内容,如果未能解决你的问题,请参考以下文章

【java】集合List里面的元素排列

Java-集合(没做出来)第四题 (List)写一个函数reverseList,该函数能够接受一个List,然后把该List 倒序排列。 例如: List list = new ArrayList(

day5 元组列表字典和集合

python 怎么将一个数组逆序输出?

java代码中 单表查询出的list集合 怎么读写到redis中

全排列