如何对我的对象列表进行排序

时间:2014-10-01 09:44:52

标签: java eclipse sorting

我现在已经编写好几个小时来完成我的任务。现在我已经完成了,我想添加一种方法来对客户的价值进行排序。因此,客户2排在首位,最后排名第5位。我该怎么做?

以下是第一堂课的代码:

public class Customer {

    private int customerNumber;
    private int purchases;
    private int shippingCost;
    private int productPrice;

    public int getProductPrice() {
        return productPrice;
    }

    public void setProductPrice(int productPrice) {
        this.productPrice = productPrice;
    }

    public int getPurchases() {
        return purchases;
    }

    public Customer(int purchases, int shippingCost, int productPrice) {
        this.purchases = purchases;
        this.shippingCost = shippingCost;
        this.productPrice = productPrice;
    }

    public void setPurchases(int purchases) {
        this.purchases = purchases;
    }

    public int getShippingCost() {
        return shippingCost;
    }

    public void setShippingCost(int shippingCost) {
        this.shippingCost = shippingCost;
    }

    protected int calculateValue() {
        int value=purchases*(productPrice-shippingCost);
        return value;
    }

    public int getCustomerNumber() {
        return customerNumber;
    }

    public void setCustomerNumber(int customerNumber) {
        this.customerNumber = customerNumber;
    }

第二节课:

public class Subscriber extends Customer{

    private int years;
    private int fee;

    public Subscriber(int purchases, int shippingCost, int productPrice) {
        super(purchases, shippingCost,  productPrice);
    }

    public Subscriber(int purchases, int shippingCost, 
            int productPrice,int _years,int _fee) {
        super(purchases, shippingCost, productPrice);
        years=_years;
        fee=_fee;
    }

    public int getYears() {
        return years;
    }

    public void setYears(int years) {
        this.years = years;
    }

    public int getFee() {
        return fee;
    }

    public void setFee(int fee) {
        this.fee = fee;
    }

    protected int calculateSubscriptionValue() {
        return years*fee;
    }

    protected int calculateValue(){
        int value=super.calculateValue();   
        value+=calculateSubscriptionValue();
        return value;
    }

第三课:

public class BusinessSubscriber extends Subscriber{

    public BusinessSubscriber(int purchases, int shippingCost, int productPrice, int _years, int _fee) {
        super(purchases, shippingCost, productPrice, _years, _fee);
    }
    private double supportCost;
    private double supportTime;

    public double getSupportCost() {
        return supportCost;
    }
    public void setSupportCost(double supportCost) {
        this.supportCost = supportCost;
    }
    public double getSupportTime() {
        return supportTime;
    }
    public void setSupportTime(double supportTime) {
        this.supportTime = supportTime;
    }

    //override method: // skriv över den // problem med denna metod. uträkningsfel
    protected int calculateValue(){
        int value=super.calculateValue();   
        value+=calculateSubscriptionValue() - calculateSubscriptionValue();
        return value;
    }
}

最后和主要课程:

public class printValue {

    public static void main(String[] args) {
        System.out.println("Unsorted version without DataStructure");
        System.out.println("-----------------------");
        BusinessSubscriber num1 = new BusinessSubscriber(3, 33, 99, 8, 78); 

        num1.setCustomerNumber(1);

        num1.setPurchases(3);
        num1.setShippingCost(33);
        num1.setYears(8);
        num1.setSupportTime(2);
        num1.setProductPrice(99);
        num1.setFee(78);
        num1.setSupportCost(25);
        System.out.println("Customer number: " + num1.getCustomerNumber() + " is worth " + num1.calculateValue() + "!");

        BusinessSubscriber num2 = new BusinessSubscriber (0, 0, 99, 18, 78);

        num2.setCustomerNumber(2);

        num2.setPurchases(0);
        num2.setShippingCost(0);
        num2.setYears(18);
        num2.setSupportTime(7);

        num2.setProductPrice(99);
        num2.setFee(78);
        num2.setSupportCost(25);

        System.out.println("Customer number: " + num2.getCustomerNumber() + " is worth " + num2.calculateValue() + "!");

        BusinessSubscriber num3 = new BusinessSubscriber (0, 0, 99, 8, 78);

        num3.setCustomerNumber(3);

        num3.setPurchases(0);
        num3.setShippingCost(0);
        num3.setYears(8);
        num3.setSupportTime(0);

        num2.setProductPrice(99);
        num2.setFee(78);
        num2.setSupportCost(25);

        System.out.println("Customer number: " + num3.getCustomerNumber() + " is worth " + num3.calculateValue() + "!");

        //  int purchases, int shippingCost, int productPrice, int _years, int _fee 

        Subscriber num4 = new Subscriber (12, 33, 99, 5, 78);

        num4.setCustomerNumber(4);

        num4.setPurchases(12);
        num4.setShippingCost(33);
        num4.setYears(5);

        num4.setProductPrice(99);
        num4.setFee(78);

        System.out.println("Customer number: " + num4.getCustomerNumber() + " is worth " + num4.calculateValue() + "!");

        Customer num5 = new Customer (8, 33, 99);

        num5.setCustomerNumber(5);

        num5.setPurchases(8);
        num5.setShippingCost(33);
        num5.setProductPrice(99);

        System.out.println("Customer number: " + num5.getCustomerNumber() + " is worth " + num5.calculateValue() + "!");

        // sortera efter calculatevalue metoden i businesssubsriber
}

代码的输出:

Customer number: 1 is worth 822!
Customer number: 2 is worth 1404!
Customer number: 3 is worth 624!
Customer number: 4 is worth 1182!
Customer number: 5 is worth 528!

我希望2号客户位于最前面,5位最后,即排序。我怎么做?谢谢。

我试过这个,但那就够了吗?

Integer [] sort = {num1.calculateValue(), num2.calculateValue(), num3.calculateValue(), num4.calculateValue(), num5.calculateValue()};

List<Integer> l1 = Arrays.asList(sort);

Collections.sort(l1);
System.out.println("Values sorted: " + l1);

但更有效的方法是将它全部放在地图中,循环遍历它然后打印它。我怎么做?

此外,我尽可能多地编辑文本,因此看起来并不凌乱。

4 个答案:

答案 0 :(得分:0)

如果您根据calculateValue()对客户进行排序,可以这样做:

Map sortedCustomer= new TreeMap();
sortedCustomer.put(num1.calculateValue(),num1);
sortedCustomer.put(num2.calculateValue(),num2);
sortedCustomer.put(num3.calculateValue(),num3);
sortedCustomer.put(num4.calculateValue(),num4);
sortedCustomer.put(num5.calculateValue(),num5);
//sortedCustomer is in ascending order...reverse it
sortedCustomerDescending =(TreeSet)sortedCustomer.descendingSet();
//get entrySet
  Set set = sortedCustomerDescending.entrySet();
  // Get an iterator
  Iterator i = set.iterator();
  // Display elements
  while(i.hasNext()) {
     Map.Entry me = (Map.Entry)i.next();
  //print the entries
     System.out.print(me.getKey() + ": ");
     System.out.println(me.getValue());
  }

答案 1 :(得分:0)

我假设你想按降序排序。

您需要将实现传递给Comaprator并将其传递给Collections#sort方法以及包含要排序的对象的列表。

这应该做的事情

public void testSort() {
    List<Customer> cL = Arrays.asList(new Customer(1, 822), 
            new Customer(2, 1404), 
            new Customer(3, 624), 
            new Customer(4, 1182), 
            new Customer(5, 528));
    Collections.sort(cL, new Comparator<Customer>() {
        @Override
        public int compare(Customer o1, Customer o2) {
            return o2.getCalc() - o1.getCalc();
        }
    });

    for( Customer c : cL) {
        System.out.println("Customer number: " + c.getCustomerNumber() 
                + " is worth " + c.getCalc() + "!");
    }
}

输出:

Customer number: 2 is worth 1404!
Customer number: 4 is worth 1182!
Customer number: 1 is worth 822!
Customer number: 3 is worth 624!
Customer number: 5 is worth 528!

为简单起见,我在calc本身添加了一个Customer字段,您可能希望为BusinessSubscriber类实现Comparator,因为您要查找的值来自它

答案 2 :(得分:0)

您的Customer类应实现Comparable接口

public class Customer implements Comparable<Customer>{

您应该覆盖其compareTo方法

@Override
public int compareTo(Customer cus) {
int calculatedValue = cus.calculateValue();
return calculatedValue - this.calculateValue();
}

最后在PrintValue类中,将这些客户添加到ArrayList并使用Collections.sort

ArrayList<Customer> custList = new ArrayList<>();
custList.add(num1);
custList.add(num2);
custList.add(num3);
custList.add(num4);
custList.add(num5);

Collections.sort(custList);

for (Iterator iterator = custList.iterator(); iterator.hasNext();) {
    Customer customer = (Customer) iterator.next();
    System.out.println("Customer number: " + customer.getCustomerNumber() + " is worth " + customer.calculateValue() + "!");
}

答案 3 :(得分:0)

如果您有对象列表,可以使用Collections.sort对其进行排序。

愚蠢的例子(未编译或运行):

Collections.sort(listOfUsers, new Comparator<User>() {
    @Override
    public int compare(User u1, User u2) {
        return Integer.compare(u1.getAge(), u2.getAge());
    }
});

这种比较可能不完全是你需要做的,但你明白了: - )