解决托盘问题

合并托盘 现已定义好托盘(class Tray)、商品(Goods)的结构,其中一个托盘可以包含1个或多个商品,商品由ID、数量组成。 请将相同的托盘(托盘ID相等),同一个托盘内,请将相同的商品合并(商品ID相等时,数量相加)。 例如现有结构 Tray_0: (Goods_1, 2), (Goods_2, 1), Tray_1: (Goods_2, 2), Tray_0: (Goods_1, 1), (Goods_3, 1), 调用result = mergeTrays(trays);方法后,合并后的结果放入result对象,结果应当为 Tray_0: (Goods_1, 3), (Goods_2, 1), (Goods_3, 1), Tray_1: (Goods_2, 2),

package com.dan.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class MergeTrays {

static class Tray {
int id;
List<Goods> goods;
}

static class Goods {
int id;
int count;
}

private static final int MAX_TRAYS = 10;
private static final int MAX_TRAY_ID = 6;
private static final int MAX_GOODS = 3;
private static final int MAX_GOODS_ID = 10;
private static final int MAX_GOODS_COUNT = 3;

public static void main(String[] args) {
List<Tray> trays = genTrays();
printTrays("原始数据", trays);
List<Tray> result = mergeTrays(trays);
printTrays("处理结果", result);
}

private static List<Tray> genTrays() {
List<Tray> trays = new ArrayList<Tray>();
for (int i = 0; i < MAX_TRAYS; ++i) {
Tray tray = genTray();
trays.add(tray);
}
return trays;
}

private static Random rnd = new Random();

private static Tray genTray() {
Tray tray = new Tray();
tray.id = rnd.nextInt(MAX_TRAY_ID);
tray.goods = genGoods();
return tray;
}

private static List<Goods> genGoods() {
List<Goods> result = new ArrayList<Goods>();
int goodsSize = rnd.nextInt(MAX_GOODS) + 1;
for (int i = 0; i < goodsSize; ++i) {
Goods g = new Goods();
g.id = rnd.nextInt(MAX_GOODS_ID);
g.count = rnd.nextInt(MAX_GOODS_COUNT) + 1;
result.add(g);
}
return result;
}

private static void printTrays(String title, List<Tray> list) {
System.out.println(title);
for (Tray t : list) {
if (t == null)
continue;
System.out.print(" Tray_" + t.id + ": ");
if (t.goods != null) {
for (Goods g : t.goods) {
if (g == null)
continue;
System.out.print("(Goods_" + g.id + ", " + g.count + "), ");
}
}
System.out.println();
}
System.out.println();
}

/** 第一种做法 */
// private static List<Tray> mergeTrays(List<Tray> list) {
// //TODO 请补充代码
//
// /* 获取list中对象个数 */
// int size = list.size();
//
// /** 进行第一次去重复 */
// /* 定义第一个新的List集合存储新的Tray对象【第一轮去重复】 */
// List<Tray> newList = new ArrayList<Tray>();
// for (int i = 0; i < size; i++) {
//
// /* 获取当前 i 下标的tray对象 */
// Tray tray1 = list.get(i);
// /* 判断是否是已经去掉重复的tray对象 */
// boolean bool1 = false;
// for (Tray tray : newList) {
// if(tray1.id == tray.id){
// bool1 = true;
// break;
// }
// }
// if(bool1){
// continue;
// }
//
// /* 从当前list位置的后面一个开始遍历 */
// List<Goods> goodsList = new ArrayList<Goods>();
// /* 将当前对象的goods装入goodsList */
// goodsList.addAll(tray1.goods);
// for (int j = i + 1; j < size; j++) {
//
// /* 如果与后面的tray.id相等,执行以下代码 */
// if(tray1.id == list.get(j).id){
// goodsList.addAll(list.get(j).goods);
// }
// }
// tray1.goods = goodsList;
// newList.add(tray1);
// }
//
// /** 进行第二次去重复 */
// /* 定义第二个新的List集合存储新的Tray对象【第二轮去重复】 */
// List<Tray> newList2 = new ArrayList<Tray>();
// for (Tray tray2 : newList) {
// Tray tray3 = new Tray();
// tray3.id = tray2.id;
//
// Map<Integer, Integer> map = new HashMap<Integer, Integer>();
// for (Goods goods : tray2.goods) {
// if(map.containsKey(goods.id)){
// map.put(goods.id, map.get(goods.id) + goods.count);
// }else
// map.put(goods.id, goods.count);
// }
//
// List<Goods> list2 = new ArrayList<Goods>();
// for (Map.Entry<Integer, Integer> mapGood: map.entrySet()) {
// Goods goods = new Goods();
// goods.id = mapGood.getKey();
// goods.count = mapGood.getValue();
// list2.add(goods);
// }
// tray3.goods = list2;
// newList2.add(tray3);
// }
// return newList2;
// }

/** 第二种做法 */
// private static List<Tray> mergeTrays(List<Tray> list) {
// // TODO 请补充代码
//
// /* 【第一轮去重复】 */
// Map<Integer, List<Goods>> trayMap = new HashMap<Integer, List<Goods>>();
// for (Tray tray : list) {
// if (trayMap.containsKey(tray.id)) {
// List<Goods> goods = trayMap.get(tray.id);
// goods.addAll(tray.goods);
// trayMap.put(tray.id, goods);
// } else {
// trayMap.put(tray.id, tray.goods);
// }
//
// }
// Set<Integer> traySet = trayMap.keySet();
// Iterator<Integer> trayIterator = traySet.iterator();
//
// list.clear();
// while (trayIterator.hasNext()) {
// Tray tray = new Tray();
// tray.id = trayIterator.next();
// tray.goods = trayMap.get(tray.id);
//
// list.add(tray);
// }
//
// /* 【第二轮去重复】 */
// Map<Integer, Integer> goodsMap = null;
// for (Tray tray : list) {
// List<Goods> goodss = tray.goods;
// goodsMap = new HashMap<Integer, Integer>();
// for (Goods goods : goodss) {
// if (!goodsMap.containsKey(goods.id)) {
// goodsMap.put(goods.id, goods.count);
// } else {
// goodsMap.put(goods.id, goodsMap.get(goods.id) + goods.count);
// }
// }
// Set<Integer> goodsSet = goodsMap.keySet();
// Iterator<Integer> goodsIterator = goodsSet.iterator();
//
// tray.goods.clear();
// Goods goods = null;
// while(goodsIterator.hasNext()){
// goods = new Goods();
// goods.id = goodsIterator.next();
// goods.count = goodsMap.get(goods.id);
//
// tray.goods.add(goods);
// }
// }
//
// return list;
// }

/** 第三种做法 */
// private static List<Tray> mergeTrays(List<Tray> list) {
// // TODO 请补充代码
//
// // 模拟 tray类【Map.key = tary.id】【Map.value = tary.goods】
// Map<Integer, List<Goods>> trayMap = null;
// // 模拟 goods类【Map.key = goods.id】【Map.value = goods.count】
// Map<Integer, Integer> goodsMap = null;
//
// /* 【第一轮去重复】 */
// trayMap = new HashMap<Integer, List<Goods>>();
// for (Tray tray : list) {
// /*
// * 这里IF是因为Map不能出现key相同的情况,所以当要添加的key在Map中存在怎么办,不存在又怎么办?自己想
// */
// if (trayMap.containsKey(tray.id)) {
// List<Goods> goods = trayMap.get(tray.id);
// goods.addAll(tray.goods);
// trayMap.put(tray.id, goods);
// } else {
// trayMap.put(tray.id, tray.goods);
// }
// }
//
// Set<Integer> traySet = trayMap.keySet();
// Iterator<Integer> trayIterator = traySet.iterator();
//
// list.clear();
// while (trayIterator.hasNext()) {
// Tray tray = new Tray();
// tray.id = trayIterator.next();
//
// /* 【第二轮去重复】 */
// List<Goods> goodss = trayMap.get(tray.id);
// goodsMap = new HashMap<Integer, Integer>();
// for (Goods goods : goodss) {
// /*
// * 这里IF是因为Map不能出现key相同的情况, 所以当要添加的key在Map中存在怎么办,不存在又怎么办?自己想
// */
// if (!goodsMap.containsKey(goods.id)) {
// goodsMap.put(goods.id, goods.count);
// } else {
// goodsMap.put(goods.id, goodsMap.get(goods.id) + goods.count);
// }
// }
// Set<Integer> goodsSet = goodsMap.keySet();
// Iterator<Integer> goodsIterator = goodsSet.iterator();
//
// tray.goods.clear();
// Goods goods = null;
// while (goodsIterator.hasNext()) {
// goods = new Goods();
// goods.id = goodsIterator.next();
// goods.count = goodsMap.get(goods.id);
//
// tray.goods.add(goods);
// }
// // 去重复成功
// list.add(tray);
// }
// return list;
// }

/** 第四种做法 */
private static List<Tray> mergeTrays(List<Tray> list) {
// TODO 请补充代码

// 模拟 tray类【Map.key = tary.id】【Map.value = tary.goods】
Map<Integer, List<Goods>> trayMap = null;
// 模拟 goods类【Map.key = goods.id】【Map.value = goods.count】
Map<Integer, Integer> goodsMap = null;

/* 【第一轮去重复】 */
trayMap = new HashMap<Integer, List<Goods>>();
for (Tray tray : list) {
/*
* 这里IF是因为Map不能出现key相同的情况,所以当要添加的key在Map中存在怎么办,不存在又怎么办?自己想
*/
if (trayMap.containsKey(tray.id)) {
List<Goods> goods = trayMap.get(tray.id);
goods.addAll(tray.goods);
trayMap.put(tray.id, goods);
} else {
trayMap.put(tray.id, tray.goods);
}
}

// 清空传递过来的的List集合,用于装去重复后的数据
list.clear();

// Tray类去重复成功,开始准备第二轮去重复
for (Map.Entry<Integer, List<Goods>> trayMap2 : trayMap.entrySet()) {
Tray tray = new Tray();
tray.id = trayMap2.getKey();
tray.goods = new ArrayList<Goods>();

/* 【第二轮去重复】 */
List<Goods> goodss = trayMap2.getValue();
goodsMap = new HashMap<Integer, Integer>();
for (Goods goods : goodss) {
/*
* 这里IF是因为Map不能出现key相同的情况, 所以当要添加的key在Map中存在怎么办,不存在又怎么办?自己想
*/
if (!goodsMap.containsKey(goods.id)) {
goodsMap.put(goods.id, goods.count);
} else {
goodsMap.put(goods.id, goodsMap.get(goods.id) + goods.count);
}
}

Goods goods = null;
for (Map.Entry<Integer, Integer> goodsMap2 : goodsMap.entrySet()) {
goods = new Goods();
goods.id = goodsMap2.getKey();
goods.count = goodsMap2.getValue();
tray.goods.add(goods);
}
// 去重复成功
list.add(tray);
}
return list;
}
}

原文地址:https://www.cnblogs.com/tyzmzlf/p/6675623.html