简体   繁体   English

将对象与集合中的唯一标识符进行排序和关联

[英]Sort and associate objects with unique identifier in a collection

I'm working my through an assignment and got stuck on step 5, would appreciate any help.我正在完成一项任务并卡在第 5 步,不胜感激。

  1. Carefully study the class structure in Products.java.仔细研究 Products.java 中的 class 结构。
  2. Design a generic container called GenericOrder that acts as a collection of an arbitrary number of objects in Products.java.设计一个名为 GenericOrder 的通用容器,它充当 Products.java 中任意数量的对象的集合。 Design a mechanism that gives each instance of the container a unique identifier.设计一种机制,为容器的每个实例提供唯一标识符。 Implement as many methods as necessary.根据需要实施尽可能多的方法。 You must use Java generics features.您必须使用 Java generics 功能。
  3. Design and implement a subclass of GenericOrder called ComputerOrder that takes an arbitrary number of different classes of ComputerPart objects, Peripheral objects, and Service objects.设计和实现一个名为 ComputerOrder 的 GenericOrder 子类,它采用任意数量的不同类的 ComputerPart 对象、Peripheral 对象和 Service 对象。 Implement as many methods as necessary.根据需要实施尽可能多的方法。
  4. Design and implement a subclass of GenericOrder called PartyTrayOrder that takes an arbitrary number of different classes of Cheese objects, Fruit objects, and Service objects.设计并实现一个名为 PartyTrayOrder 的 GenericOrder 子类,它采用任意数量的不同类别的 Cheese 对象、Fruit 对象和 Service 对象。 Implement as many methods as necessary.根据需要实施尽可能多的方法。
  5. Design and implement a class called OrderProcessor.设计并实现一个名为 OrderProcessor 的 class。 You must implement at least the following methods:您必须至少实现以下方法:
    • accept;接受; // this method accepts a GenericOrder or any of its subclass objects and stores it in any internal collection of OrderProcessor. // 此方法接受 GenericOrder 或其任何子类对象,并将其存储在 OrderProcessor 的任何内部集合中。
    • process;过程; // this method sorts all accepted orders in the internal collection of GenericOrder into collections of ComputerPart, Peripheral, Cheese, Fruit, and Service. // 该方法将GenericOrder内部集合中所有接受的订单排序到ComputerPart、Peripheral、Cheese、Fruit和Service的collections中。 You must associate each object with the unique identifier.您必须将每个 object 与唯一标识符相关联。 You may refer to the TwoTuple.java example in the text book.您可以参考教科书中的 TwoTuple.java 示例。
    • dispatchXXX;派遣XXX; // this method simulates the dispatch of the sorted collections. // 此方法模拟排序后的 collections 的调度。 For example, the method dispatchComputerParts() should produce this output:例如,方法 dispatchComputerParts() 应该产生这个 output:
      • Motherboard name=Asus, price=$37.5, order number=123456主板名称=华硕,价格=$37.5,订单号=123456
      • Motherboard – name=Asus, price=$37.5, order number=987654主板 – 名称=华硕,价格=37.5 美元,订单号=987654
      • RAM – name=Kingston, size=512, price=$25.0, order number=123456 RAM – 名称=金士顿,大小=512,价格=25.0 美元,订单号=123456
  6. Create a client class to test OrderProcessor.创建一个客户端 class 来测试 OrderProcessor。 You will need to create a datagenerator for testing purpose.您将需要创建一个数据生成器用于测试目的。 It is not mandatory but you may use a variation of Data Generator in TIJ pages 637 to 638.这不是强制性的,但您可以使用 TIJ 第 637 至 638 页中的数据生成器的变体。

Here is what I have for Q5这是我为 Q5 准备的

public abstract class OrderProcessor<T> {
    private ArrayList<T> dataCollection = new ArrayList<T>();

    public void accept(T item){
        dataCollection.add(item);
    }

    public void process(){
        Collections.sort(dataCollection);
    }

    public List getDataCollection(){
        return dataCollection;
    }
} 

In its current state Collections.sort(dataCollection);在其当前 state Collections.sort(dataCollection); doesn't compile because it does not accept T and if I change the ArrayList to String any function used from other subclasses won't work because they all T .无法编译,因为它不接受T并且如果我将 ArrayList 更改为String ,其他子类中使用的任何 function 都将不起作用,因为它们都是T Any help would be greatly appreciated.任何帮助将不胜感激。

thanks in advance.提前致谢。

EDIT: Since you want to partition your orders and not sort, you can use something like this:编辑:由于您想对订单进行分区而不是排序,因此可以使用以下内容:

dataCollection.stream().collect(
  Collectors.groupingBy(order -> order.getIdentifier())
)

Here, this groups them by their identifiers and puts them into a Map .在这里,这将它们按标识符分组并将它们放入Map中。 The order.getIdentifier() part is just a placeholder for whatever you want to use to divide them up. order.getIdentifier()部分只是您想要用来划分它们的任何内容的占位符。 The return type will be Map<TypeOfIdentifier, T> .返回类型将为Map<TypeOfIdentifier, T>

For this to work, though, your T has to be of some specific type ( T extends Product perhaps?) so you can get the identifier.但是,要使其正常工作,您的T必须是某种特定类型( T extends Product ?),以便您可以获得标识符。 Since I don't know the code for differentiating between different products, I can't put the exact code here.由于我不知道区分不同产品的代码,所以我不能在这里放确切的代码。

The Javadoc for Collectors Collectors的 Javadoc


This is why Collections.sort wasn't working for you, but you don't need Collections.sort anyways.这就是为什么 Collections.sort 不适合你,但你不需要 Collections.sort 反正。

T must extend the Comparable interface, because obviously you can't sort objects of just any type. T 必须扩展Comparable接口,因为显然您不能对任何类型的对象进行排序。 The Comparable interface has a compareTo method that lets you sort. Comparable 接口有一个compareTo方法,可让您进行排序。

An alternative would be to write a custom Comparator that defines a single method: compare , which would take 2 objects of type T and return an int representing the order (in most cases it's basically the first argument minus the second argument).另一种方法是编写一个自定义的Comparator ,它定义了一个方法: compare ,它将接受 2 个T类型的对象并返回一个表示顺序的int (在大多数情况下,它基本上是第一个参数减去第二个参数)。 For this, you would need to use Collections.sort(dataCollection, customComparator) .为此,您需要使用Collections.sort(dataCollection, customComparator)

You can define your comparator with a lambda expression, but I can't help you beyond that because I have no idea how you want to sort your objects.您可以使用 lambda 表达式定义比较器,但除此之外我无法帮助您,因为我不知道您想如何对对象进行排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM