DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report

Filtering List Items, The Old, The New and the Slick

Amit Raz user avatar by
Amit Raz
·
Jan. 11, 09 · News
Like (0)
Save
Tweet
Share
2.69K Views

Join the DZone community and get the full member experience.

Join For Free

Hi all

I was into filtering items lately :) and I have decided to write about some of the techniques out there starting with the old:

The good old Foreach

Well we have all used this before once you get a list in your hands just iterate through all the items and select the ones you want:

private static List<int> TheOld_FilterPositiveItems(List<int> t)
{
List<int> ret = new List<int>();
foreach (int i in t)
{
if (i > 0)
{
ret.Add(i);
}
}
return ret;
}

I hope you are not using this method. As there are other good methods like this one:

Filtering Using Predicates

This method is much more simple and nice. first we will have to define a predicate that will point to a function that will hold the logic of the filtering. Here is the function that will provide the testing of whether the integer is positive or not:

private static bool intpredicate(int i)
{
if (i > 0)
{
return true;
}
return false;
}

Pretty simple.

 

Here is how we use it to filter all the integers:

private static List<int> FilterPositiveItems2(List<int> t)
{
Predicate<int> pred = new Predicate<int>(intpredicate);
return t.FindAll(pred);
}

That takes care of filtering using predicates. Not a bad way of doing it but here comes the slickest way of filtering:

Filtering Using Lmbda Expressions

This is just an improvement on the previous method instead of using the predicate we will provide List.FindAll with a lambda expression.

We will use the following expression:

n => n>0 which can be translated like this: n where n is larger then 0.

this lambda expression has a return type of boolean so it can be placed instead of the predicate:

private static List<int> TheSlick_FilterPositiveItems(List<int> t)
{
return t.FindAll(n => n > 0);
}

That’s it.

Enjoy

Amit

Published at DZone with permission of Amit Raz. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Spring Boot, Quarkus, or Micronaut?
  • Full Lifecycle API Management Is Dead
  • mTLS Everywere
  • NoSQL vs SQL: What, Where, and How

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: