<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Bikash Mohapatra on Medium]]></title>
        <description><![CDATA[Stories by Bikash Mohapatra on Medium]]></description>
        <link>https://medium.com/@mbikash.ph?source=rss-788916bb59a4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*tFc2BHVfQjE4jkr9BXnB-A.jpeg</url>
            <title>Stories by Bikash Mohapatra on Medium</title>
            <link>https://medium.com/@mbikash.ph?source=rss-788916bb59a4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 17:37:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mbikash.ph/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Exploring Java 8 Predicate with Real-world Examples]]></title>
            <link>https://medium.com/@mbikash.ph/exploring-java-8-predicate-with-real-world-examples-ff91e4fb53eb?source=rss-788916bb59a4------2</link>
            <guid isPermaLink="false">https://medium.com/p/ff91e4fb53eb</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[java8]]></category>
            <category><![CDATA[java-predicate]]></category>
            <category><![CDATA[predicate-logic]]></category>
            <dc:creator><![CDATA[Bikash Mohapatra]]></dc:creator>
            <pubDate>Sat, 28 Dec 2024 18:59:22 GMT</pubDate>
            <atom:updated>2024-12-28T18:59:22.269Z</atom:updated>
            <content:encoded><![CDATA[<p>Since Java 8 features are frequently asked in interviews also, I have thought of writing about them. You can always access the reading list from here. Along with that I have mention coding examples associated with it.</p><p>Java 8 introduced the java.util.function package, which brought a range of functional interfaces to support functional programming. Among these, the Predicate interface stands out as a powerful tool for evaluating conditions and filtering data.</p><p>A Predicate is a functional interface that accepts a single argument and returns a boolean value. It is typically used in scenarios where you need to test an object against a condition. The interface has a single abstract method :</p><pre>boolean test(T t);</pre><p>Using a Predicate simplifies code by allowing developers to express conditions as lambda expressions. For example, consider a list of integers where you want to filter even numbers:</p><pre>List&lt;Integer&gt; numbers = Arrays.asList(1, 2, 3, 4, 5);<br>numbers.stream()<br>       .filter(n -&gt; n % 2 == 0)<br>       .forEach(System.out::println);</pre><p>we’ll explore how Predicate can be used with a Product class to filter data efficiently. Let’s dive into practical examples based on a Product class with ten fields.</p><h4>Product Class Definition</h4><p>Here’s the Product class we’ll work with:</p><pre>class Product {<br>    private int id;<br>    private String name;<br>    private String category;<br>    private double price;<br>    private String brand;<br>    private String manufacturer;<br>    private int stockQuantity;<br>    private boolean isAvailable;<br>    private double weight;<br>    private String description;<br><br>    public Product(int id, String name, String category, double price, String brand, String manufacturer,<br>                   int stockQuantity, boolean isAvailable, double weight, String description) {<br>        this.id = id;<br>        this.name = name;<br>        this.category = category;<br>        this.price = price;<br>        this.brand = brand;<br>        this.manufacturer = manufacturer;<br>        this.stockQuantity = stockQuantity;<br>        this.isAvailable = isAvailable;<br>        this.weight = weight;<br>        this.description = description;<br>    }<br><br>    public int getId() {<br>        return id;<br>    }<br><br>    public String getName() {<br>        return name;<br>    }<br><br>    public String getCategory() {<br>        return category;<br>    }<br><br>    public double getPrice() {<br>        return price;<br>    }<br><br>    public String getBrand() {<br>        return brand;<br>    }<br><br>    public String getManufacturer() {<br>        return manufacturer;<br>    }<br><br>    public int getStockQuantity() {<br>        return stockQuantity;<br>    }<br><br>    public boolean isAvailable() {<br>        return isAvailable;<br>    }<br><br>    public double getWeight() {<br>        return weight;<br>    }<br><br>    public String getDescription() {<br>        return description;<br>    }<br><br>    @Override<br>    public String toString() {<br>        return &quot;Product{&quot; +<br>                &quot;id=&quot; + id +<br>                &quot;, name=&#39;&quot; + name + &#39;\&#39;&#39; +<br>                &quot;, category=&#39;&quot; + category + &#39;\&#39;&#39; +<br>                &quot;, price=&quot; + price +<br>                &quot;, brand=&#39;&quot; + brand + &#39;\&#39;&#39; +<br>                &quot;, manufacturer=&#39;&quot; + manufacturer + &#39;\&#39;&#39; +<br>                &quot;, stockQuantity=&quot; + stockQuantity +<br>                &quot;, isAvailable=&quot; + isAvailable +<br>                &quot;, weight=&quot; + weight +<br>                &quot;, description=&#39;&quot; + description + &#39;\&#39;&#39; +<br>                &#39;}&#39;;<br>    }<br>}</pre><h4>Use Case: Filtering Products with Predicate</h4><p>We’ll now create a list of Product objects and use Predicate to filter them based on various conditions.</p><h3>1. Filter Products by Price</h3><pre>Predicate&lt;Product&gt; priceGreaterThan500 = product -&gt; product.getPrice() &gt; 500;<br>List&lt;Product&gt; expensiveProducts = productList.stream()<br>        .filter(priceGreaterThan500)<br>        .collect(Collectors.toList());</pre><p>This filters products with a price greater than 500.</p><h3>2. Filter Products by Category</h3><pre>Predicate&lt;Product&gt; categoryFilter = product -&gt; &quot;Category3&quot;.equals(product.getCategory());<br>List&lt;Product&gt; category3Products = productList.stream()<br>        .filter(categoryFilter)<br>        .collect(Collectors.toList());</pre><p>This filters products in category “Category3”.</p><h3>3. Filter Available Products</h3><pre>Predicate&lt;Product&gt; isAvailable = Product::isAvailable;<br>List&lt;Product&gt; availableProducts = productList.stream()<br>        .filter(isAvailable)<br>        .collect(Collectors.toList());</pre><p>This filters products that are available.</p><h3>4. Filter by Brand</h3><pre>Predicate&lt;Product&gt; brandFilter = product -&gt; &quot;Brand1&quot;.equals(product.getBrand());<br>List&lt;Product&gt; brand1Products = productList.stream()<br>        .filter(brandFilter)<br>        .collect(Collectors.toList());</pre><p>This filters products with brand “Brand1”.</p><h3>5. Filter by Stock Quantity</h3><pre>Predicate&lt;Product&gt; stockFilter = product -&gt; product.getStockQuantity() &gt; 20;<br>List&lt;Product&gt; highStockProducts = productList.stream()<br>        .filter(stockFilter)<br>        .collect(Collectors.toList());</pre><p>This filters products with stock quantity greater than 20.</p><h3>6. Filter Products by Weight Range</h3><pre>Predicate&lt;Product&gt; weightRangeFilter = product -&gt; product.getWeight() &gt;= 1.0 &amp;&amp; product.getWeight() &lt;= 3.0;<br>List&lt;Product&gt; mediumWeightProducts = productList.stream()<br>        .filter(weightRangeFilter)<br>        .collect(Collectors.toList());</pre><p>This filters products with a weight between 1.0 and 3.0.</p><h3>7. Filter Products with Description Containing a Word</h3><pre>Predicate&lt;Product&gt; descriptionContains = product -&gt; product.getDescription().contains(&quot;specific word&quot;);<br>List&lt;Product&gt; matchingDescriptionProducts = productList.stream()<br>        .filter(descriptionContains)<br>        .collect(Collectors.toList());</pre><p>This filters products whose description contains “specific word”.</p><h3>8. Combine Predicates for Complex Filtering</h3><pre>Predicate&lt;Product&gt; categoryAndPriceFilter = product -&gt; &quot;Category2&quot;.equals(product.getCategory()) &amp;&amp; product.getPrice() &lt; 300;<br>List&lt;Product&gt; filteredProducts = productList.stream()<br>        .filter(categoryAndPriceFilter)<br>        .collect(Collectors.toList());</pre><p>This filters products in “Category2” with a price less than 300.</p><h3>9. Negate a Predicate</h3><pre>Predicate&lt;Product&gt; notAvailable = Predicate.not(Product::isAvailable);<br>List&lt;Product&gt; unavailableProducts = productList.stream()<br>        .filter(notAvailable)<br>        .collect(Collectors.toList());</pre><p>This filters products that are <strong>not available</strong>.</p><h3>10. Filter by Multiple Conditions</h3><pre>Predicate&lt;Product&gt; complexFilter = product -&gt; &quot;Category4&quot;.equals(product.getCategory()) &amp;&amp;<br>        product.getPrice() &gt; 400 &amp;&amp;<br>        product.getStockQuantity() &lt; 30;<br>List&lt;Product&gt; complexFilteredProducts = productList.stream()<br>        .filter(complexFilter)<br>        .collect(Collectors.toList());</pre><p>This filters products that belong to “Category4”, have a price greater than 400, and stock quantity less than 30.</p><h4>Conclusion</h4><p>Java 8 Predicate simplifies conditional filtering for collections like List by leveraging lambda expressions and method references. In this blog, we explored various use cases for Predicate with the Product class. Try experimenting with these examples to get hands-on experience with functional programming in Java 8!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ff91e4fb53eb" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>