How to Search for Product with least cost from List? I have a List of Speakers in List. I want to get the speaker with the least cost.
Speakers.java
package com.mugil.bean; public class Speakers { private Integer speakerID; private String companyName; private Integer price; private String sellerName; public Speakers(Integer pSpeakerId, String pcompanyName, Integer pPrice, String psellerName) { this.speakerID = pSpeakerId; this.companyName = pcompanyName; this.price = pPrice; this.sellerName = psellerName; } public int getSpeakerID() { return speakerID; } public void setSpeakerID(int speakerID) { this.speakerID = speakerID; } public String getCompanyName() { return companyName; } public void setCompanyName(String companyName) { this.companyName = companyName; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public String getSellerName() { return sellerName; } public void setSellerName(String sellerName) { this.sellerName = sellerName; } @Override public String toString() { return "Speakers [speakerID=" + speakerID + ", companyName=" + companyName + ", price=" + price + ", sellerName=" + sellerName + "]"; } }
SearchSpeakers.java
package com.mugil.test; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.stream.Collector; import java.util.stream.Collectors; import com.mugil.bean.Speakers; public class SearchSpeakers { public static void main(String[] args) { Speakers objSpeaker1 = new Speakers(101, "JBL Go", 120, "Amazon"); Speakers objSpeaker2 = new Speakers(102, "Panasonic", 420, "Amazon"); Speakers objSpeaker3 = new Speakers(103, "JBL Go", 900, "Flipkart"); Speakers objSpeaker4 = new Speakers(104, "JBL Go", 120, "Amazon"); Speakers objSpeaker5 = new Speakers(105, "JBL Go", 350, "Amazon"); Speakers objSpeaker6 = new Speakers(101, "Philips", 120, "EBay"); Speakers objSpeaker7 = new Speakers(102, "JBL Go", 125, "Ebay"); Speakers objSpeaker8 = new Speakers(103, "JBL Go", 35, "Smart Shoppe"); Speakers objSpeaker9 = new Speakers(104, "Panasonic", 80, "Amazon"); Speakers objSpeaker10 = new Speakers(105, "Philips", 180, "Amazon"); List < Speakers > arrSpeakers = new ArrayList < Speakers > (); arrSpeakers.add(objSpeaker1); arrSpeakers.add(objSpeaker2); arrSpeakers.add(objSpeaker3); arrSpeakers.add(objSpeaker4); arrSpeakers.add(objSpeaker5); arrSpeakers.add(objSpeaker6); arrSpeakers.add(objSpeaker7); arrSpeakers.add(objSpeaker8); arrSpeakers.add(objSpeaker9); arrSpeakers.add(objSpeaker10); Comparator < Speakers > spkComparator = (Speakers objSpeaker1a, Speakers objSpeaker2a) -> objSpeaker1a.getPrice() - objSpeaker2a.getPrice(); arrSpeakers.stream().sorted(spkComparator).collect(Collectors.groupingBy(Speakers::getCompanyName)).values() .stream().forEach(searchedModel -> System.out .println("Speaker with Lowest Cost" + searchedModel.stream().findFirst())); } }
arrSpeakers.stream().sorted(spkComparator).collect(Collectors.groupingBy(Speakers::getCompanyName)).values() .stream().forEach(searchedModel -> System.out .println("Speaker with Lowest Cost" + searchedModel.stream().findFirst()));
arrSpeakers.stream() – Convert to Stream
arrSpeakers.stream().sorted(spkComparator) – Sorts the list by price
arrSpeakers.stream().sorted(spkComparator).collect(Collectors.groupingBy(Speakers::getCompanyName)) – Groups the Sorted List by Company Name
arrSpeakers.stream().sorted(spkComparator).collect(Collectors.groupingBy(Speakers::getCompanyName)).values()
.stream() – Convert the Sorted List into Stream
The above code could be rewritten as follows
//Sort by Price and group by company name Stream objStream = arrSpeakers.stream() .sorted(spkComparator) .collect(Collectors.groupingBy(Speakers::getCompanyName)).values(); //Loop through Stream of List and print the first one objStream.forEach(listOfStudent -> System.out.println((ArrayList)listOfStudent.stream().findFirst()));
Output
Speaker with Lowest CostOptional[Speakers [speakerID=101, companyName=Philips, price=120, sellerName=EBay]] Speaker with Lowest CostOptional[Speakers [speakerID=104, companyName=Panasonic, price=80, sellerName=Amazon]] Speaker with Lowest CostOptional[Speakers [speakerID=103, companyName=JBL Go, price=35, sellerName=Smart Shoppe]]