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 8044055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:06:24+00:00 2026-06-05T05:06:24+00:00

I would like to be able to test if a List contain an object

  • 0

I would like to be able to test if a List contain an object with a given key-value

For example, I would like to do something like Iterables.contains(l2, “lname”, “Jordan”)); instead of having to create all other Map objects like below in l2

//List<String> l = Arrays.asList("Mickael", "Jordan", "His Airness");
//System.out.println(Iterables.contains(l, "Jordan"));

Map<String, String> p1 = new HashMap<String, String>();
p1.put("fname", "Mickael");
p1.put("lname", "Jordan");
p1.put("nname", "His Airness");

Map<String, String> p2 = new HashMap<String, String>();
p2.put("fname", "Paul");
p2.put("lname", "Pierce");
p2.put("nname", "The Truth");
List<Map<String, String>> l2 = Arrays.asList(p1, p2);
Map<String, String> p3 = new HashMap<String, String>();
p3.put("fname", "Mickael"); //
p3.put("lname", "Jordan");
p3.put("nname", "His Airness"); //
System.out.println(Iterables.contains(l2, p3));

I’d like to know if there’s such guava’s function, and not doing a loop on l2 and testing each elt.get(“lname”)

Edit

3 solutions answered: trying to see which one is more perfomant

System.out.println(Iterables.any(l2, withEntry("lname", "Jordan"))); //@axtavt
System.out.println(has("lname", "Jordan")); //@JB
System.out.println(Iterables.any(l2, new KeyValuePredicate("lname", "Jordan"))); //@JB

public static Boolean has(final String key, final String value) {
    return Iterables.any(l2, new Predicate<Map<String, String>>() {
        @Override
        public boolean apply(Map<String, String> input) {
            return input.get(key).equals(value);
        }
    });
}

public static Predicate<Map<String, String>> withEntry(final String key, final String value) {
    return new Predicate<Map<String, String>>() {
        public boolean apply(Map<String, String> input) {
            return value.equals(input.get(key));
        }
    };
}

class KeyValuePredicate implements Predicate<Map<String, String>>{
private String key;
private String value;
public KeyValuePredicate(String key, String value) {
    super();
    this.key = key;
    this.value = value;
}
@Override
public boolean apply(Map<String, String> arg0) {
    // TODO Auto-generated method stub
    return arg0.get(key).equals(value);
}

}
  • 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-06-05T05:06:26+00:00Added an answer on June 5, 2026 at 5:06 am
    return Iterables.any(l2, new Predicate<Map<String, String>>() {
        @Override
        public boolean apply(Map<String, String> input) {
            return input.get("lname").equals("Jordan");
        }
    });
    

    But you’re using maps when you should use objects with properties.

    Of course, if you need to do that multiple times, with various properties, you should transform the predicate into a non-anonymous, reusable class:

    return Iterables.any(l2, new KeyValuePredicate("lname", "Jordan"));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to be able to run a nose test script which accepts
I would like to be able to do something like: AOEU=$(echo aoeu) and have
I would like an EasyMock mock to be able to expect an empty list
I would like to be able to get access to the object that is
Would like to be able to set colors of headings and such, different font
I would like to be able to iterate through the grid elements with a
I would like to be able to hide/un-hide a file in Windows in C++,
I would like to be able to check that the position of the cursor
I would like to be able to have several different zones indicated by the
I would like to be able to edit the Fortran code that is referred

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.