交易链问题

Tags
#算法

数据模型

public class Trade {
    public long id;//交易id
    public long productId;//商品id
    public Character seller;//卖家
    public Character buyer;//买家

    public Trade(long id, long productId, Character seller, Character buyer) {
        this.id = id;
        this.productId = productId;
        this.seller = seller;
        this.buyer = buyer;
    }

    @Override
    public String toString() {
        return "Trade{" +
                "id=" + id +
                ", productId=" + productId +
                ", seller=" + seller +
                ", buyer=" + buyer +
                '}';
    }
}

业务逻辑

public class ExampleUnitTest {
    /**
     * 17620893113
     * 13120939660
     *
     * @throws Exception
     */
    @Test
    public void test() throws Exception {
        //1.首先,模拟出两笔交易历史
        List<Trade> list = new ArrayList<>();
        //1->7->6->2->0->8->9->3->1->1->3
        String test1 = "17620893113";
        for (int i = 0; i < test1.length() - 1; i++) {
            list.add(new Trade(i, 1, test1.charAt(i), test1.charAt(i + 1)));
        }
        //13120939660
        int offset = list.size();
        String test2 = "13120939660";
        for (int i = 0; i < test2.length() - 1; i++) {
            list.add(new Trade(offset + i, 2, test2.charAt(i), test2.charAt(i + 1)));
        }
        //打乱顺序,以便模拟真实的交易数据
        Collections.shuffle(list);
        System.out.println("模拟数据:");
        list.forEach(new Consumer<Trade>() {
            @Override
            public void accept(Trade trade) {
                System.out.println(trade);
            }
        });
        //2.接着,我们需要找到商品id为2的交易,并生成交易链
        List<Trade> collect = list.stream()
                //找到商品id的交易
                .filter(new Predicate<Trade>() {
                    @Override
                    public boolean test(Trade trade) {
                        return trade.productId == 2;
                    }
                })
                //按交易id排序
                .sorted(new Comparator<Trade>() {
                    @Override
                    public int compare(Trade o1, Trade o2) {
                        return o1.id - o2.id > 0 ? 1 : -1;
                    }
                })
                .collect(Collectors.toList());
        //打印输出
        System.out.println("商品id为2的交易链:");
        for (int i = 0; i < collect.size(); i++) {
            Trade trade = collect.get(i);
            if (i != collect.size() - 1) {
                System.out.print(trade.seller + "->");
            } else {
                System.out.print(trade.seller + "->" + trade.buyer);
            }
       

输出结果

模拟数据:
Trade{id=8, productId=1, seller=1, buyer=1}
Trade{id=19, productId=2, seller=6, buyer=0}
Trade{id=16, productId=2, seller=3, buyer=9}
Trade{id=9, productId=1, seller=1, buyer=3}
Trade{id=10, productId=2, seller=1, buyer=3}
Trade{id=12, productId=2, seller=1, buyer=2}
Trade{id=2, productId=1, seller=6, buyer=2}
Trade{id=0, productId=1, seller=1, buyer=7}
Trade{id=1, productId=1, seller=7, buyer=6}
Trade{id=11, productId=2, seller=3, buyer=1}
Trade{id=17, productId=2, seller=9, buyer=6}
Trade{id=5, productId=1, seller=8, buyer=9}
Trade{id=13, productId=2, seller=2, buyer=0}
Trade{id=7, productId=1, seller=3, buyer=1}
Trade{id=4, productId=1, seller=0, buyer=8}
Trade{id=14, productId=2, seller=0, buyer=9}
Trade{id=15, productId=2, seller=9, buyer=3}
Trade{id=3, productId=1, seller=2, buyer=0}
Trade{id=18, productId=2, seller=6, buyer=6}
Trade{id=6, productId=1, seller=9, buyer=3}
商品id为2的交易链:
1->3->1->2->0->9->3->9->6->6->0

© fishyer 2022