java - 基于Java中的类变量对用户定义对象的2个LinkedList进行自定义排序

标签 java sorting object linked-list

我有两个 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;
}

最佳答案

也许这就是您想要的,对于 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;
}

关于java - 基于Java中的类变量对用户定义对象的2个LinkedList进行自定义排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46129558/

相关文章:

java - 使用 Java 映射大于 2GB 的文件

从两个列表中选择最佳组合的算法

sorting - Java8 Streams 中的惰性排序(),需要在每次迭代时求助

javascript - React 中的对象

java - 如何在 Java 中对对象数组进行排序

java - Java 应用程序中的 SOAP 调用

java - 计算一组 double (本质上是图表上的点)的斜率最有效的方法是什么?

java - 智能gwt的comboBoxItem deosn不会根据用户输入进行过滤

javascript - Jquery/JavaScript : Sort a datatable on column that has <a>

python - 将对象的方法映射到对象列表的更 pythonic 方式