简体   繁体   中英

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. 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.

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. 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.

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. 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.

Your class Example can implement "Comparable" so that you can define how they need to be compared. You will need to implement the method 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. You have to let compiler know which way it should compare your e variable to its list's elements so as to remove it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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