java8 List<CgProduct> 产品列表如何按照产品抬头分组后,统计出产品名称以及总额(产品数量与产品单价相乘)列表,再将总额由大到小排序?

wangxi得 发布于 05/31 15:49
阅读 433
收藏 0

List cgProList = new ArrayList<>();

CgProduct cgProduct = new CgProduct();

cgProduct.setCpsl("1"); cgProduct.setCpdj("10.1");

cgProduct.setStatus("1");

cgProduct.setShtt("小米");//产品抬头

cgProList.add(cgProduct);

CgProduct cgProduct2 = new CgProduct();

cgProduct2.setCpsl("6");

cgProduct2.setCpdj("2.1");

cgProduct2.setShtt("小米2");

cgProduct2.setStatus("1");

cgProList.add(cgProduct2);

CgProduct cgProduct3 = new CgProduct();

cgProduct3.setCpsl("6");

cgProduct3.setCpdj("5.1");

cgProduct3.setShtt("小米2");

cgProduct3.setStatus("1");

cgProList.add(cgProduct3);

Map> collect = cgProList.stream().collect(Collectors.groupingBy(CgProduct::getShtt));

 

请问接下来如何统计出那个产品总额(产品数量与产品单价相乘)列表,再将总额由大到小排序?

加载中
0
tcxu
tcxu

产品台头才对,台头是名词,抬头是动词。 
至于属性名称: Status, Cps, ...,谁表示 "产品名称", 谁表示 "产品数量", 谁表示 "产品单价"?

wangxi得
wangxi得
产品抬头shtt,产品数量:cpsl,产品单价:cpdj ,产品名称与抬头一个意思,多谢帮忙
0
tcxu
tcxu
import java.util.*;

class CgProduct {
	private String cpsl="";
	private String cpdj="";
	private String status="";
	private String shtt="";
	private int age;
	public CgProduct(String cpsl, String cpdj, String status, String shtt){
		this.cpsl = cpsl;
		this.cpdj = cpdj;
		this.status = status;
		this.shtt = shtt;
		}
	public String getShtt(){
		return shtt;
	}
	public String getCpsl(){
		return cpsl;
	}
	public String getCpdj(){
		return cpdj;
	}
	public String toString(){
		String s = "产品数量 : " + cpsl;
		s += ", 产品单价: " + cpdj;
		s += ", 状态: " + status;
		s += ", 名称: " + shtt;
		return s;
	};
	
}

class LieBiao implements Comparable<LieBiao> {
	private String name;
	private double total;
	public LieBiao(String name, double total){
		this.name = name;
		this.total=total;
		}		
	public int compareTo(LieBiao  a) {	
       return (int)(a.total - this.total);
  	}
  	public String toString(){
  		String s = "产品: " + name;
  		s +=", 总额: " + total;
  		return s;
  	}
}
		
public class Wangxide{
 
public static void main(String args[]){
	List<CgProduct> cgProList = new ArrayList<>();
	cgProList.add(new CgProduct("15","10.1","1","小米1"));
	cgProList.add(new CgProduct("6","2.1","1","小米2"));
	cgProList.add(new CgProduct("6","5.1","1","小米1"));
	cgProList.add(new CgProduct("20","10.1","11","小米3"));
	cgProList.add(new CgProduct("16","2.1","11","小米2"));
	cgProList.add(new CgProduct("16","5.1","11","小米3"));
	System.out.println("原始数据");
	for( CgProduct o : cgProList)
	System.out.println(o); 
	
	List<CgProduct> x1 = new ArrayList<>(); //小米1
	List<CgProduct> x2 = new ArrayList<>(); //小米2
	List<CgProduct> x3 = new ArrayList<>(); //小米3
	
	for( CgProduct o : cgProList) 
	if (o.getShtt().compareTo("小米1")==0)
		x1.add(o);
		else 	if (o.getShtt().compareTo("小米2")==0)
		x2.add(o);
		else 	if (o.getShtt().compareTo("小米3")==0)
		x3.add(o);	
	double t1=0,t2=0,t3=0; //各个产品的总额
	for( CgProduct o : x1)
	t1 += Double.parseDouble(o.getCpdj())*Double.parseDouble(o.getCpsl());
	for( CgProduct o : x2)
	t2 += Double.parseDouble(o.getCpdj())*Double.parseDouble(o.getCpsl());
	for( CgProduct o : x3)
	t3 += Double.parseDouble(o.getCpdj())*Double.parseDouble(o.getCpsl());

	List<LieBiao> list = new ArrayList<>();
	list.add(new LieBiao("小米1" , t1));
	list.add(new LieBiao("小米2" , t2));
	list.add(new LieBiao("小米3" , t3));
	Collections.sort(list);
 
 	System.out.println("产品统计列表");
	for(LieBiao o : list)
	System.out.println(o.toString());

		};
}

0
dongjunv5
dongjunv5

groupingBy 再加个排序

tcxu
tcxu
有道理。
0
加菲爱
加菲爱
public class CgProduct implements Serializable {

    private static final long serialVersionUID = 58527237403680921L;

    private Integer cpsl;
    private Integer cpdj;
    private String status;
    private String shtt;

}

public static void main(String[] args) {
        List<CgProduct> cgProList = new ArrayList<>();
        CgProduct cgProduct = new CgProduct();
        cgProduct.setCpsl(1);
        cgProduct.setCpdj(10);
        cgProduct.setStatus("1");
        cgProduct.setShtt("小米");//产品抬头
        cgProList.add(cgProduct);

        CgProduct cgProduct2 = new CgProduct();
        cgProduct2.setCpsl(6);
        cgProduct2.setCpdj(2);
        cgProduct2.setShtt("小米2");
        cgProduct2.setStatus("1");
        cgProList.add(cgProduct2);

        CgProduct cgProduct3 = new CgProduct();
        cgProduct3.setCpsl(6);
        cgProduct3.setCpdj(5);
        cgProduct3.setShtt("小米2");
        cgProduct3.setStatus("1");
        cgProList.add(cgProduct3);

        //根据名称分组
        Map<String, List<CgProduct>> collect = cgProList.stream().collect(Collectors.groupingBy(CgProduct::getShtt));
        Map<String, Long> result = new HashMap<>();
        //计算数量*单价
        collect.forEach((shtt, list) -> {
            IntSummaryStatistics collect1 = list.stream().map(cgp -> cgp.getCpsl() * cgp.getCpdj()).collect(Collectors.toList())
                    .stream().collect(Collectors.summarizingInt(Integer::intValue));
            result.put(shtt, collect1.getSum());
        });
        //对结果进行降序
        result.values().stream().sorted(Comparator.reverseOrder());
        System.out.println(result);
    }

 

tcxu
tcxu
谢谢你的工作。可惜 我这里用的是 java version "1.8.0_162", 编译通不过。于是我改动了一下你的代码,以便通过编译,得以顺利运行。
0
tcxu
tcxu

根据: 加菲爱代码 修改成:

import java.util.*;
import java.util.stream.Collectors;
import java.util.function.Function;

class CgProduct {
	private Integer cpsl=0;
	private Integer cpdj=0;
	private String status="";
	private String shtt="";
	private int age;
	public CgProduct(Integer cpsl, Integer cpdj, String status, String shtt){
		this.cpsl = cpsl;
		this.cpdj = cpdj;
		this.status = status;
		this.shtt = shtt;
		}
	public String getShtt(){
		return shtt;
	}
	public Integer getCpsl(){
		return cpsl;
	}
	public Integer getCpdj(){
		return cpdj;
	}
	public String toString(){
		String s = "产品数量 : " + cpsl;
		s += ", 产品单价: " + cpdj;
		s += ", 状态: " + status;
		s += ", 名称: " + shtt;
		return s;
	};
	
}

public class Wangxide1{
 
public static void main(String args[]){
	List<CgProduct> cgProList = new ArrayList<>();
	cgProList.add(new CgProduct(new Integer("15"),new Integer("10"),"1","小米1"));
	cgProList.add(new CgProduct(new Integer("6"),new Integer("2"),"1","小米2"));
	cgProList.add(new CgProduct(new Integer("6"),new Integer("5"),"1","小米1"));
	cgProList.add(new CgProduct(new Integer("20"),new Integer("11"),"11","小米3"));
	cgProList.add(new CgProduct(new Integer("16"),new Integer("21"),"11","小米2"));
	cgProList.add(new CgProduct(new Integer("16"),new Integer("5"),"11","小米3"));
	System.out.println("原始数据");
	for( CgProduct o : cgProList)
	System.out.println(o); 
	
        //根据名称分组
        Map<String, List<CgProduct>> collect = cgProList.stream().collect(Collectors.groupingBy(CgProduct::getShtt));
        Map<String, Long> result = new HashMap<>();
        //计算数量*单价
        collect.forEach((shtt, list) -> {
            IntSummaryStatistics collect1 = list.stream().map(cgp -> cgp.getCpsl() * cgp.getCpdj()).collect(Collectors.toList())
                    .stream().collect(Collectors.summarizingInt(Integer::intValue));
            result.put(shtt, collect1.getSum());
        });
        //排序,按升序输出
        System.out.println("按升序输出");
        result.values().stream().sorted();
        System.out.println(result);
        System.out.println("按楼主要求,降序输出:");
        ListIterator<Map.Entry<String,Long>> i = new ArrayList<Map.Entry<String,Long>>(result.entrySet()).listIterator(result.size());
        while(i.hasPrevious()) {  
            Map.Entry<String, Long> entry=i.previous();  
            System.out.println(entry.getKey()+":"+entry.getValue());  
        }

		};
}

 

OSCHINA
登录后可查看更多优质内容
返回顶部
顶部