Most efficient way to find the collection of all ids in a collection of entities

83,067

Solution 1

Assuming you have

class Entity {
    final long id;
    final String data;

    public long getId() {
        return id;
    }

    public String getData() {
        return data;
    }

    Entity(long id, String data) {
        this.id = id;
        this.data = data;
    }
}

In Java 8 you can write

Collection<Entity> entities = Arrays.asList(new Entity(1, "one"), 
                  new Entity(11, "eleven"), new Entity(100, "one hundred"));
// get a collection of all the ids.
List<Long> ids = entities.stream()
                         .map(Entity::getId).collect(Collectors.toList());

System.out.println(ids);

prints

[1, 10, 100]

As you can imagine this is rather ugly in Java 7 or less. Note the Entity.getId when applied to map() means call this method on each element.

Now, the real interesting part is you can do this.

List<Long> ids = entities.parallelStream()
                         .map(Entity::getId).collect(Collectors.toList());

In most cases using a parallel stream will hurt performance, but it makes trying it and seeing amazingly easy (possibly too easy ;)


The most efficient way is to have, or build a Map.

Map<Long, Entity> entitiesMap = ...
// get all ids
Collection<Long> addIds = entitiesMap.keySet();

// look up entities by id.
List<Long> ids = ...
List<Entity> matching = new ArrayList<>();
for(Long id: ids)
    matching.add(entitiesMap.get(id));

Solution 2

Most efficient? Basically just iterate and add to the list. You have to look at each item.

Collection<Long> ids = new LinkedList<Long>();
for (Entity e : entities) {
    ids.add(e.id);
}

Or, if you can use Java 1.8, you can do something like:

entities.forEach((e) -> ids.add(e.id));

Solution 3

You won't get anything shorter than:

Collection<Long> ids = new ArrayList<>();
for (Entity e : entities) ids.add(e.getId());

I assume all ways would iterate over the collection

Not necessarily. This creates a collection that is directly backed by the underlying entities collection (future changes to the entities collection appear in the ids collection):

Collection<Long> ids = new AbstractCollection<Long>() {
    @Override
    public int size() {
        return entities.size();
    }

    @Override
    public Iterator<Long> iterator() {
        return new Iterator<Long>() {
            private Iterator<Entity> base = entities.iterator();
            @Override public boolean hasNext() { return base.hasNext(); }
            @Override public Long next() { return base.next().getId(); }
            @Override public void remove() { base.remove(); }
        };
    }
};

Solution 4

I don't know if this is necessarily the most efficient, but for pre-Java 8, I've become fond of using property interfaces as described here: http://blog.cgdecker.com/2010/06/property-interfaces-and-guava.html

As described in the blog post, you would have a simple interface named something like HasId:

public interface HasId {
    long getId();
}

Your Entity class would look like this:

public class Entity implements HasId {
    private long id;    
    private String data;

    public long getId() {
        return id;
    }

    public String getData() {
        return data;
    }
}

and you would have a simple Function like this somewhere:

public class ToId implements Function<HasId, Long> {
    public Long apply(HasId hasId) {
        return hasId.getId();
    }
}

Finally, to make use of it:

Collection<Long> ids = Collections2.transform(entities, new ToId());

This is excessive if you only need it for one thing, but if you have a ton of objects that can sanely implement HasId or other such interfaces, I find it very enjoyable to work with.

Share:
83,067
rapt
Author by

rapt

Updated on August 27, 2020

Comments

  • rapt
    rapt over 3 years

    I have an entity:

    public class Entity
    {
        private long id;    
        private String data;
    
        public long getId() {
            return id;
        }
    
        public String getData() {
            return data;
        }
    }
    

    and a collection of entities:

    Collection<Entity> entities= ...
    

    What is the most efficient way to find the Collection<Long> of all the ids in entities?

  • arshajii
    arshajii almost 10 years
    It would probably be better to use an ArrayList and specify its size (i.e. entities.size()).
  • rapt
    rapt almost 10 years
    Of course, but is there a utility that could do this for me in Guava or Apache Commons?
  • Vishy
    Vishy almost 10 years
    @rapt without closures it would be more code to use a library than to just write a simple loop. Can you use Java 8?
  • Vishy
    Vishy almost 10 years
    In that case forget Guava and Apache Commons and just use streams, it is much easier. Do you have a getter for Id as that would help.
  • Vishy
    Vishy almost 10 years
    @rapt I have updated my answer. if you are using Java 8, you really want to get you head around closures.
  • Vishy
    Vishy almost 10 years
    entities.forEach((e) -> ids.add(e)) is the same as ids.addAll(entries);
  • rapt
    rapt almost 10 years
    and BTW, what would it look like in Java 7? I thought closures were introduced only starting at Java 8?
  • Vishy
    Vishy almost 10 years
    @rapt In Java 7 you would use a loop. It is only due to Java 8's syntactic sugar that it even makes some sense. Note: the loop is still shorter code.
  • kmera
    kmera almost 10 years
    Sorry i meant ids.add(e.id)
  • Míra
    Míra over 5 years
    Equivalent is .map(e -> e.getId()).collect(..)
  • Vishy
    Vishy over 5 years
    @Míra This is slightly shorter due to the length of the class name. I meant if you turned all the variables/class into one letter and counted the symbols involved, the loop would be shorter in terms of symbol complexity.
  • Stefano Groenland
    Stefano Groenland over 3 years
    You are replying to an old question which already has an accepted answer.
  • Víctor Gil
    Víctor Gil over 2 years
    Since Java 16 there is now a better variant to produce an unmodifiable list directly from a stream: Stream.toList() instead of Stream.collect(Collectors.toList())