Когда-то давно писал статью
https://user12vv.wordpress.com/2016/10/19/java-работа-с-коллекциями-объектов/
В Java8 это можно сделать намного проще:

Есть обычный класс Product

package org.example.pojo;

public class Product {
	
	private String name;
	private Integer price;	
	private String brand;
		
	
	
	public Integer getPrice() {
		return price;
	}

	public Product setPrice(Integer price) {
		this.price = price;
		return this;
	}
	
	public String getName() {
		return name;
	}
	public Product setName(String name) {
		this.name = name;
		return this;
	}
	
	public String getBrand() {
		return brand;
	}
	public Product setBrand(String brand) {
		this.brand = brand;
		return this;
	}

	@Override
	public String toString() {
		return "Product [name=" + name + ", price=" + price + ", brand=" + brand + "]";
	}
	
	
	
}

Далее создаем класс ProductCollections для удобства работы с коллекциями:

package org.example.pojoCollections;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.log4j.Logger;
import org.example.pojo.Product;

public class ProductCollections {

	private static final Logger log = Logger.getLogger(ProductCollections.class);

	private ArrayList<Product> prodCollection = new ArrayList<>();

	private Stream<Product> products() {
		return getProdCollection().stream();
	}

	public int size() {
		return prodCollection.size();
	}

	public Product get(int i) {
		return prodCollection.get(i);
	}

	public boolean isEmpty() { 
		return prodCollection.isEmpty();
	}

	public ArrayList<Product> getProdCollection() {
		if (isEmpty()) {
			prodCollection.add(new Product().setName("Икра красная \"Вкуснота \"").setBrand("Наши традиции").setPrice(25));
			prodCollection.add(new Product().setName("Чай зеленый \"Принцесса Нури\"").setBrand("Ахмад").setPrice(15));
			prodCollection.add(new Product().setName("Чай ароматный с бергамотом.").setBrand("Беседа").setPrice(140));
			prodCollection.add(new Product().setName("Чай ароматный с бергамотом.").setBrand("Беседа").setPrice(65));
			prodCollection.add(new Product().setName("Lipton Yellow Label").setBrand("Lipton").setPrice(50));
			prodCollection.add(new Product().setName("Magic aroma").setBrand("Lipton").setPrice(18));
			prodCollection.add(new Product().setName("English Breakfast").setBrand("Lipton").setPrice(12));
			prodCollection.add(new Product().setName("Royal Ceylon").setBrand("Lipton").setPrice(14));
		}

		return prodCollection;
	}
	
	

	@Override
	public String toString() {
		return "ProductCollections [prodCollection=" + prodCollection + "]";
	}

	public List<Product> sortByName() {
		return products().sorted(Comparator.comparing(Product::getName)).collect(Collectors.toList());
	}

	public List<Product> sortByPrice() {
		return products().sorted(Comparator.comparingInt(Product::getPrice)).collect(Collectors.toList());

	}

	public Product prodWithMaxPrice() {
		return products().max(Comparator.comparing(Product::getPrice)).get();
	}

	public Product prodWithMinPrice() {
		return products().min(Comparator.comparingInt(Product::getPrice)).get();
	}

	public List<Product> findBy(Predicate<Product> predicate) {
		List<Product> list = products().filter(predicate)
				.collect(Collectors.toList());

		if (list.isEmpty()) {
			log.info("Dont find element.");
		}
		return list;
	}
	
	
	public static Predicate<Product> productName(String productName) {
		return p -> p.getName().contains(productName);
	}
	
	public static Predicate<Product> productBrand(String productBrand) {
		return p -> p.getBrand().contains(productBrand);
	}
	
	public static Predicate<Product> productPrice(Integer productPrice) {
		return p -> p.getPrice().equals(productPrice);
	}

}

Ну и класс для проверки:

package org.example.main;

import org.apache.log4j.Logger;
import org.example.pojoCollections.ProductCollections;
import static org.example.pojoCollections.ProductCollections.productBrand;
import static org.example.pojoCollections.ProductCollections.productName;
import static org.example.pojoCollections.ProductCollections.productPrice;

public class MainClasss {
	
	private static final Logger log = Logger.getLogger(MainClasss.class);
	
	public static void main(String[] args) {
		
		
		
		ProductCollections pc = new ProductCollections();	
		
		log.info(pc.prodWithMaxPrice()); 
		log.info(pc.prodWithMinPrice());
		
		System.out.println("=========================");
		
		log.info(pc.findBy(productBrand("Беседа")));
		log.info(pc.findBy(productName("Ceylon")));
		log.info(pc.findBy(productPrice(1000)));
		
		
	
		
		
	}

}

Весь код на гитхаб:
https://github.com/VictorSem/ProductJava8

Реклама