简体   繁体   English

如何从对象列表中提取K个“最小”元素?

[英]How do I extract the K “smallest” elements from a list of objects?

I'm looping through a List to find a particular entry, then assigning that to a variable and trying to remove it later. 我循环遍历List以查找特定条目,然后将其分配给变量并稍后尝试将其删除。 It's easier to demo than to explain. 演示比解释更容易。

ArrayList<Example> list1 = populate();

Example ex1 = list1.get(0);
Example ex2 = ex1;
list1.remove(ex2);

I know this likely has something to do with Java's inability to handle pointers, but a viable solution would be great. 我知道这可能与Java无法处理指针有关,但一个可行的解决方案会很棒。

Edit: To elaborate, this is a brief example of my code rather than giving you the full thing. 编辑:详细说明,这是我的代码的一个简短示例,而不是给你完整的东西。 What I'm doing is iterating through a list to find the lowest 10 numbers. 我正在做的是遍历列表以找到最低的10个数字。 My technique is to go through the list, find the lowest and add it to another list, then remove that number from the original list and repeat. 我的技术是浏览列表,找到最低值并将其添加到另一个列表,然后从原始列表中删除该号码并重复。 But my list is made of objects which have an int value inside them, rather than a list of integers. 但是我的列表是由在其中包含int值的对象组成的,而不是整数列表。

for(0 to 9){
    for(0 to list.size){
        if(list.get(x) < smallest)
            smallest = list.get(x)
    }
    smallestList.add(smallest);
    list.remove(smallest)
}

I would sort the list. 我会对清单进行排序。 Then, I would create a list with those 10 smallest objects and change the original list list1 to contain the remaining objects. 然后,我将创建一个包含这10个最小对象的列表,并更改原始列表list1以包含其余对象。 Something like: 就像是:

Collection.sort(list1);
ArrayList<Example> yourSmallestElements = (ArrayList<Example>)(list1.sublist(0, 9).clone());
list1.removeAll(yourSmallestElements);

NOTE: I cloned the sublist because sublist() only returns a view of the list list1 , and that's not what you want here. 注意:我克隆了子列表,因为sublist()只返回列表list1视图 ,而这不是你想要的。

Your class Example can implement "Comparable" so that you can define how they need to be compared. 您的类Example可以实现“Comparable”,以便您可以定义它们的比较方式。 You will need to implement the method compareTo() . 您需要实现compareTo()方法。 Something like this: 像这样的东西:

public class Example implements Comparable<Example> {
    private int integerVal = <a value>;

    public int compareTo(Example exampleObject) {
        return exampleObject.integerVal - this.integerVal;
    }   
}

Have a look at this link , more precisely the class that begins as follows: 看看这个链接 ,更确切地说是从以下开始的类:

public class Fruit implements Comparable<Fruit>{

If you want to sort your objects... 如果你想对对象进行排序......

Example e;
int min=-1; // assuming the list has +ve numbers only
for (Example elem : yourList)
{
if ( elem.gtVaribale() <= min ) //assuming you have variable field in your object
{
  e = elem;
  min = elem.getVariable();
}
}
yourList.remove(e);

//repeat this for remaining elements of the list

//you can create another sorted list, and do sortedList.add(e), so that sortedList
//have objects in ascending order (of the variable you want to sort) of objects you had in yourList

This is just a pseudoCode and I have not compiled it. 这只是一个伪代码,我没有编译它。

Here you will have to override the comparable method for class Example. 在这里,您必须覆盖类Example的可比方法。 You have to let compiler know which way it should compare your e variable to its list's elements so as to remove it. 你必须让编译器知道它应该将你的e变量与其列表元素进行比较的方式,以便将其删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM