代码之家  ›  专栏  ›  技术社区  ›  hema chandra

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

  •  0
  • hema chandra  · 技术社区  · 7 年前

    我有两个LinkedList:newLinkedList和oldLinkedList,都包含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;
        }
    }
    

    如果我在两个LinkedList中得到相同的价格,那么我必须保留新的LinkedList 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;
    }
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Ke Li    7 年前

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

        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;
    }
    
        2
  •  0
  •   Alex Favieres    7 年前

    您可以在Bid类中实现比较器接口并使用集合。sort()方法。