您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關Android中任實現將 List中的對象以字段排序,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
在Android開發中,有時我們需要對一個對象的集合按照某一個字段進行排序,
Bean
public class Student { private int studentId; private String studentName; private int age; public Student(int studentId , String studentName, int age){ this.studentId=studentId; this.studentName=studentName; this.age=age; } public int getStudentId() { return studentId; } public void setStudentId(int studentId) { this.studentId = studentId; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
實現排序
實現排序比較類 Comparator ,里面實現排序規則。
public class test { /** * @param args */ public static void main(String[] args) { Student stu1 = new Student (1,"zhangsan",28); Student stu2 = new Student (2,"zhagnsan",19); Student stu3 = new Student (3,"wangwu",19); Student stu4 = new Student (4,"wangwu",19); Student stu5 = new Student (5,"zhaoliu",18); ArrayList<Student> list = new ArrayList<Student>(); list.add(stu1); list.add(stu2); list.add(stu3); list.add(stu4); list.add(stu5); //排序規則,這里是以年齡先排序,如果年齡相同 Comparator<Student> comparator = new Comparator<Student>() { public int compare(Student s1, Student s2) { // 先排年齡 if (s1.getAge() != s2.getAge()) { return s1.getAge() - s2.getAge(); } else if (!s1.getStudentName().equals(s2.getStudentName())) { // 年齡相同則按姓名排序 return s1.getStudentName().compareTo(s2.getStudentName()); } else { // 姓名也相同則按學號排序 return s1.getStudentId() - s2.getStudentId(); } } }; //這里就會自動根據規則進行排序 Collections.sort(list,comparator); for(int i=0;i<list.size();i++){ Student stu=list.get(i); System.out.println("年齡:"+stu.getAge()+" 姓名:"+stu.getStudentName()+" 學號:"+stu.getStudentId()); } } }
排序結果
結果:
年齡:18 姓名:zhaoliu 學號:5
年齡:19 姓名:wangwu 學號:3
年齡:19 姓名:wangwu 學號:4
年齡:19 姓名:zhagnsan 學號:2
年齡:28 姓名:zhangsan 學號:1
也可以想下面這樣寫:
Collections.sort(list, new Comparator<GoodsBean>() { @Override public int compare(GoodsBean bean1, GoodsBean bean2) { if (Integer.valueOf(bean1.getScore()).compareTo(Integer.valueOf(bean2.getScore())) == 0) { return Integer.valueOf(bean1.getRecommend_num()).compareTo(Integer.valueOf(bean2.getRecommend_num())); } else { return Integer.valueOf(bean1.getScore()).compareTo(Integer.valueOf(bean2.getScore())); } } });
這樣就可以對一個集合中的數據各種排序了。
補充知識:java利用映射表名稱反射創建實體類并賦屬性值
1.hibernate中首先進行初始化,將對應的表名和類名以鍵值對的方式存放到map中
private Map<String, String> mappings;//全局變量 /** * 根據數據庫表名獲取實體類 */ public void initMappings() { if (mappings == null) { mappings = new HashMap<String, String>(); SessionFactory factory = this.getSessionFactory(); Map metaMap = factory.getAllClassMetadata(); for (String key : (Set<String>) metaMap.keySet()) { AbstractEntityPersister classMetadata = (AbstractEntityPersister) metaMap.get(key); String tableName = classMetadata.getTableName().toLowerCase(); int index = tableName.indexOf("."); if (index >= 0) { tableName = tableName.substring(index + 1); } String className = classMetadata.getEntityMetamodel().getName(); mappings.put(tableName, className); } } }
2.調用方法,傳入表名得到對應的實體類名
public String getEntityNameByTableName(String tableName) { initMappings(); return mappings.get(tableName); }
3.根據實體類名創建實體類
/** *listobj:要賦的屬性值集合,順序要和實體類屬性順序一致 */ public Object getByReflect(String tableName, List listobj)throws Exception { Class<?> model = Class.forName(tableName); Object object = new Object(); if (model != null) { Field[] field = model.getDeclaredFields(); String[] modelName = new String[field.length]; String[] modelType = new String[field.length]; object = model.newInstance(); Method m = null; for (int i = 1; i <field.length ; i++) { String name = field[i].getName(); Object value = null; name = name.substring(0, 1).toUpperCase() + name.substring(1); String type = field[i].getGenericType().toString(); if (type.equals("class java.lang.String")) { m = model.getDeclaredMethod("set" + name, String.class); if(listobj.get(i - 1) instanceof Double){ Double d=(Double) listobj.get(i-1); value=String.valueOf(d); }else{ value =(String)listobj.get(i - 1); } } if (type.equals("class java.lang.Integer")) { m = model.getDeclaredMethod("set" + name, Integer.class); Double d = (Double) listobj.get(i - 1); value = Integer.valueOf(d.intValue()); } if (type.equals("class java.lang.Short")) { m = model.getDeclaredMethod("set" + name, Short.class); value = (Short) listobj.get(i - 1); } if (type.equals("class java.lang.Float")) { m = model.getDeclaredMethod("set" + name, Float.class); value = (Float) listobj.get(i - 1); } if (type.equals("class java.lang.Double")) { m = model.getDeclaredMethod("set" + name, Double.class); value = (Double) listobj.get(i - 1); } if (type.equals("class java.lang.Boolean")) { m = model.getDeclaredMethod("set" + name, Boolean.class); value = (Boolean) listobj.get(i - 1); } if (m != null) { m.invoke(object, value); } } } return object; }
看完上述內容,你們對Android中任實現將 List中的對象以字段排序有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。