【问题标题】:Custom sorting of 2 LinkedLists of user defined objects based on the class variable in Java基于Java中的类变量对用户定义对象的2个LinkedList进行自定义排序
【发布时间】:2017-09-09 10:13:29
【问题描述】:

我有两个 LinkedList:newLinkedList 和 oldLinkedList,它们都包含 BID 类对象。以下是我的 BID 课程:

    public class Bid {

    private int quantity;
    private double bidPrice;

   public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public double getBidprice() {
        return bidPrice;
    }

    public void setBidprice(double bidPrice) {
        this.bidPrice = bidPrice;
    }
}

现在我必须创建一个新的 LinkedListlist,其中包含基于 BID 类的价格变量的排序元素 newLinkedList 和 oldLinkedList。 如果我在两个 LinkedList 中得到相同的价格,那么我必须保留 newLinkedList BID 类对象并删除旧的。

这意味着新的 LinkedList 必须包含根据价格变量排序的 Bid 类对象。

这是我的主要功能:

 public static void main(String[] args) throws InterruptedException, IOException {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter size of linkedlist 1 ");
    int size1 = Integer.parseInt(br.readLine());
    System.out.println("Enter size of linkedlist 2 ");
    int size2 = Integer.parseInt(br.readLine());
    LinkedList<Bid> oldLinkedList= addElementsToList(size1);
    LinkedList<Bid> newLinkedList= addElementsToList(size2);

    /*
          SORT BOTH THE LINKED LISTS HERE
    */

}

public static LinkedList<Bid> addElementsToList(int size) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    LinkedList<Bid> bidList = new LinkedList<Bid>();

    for (int i = 0; i < size; i++) {
        Bid bid = new Bid();
        System.out.println("Enter bid price of Object " + i);
        bid.setBidprice(Double.parseDouble(br.readLine()));
        System.out.println("Enter bid quantity of Object " + i);
        bid.setQuantity(Integer.parseInt(br.readLine()));
        bidList.add(bid);
    }
  return bidList;
}

【问题讨论】:

  • 你有两个链表..然后使用嵌套的for循环,比较器或类似的东西
  • 你有什么问题?
  • "将两个链表合并为一个,并对最终链表进行排序。"但是 LinkedList 包含“Bid”类对象。所以必须根据“Bid”类对象的价格变量进行排序。

标签: java sorting object linked-list


【解决方案1】:

也许这就是你想要的,对于 oldList 中的每个 Bid,检查它的价格是否已经存在于 newList 中。如果存在,则什么都不做,否则将其添加到newList中,并对最后一个newList进行排序。你可以测试一下。

注意:我不确定你是否真的要比较两个双倍价格。

    boolean containsSamePrice(LinkedList<Bid> list, double price) {
        for (Bid bid : list) {
            if (bid.getBidprice() == price) {
                return true;
            } 
        }
        return false;
    }

LinkedList<Bid> mergeAndSort(LinkedList<Bid> newLinkedList, LinkedList<Bid> oldLinkedList) {
    for (Bid oldBid : oldLinkedList) {
        if (!containsSamePrice(newLinkedList, oldBid.getBidprice())) {
            newLinkedList.add(oldBid);
        }
    }
    Comparator<Bid> comparator = new Comparator<Bid>() {
        @Override
        public int compare(Bid o1, Bid o2) {
            if (o1.getBidprice() < o2.getBidprice())
                return -1;
            if (o2.getBidprice() == o2.getBidprice())
                return 0;
            return 1;
        }
    };
    Collections.sort(newLinkedList, comparator);
    return newLinkedList;
}

【讨论】:

  • 谢谢先生。是的,我想比较双倍价格,但有一个小的变化,如果 oldbid.getprice() 等于 newLinkedList 中的任何价格,那么我必须将 newLinkedList Bid 对象添加到排序列表中,但不是oldLinkedList 中的 oldBid 之一
  • 是的,我们可以使用newLinkedList作为排序列表。
【解决方案2】:

您可以在 Bid 类中实现 Comparator 接口并使用 Collections.sort() 方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多