【问题标题】:Order two unequal list issue订单两个不等列表问题
【发布时间】:2012-09-12 08:39:55
【问题描述】:

我们如何对两个大小不等的列表以及 pojo 类字段未实现 equals/hashcode 方法的位置进行排序。我无法根据 list1 顺序对列表进行排序。我希望列表中发生的顺序具有“t”然后是“s”。有没有可能实现这个场景?

Field f=new Field();
f.setKey("s");
f.setValue("he");

Field f1=new Field();
f1.setKey("t");
f1.setValue("she");

List<Field> list = new ArrayList<Field>();
list.add(f);
list.add(f1);

Field f2=new Field();
f2.setKey("t");
f2.setValue("he");

Field f3=new Field();
f3.setKey("s");
f3.setValue("she");

Field f4=new Field();
f4.setKey("u");
f4.setValue("she");

List<Field> list1 = new ArrayList<Field>();
list1.add(f2);
list1.add(f3);
list1.add(f4);

【问题讨论】:

    标签: java sorting collections arraylist


    【解决方案1】:

    你可以使用Collections.sort(list, new Comparator&lt;Field&gt;() { /* ... */ });

    Javadoc

    【讨论】:

    • 我尝试过使用 final Map indices = new HashMap(); for (int i = 0; i () { public int compare(FP o1, FP o2) { int index1 = (Integer) (indices.containsKey(o1) ? indices .get(o1) : +1) ; int index2 = (Integer) (indices.containsKey(o2) ? indices .get(o2) : +1); return index1 - index2; } });不工作
    • 你能检查一下吗
    【解决方案2】:

    没有内置方法可以实现您想要的,但您可以通过提供自己的比较器,使用Collections.sort 轻松做到这一点:

    Collections.sort(list, new Comparator<Field>() {
        @Override
        public int compare(Field f1, Field f2) {
            // compare your fields here and
            //     return -1 if f2 should be before f2
            //     return 0 if they are the same
            //     return 1 if f1 should be after f2
    
            return ...;
        }
    });
    

    【讨论】:

    • final Map indices = new HashMap(); for (int i = 0; i () { public int compare(FP o1, FP o2) { int index1 = (Integer) (indices.containsKey(o1) ? indices .get(o1) : +1) ; int index2 = (Integer) (indices.containsKey(o2) ? indices .get(o2) : +1); return index1 - index2; } });不适合我 FP 是一个字段
    • @pars 为什么要使用哈希图?我以为你在处理列表?另请注意,哈希图未排序。如果需要对它们进行排序,则必须使用 SortedMap。
    • 是的,我正在处理列表,如何修改上面的代码进行排序,它不起作用
    • @pars 但是地图从何而来?您的 compare 方法似乎是错误的。列出您的比较规则并确保该方法解决所有问题。
    • 我在比较之前将列表添加到地图中,我没有得到这个,请您编辑并显示给我
    【解决方案3】:

    试试这样的:

    import java.util.HashMap; 
    import java.util.Map.Entry; 
    public class HashMapComparison 
    {     
     public static void main(String[] args) 
     {        
     HashMap<Integer, String> map1 = new HashMap<Integer, String>();   
     map1.put("s", "he");     
     map1.put("t", "she");    
     HashMap<Integer, String> map2 = new HashMap<Integer, String>();  
     map2.put("t", "he");     
     map2.put("s", "she");  
     map2.put("u", "she"); 
     HashMap<Integer, String> orderedMap1 = new HashMap<Integer, String>();  
     for (Entry<Integer, String> entry : map2.entrySet()) {      
     Integer key = entry.getKey();        
     for(Entry<Integer, String> entry : map1.entrySet()){
     if(key.equals(entry.getKey()){
       orderedMap1.put(entry);
     }
     }
     }     
     } 
    

    【讨论】:

    • 你能解释一下列表吗
    猜你喜欢
    • 1970-01-01
    • 2023-03-13
    • 2012-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-11
    相关资源
    最近更新 更多