Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7588669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:58:47+00:00 2026-05-30T19:58:47+00:00

I need to extract a number of unique elements from a collection given a

  • 0

I need to extract a number of unique elements from a collection given a certain criteria. E.g. if I have a collection with [ (a1,t1,v1), (a2,t2,v2),…,(an,tx,vy) ]
I would like to get a1, t5, v8.

With Guava, I can have a Predicate for each (e.g. IsA1 implements Predicate …) and then use Iterables.find(collection, predicate) to locate each element. That’s ok, but it potentially loops through the whole collection for each predicate.

I could combine the predicates into one using Predicates.or(…) and use Iterables.filter, but the I’ll then have to search the resulting iterable for each single particular element I need.

What I would like to have is a function -let’s call it “locate”- that would take the element collection, a list of predicates and produce a map of (predicate -> element)

<T> Map<Predicate<? super T>, T> locate(Collection<T>, Collection<Predicate<? super T>)

I’ve rolled my own as I could not find it, but it’s such a common case that I think I just didn’t find it yet.

My version for reference:

public static <T> Map<Predicate<? super T>,T> locateUnique(Iterable<T> col, Collection<Predicate<? super T>> predicates) {
    HashMap<Predicate<? super T>, T> result = Maps.newHashMap();
        for (T t:col) {
            for (Predicate<? super T> p:predicates) {
                if (p.apply(t)){
                    result.put(p, t);
                }
            }
        }
    return result;
}

Is there a out-of-the-box way to do this? I guess there should be 2 versions for completeness:

locateUnique(...) -> Map<Predicate,T>
locate(...) -> MultiMap<Predicat,T> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T19:58:48+00:00Added an answer on May 30, 2026 at 7:58 pm

    Guava team member here. The preferred way to do this is exactly the way you describe — rolling your own method — although depending on the equals method of Predicate is a little risky.

    I’m pretty confident that this is nowhere near as common a case as you describe, though, given that we’re already pretty stingy on functional idioms.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have HTML that I need to extract a part number from, the HTML
I need to extract the ID number from a string. The results are given
I have a required where i need to extract a number from a string
I need to extract a set number of lines from a file given the
I need to extract the number from the following simple string: base:873 where the
I have the following string: a:2:{s:4:user;b:1;s:6:userid;s:2:48;} What I need to do is extract number
I need to extract data from a third party system (I have no influence
I would like to extract first number, found in a path string. Some examples
i am no RegEx expert. I need to extract a certain number out of
I need to extract a single variable number from a string. The string always

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.