[1].[代码] [Java]代码
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
class Personsss{
Long age=10l;
public Long getAge() {
return age;
}
public void setAge(Long age) {
this.age = age;
}
}
public class TestC {
public static void main(String[] args) {
List> list=new ArrayList>();
Map map= new HashMap();
Map map3= new HashMap();
map3.put("10","A3型题");
list.add(map3);
map.put("8","A1型题");
list.add(map);
Map map1= new HashMap();
map1.put("9","A2型题");
list.add(map1);
Map map0= new HashMap();
map0.put("0","全部");
list.add(map0);
Map map4= new HashMap();
map4.put("11","A4型题");
list.add(map4);
Map map5= new HashMap();
map5.put("12","B1型题");
list.add(map5);
for(Map mapv:list){
System.out.println(mapv);
}
System.out.println("***********************");
sortmap(list);
for(Map mapv:list){
System.out.println(mapv);
}
//测试排序对象
List dataA=new ArrayList();
System.out.println("******************listA************");
A aa =new A();
aa.setName("aa");
aa.setOrder(1);
A bb = new A();
bb.setName("bb");
bb.setOrder(2);
A cc = new A();
cc.setName("小面");
cc.setOrder(0);
dataA.add(bb);
dataA.add(aa);
dataA.add(cc);
//list中的对象A实现Comparable接口
Collections.sort(dataA);
for(A a:dataA){
System.out.println(a.getName());
}
System.out.println("**********listB***************");
//根据Collections.sort重载方法来实现
List dataB=new ArrayList();
B ab = new B();
ab.setName("ab");
ab.setOrder("1");
B ba = new B();
ba.setName("ba");
ba.setOrder("2");
B bc = new B();
bc.setName("bac");
bc.setOrder("3");
dataB.add(ba);
dataB.add(ab);
dataB.add(bc);
sortListObject(dataB);
for(B b:dataB){
System.out.println(b.getOrder()+":"+b.getName());
}
//利用Collections.sort对HashMap里的键值对按照键进行排序
System.out.println("**********hashMap进行排序***************");
Map maps=new HashMap();
maps.put("a", 9527);
maps.put("z", 7);
maps.put("c", 888);
maps.put("x", 666);
List> mhashMapEntryList =new ArrayList>(maps.entrySet());
System.out.println("-----> 排序前的顺序");
for (int i = 0; i < mhashMapEntryList.size(); i++) {
System.out.println(mhashMapEntryList.get(i));
}
System.out.println("-----> 排序前的顺后");
sortmaplist(mhashMapEntryList);
for (int i = 0; i < mhashMapEntryList.size(); i++) {
System.out.println(mhashMapEntryList.get(i));
}
}
/**
* 主要是对List中的map排序按key值排序,这样排序有一个缺点,每个map里只能有一个key,value值,
* 否则排序不正确,优点是key值不是同一的排序可以用。
* 排序前:{10=A3型题}
{8=A1型题}
{9=A2型题}
{0=全部}
{11=A4型题}
{12=B1型题}
排序后:
{0=全部}
{8=A1型题}
{9=A2型题}
{10=A3型题}
{11=A4型题}
{12=B1型题}
* @param list
* @return
*/
public static void sortmap(List> list){
Collections.sort(list, new Comparator>(){
public int compare(Map o1,Map o2){
Integer s=null;
Integer ss=null;
for(String k:o1.keySet()){
s=Integer.parseInt(k);
}
for(String kk:o2.keySet()){
ss=Integer.parseInt(kk);
}
return s>ss?1:(s==ss?0:-1);
}
});
}
/**
* 直接排序list的可以直接调用集合
* lists中的对象String 本身含有compareTo方法,所以可以直接调用sort方法,按自然顺序排序,即升序排序
* @param list
*/
public static void sortList(List list){
Collections.sort(list);
}
/**根据Collections.sort重载方法来实现
*
* @param list
*/
public static void sortListObject(List list){
Collections.sort(list,new Comparator(){
@Override
public int compare(B b1, B b2) {
//return Integer.parseInt(b1.getOrder())>Integer.parseInt(b2.getOrder())?1:(Integer.parseInt(b1.getOrder())==Integer.parseInt(b2.getOrder())?0:-1);
return b1.getOrder().compareTo(b2.getOrder());
}
});
}
/**
* Demo描述:
* 利用Collections.sort对HashMap里的键值对按照键进行排序
*/
public static void sortmaplist( List> mhashMapEntryList){
Collections.sort(mhashMapEntryList, new Comparator>() {
@Override
public int compare(Map.Entry firstMapEntry,
Map.Entry secondMapEntry) {
return firstMapEntry.getKey().compareTo(secondMapEntry.getKey());
}
});
}
}
class A implements Comparable{
private String name;
private Integer order;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getOrder() {
return order;
}
public void setOrder(Integer order) {
this.order = order;
}
@Override
public String toString() {
return "name is "+name+" order is "+order;
}
@Override
public int compareTo(A a) {
return this.order.compareTo(a.getOrder());
}
}
class B{
private String name;
private String order;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getOrder() {
return order;
}
public void setOrder(String order) {
this.order = order;
}
@Override
public String toString() {
return "name is "+name+" order is "+order;
}
}