How to find an object in an ArrayList by property JAVA 8

How to find an object in an ArrayList by property JAVA 8

This is how the data would look like, I wanted to fetch classCode = PP

{
  "unitSet": "IMPERIAL",
  "_class": "com.model.UnitSet",
  "conversionFactors": [
    {
      "classCode": "GF",
      "unit": "ft3/min",
      "decimals": 2,
      "factor": "2118.8802"
    },
    {
      "classCode": "PP",
      "unit": "psi",
      "decimals": 1,
      "factor": "0.000145038"
    },
    {
      "classCode": "VE",
      "unit": "ft/min",
      "decimals": 1,
      "factor": "196.8503937"
    },
}

public class UnitSet {
    private String unitSet;    private List conversionFactors;
}



public class ConversionFactor {
    private String classCode;    private String unit;    private Integer decimals;    private Float factor;
}

This is how I got the data using Java Streams

Optional conversionFactor =
        unitSet.getConversionFactors()
                .stream()
                .filter(p -> p.getClassCode().equals(("PP")))
                .findFirst();



Hope this helps


No comments:

 Python Basics How to check the version of Python interpreter mac terminal

Popular in last 30 days