简体   繁体   中英

About sorting array list

I am trying to ensure that the objects i insert in productDatabase have not been already inserted and that i sort my arraylist using method sortData but without using any comparators in method sortData

public class Application {

public static void main(String[] args) {

    Product  p = new Product(15,"test",3.45);
    Product  p2 = new Product(15,"test",3.45);
    Product  p3 = new Product(4716,"koukouroukou",1.25);
    Product  p4 = new Product(6002,"bananofatsoula",0.60);

    ProductDatabase productDatabase = new ProductDatabase();

    productDatabase.addProduct(p);
    productDatabase.addProduct(p2);
    productDatabase.addProduct(p3);
    productDatabase.addProduct(p4);

    productDatabase.printDatabase();

    productDatabase.sortDatabase();
    productDatabase.printDatabase();

}
public class Product {

    private int code;
    private String name;
    private double price;

    public Product(int code, String name, double price){
        this.code = code;
        this.name = name;
        this.price = price;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String toString(){
        return code+" , description: "+name+", price: "+price;
    }

    public int hashCode(){
        return 31 * code + name.hashCode();
    }

    public boolean equals(Object o){
        Product other = (Product)o;
    if (this.code == other.code){
        return true;
    }
    else{ 
        return false;
    }
}
import java.util.ArrayList;

import java.util.Collection;


public class ProductDatabase {

private ArrayList<Product> productDatabase;

public ProductDatabase(){
    productDatabase = new ArrayList<Product>();
}

public void addProduct(Product p){
    if(!productDatabase.contains(p)){
        productDatabase.add(p);
    }
}

public void printDatabase(){
    for(Product product : productDatabase){
        System.out.println(product);
    }
}

public void sortDatabase(){

// ????????????????????????????????????????????????????????????

So my questions are

  1. Does contain(p) is enough to ensure that the same product is not already in the list?
  2. products are the same when they have the same code and name.if not what i have to do?
  3. How i sort my withous using comparators in class ProductDatabase.maybe by a new method in product ?
  4. Does productDatabase extends Product???

You can return bool value in order to know the product is already there or not . Code is used to ensure the differentiation of products if not add another code id. Product instance will carry information of just one Product so Sort must be done in the member function of class having all the records of product, not just one. No product_database does not extend product . It is a log of product class not a part .

your questions 1 and 2 are a little unclear. Can you re-write them? As for question 3. No ProductDatabase does not extend product, neither should it. ProductDatabase HAS products. ProductDatabase is not a product

  1. Yes, contains() is enough (in our case) to ensure uniqueness
  2. Since you implemented equals and hashCode - you're good
  3. You don't need to sort if you don't have another purpose to do so, but since you're using an ArrayList every time contains() is called it iterates the whole list which is not very efficient. A better implementation would use Set (a HashSet for example)
  4. ProductDatabase does not have to extend Product - it contains a list/set of products but it doesn't have any character/behavior like Product
  1. Yes, contain(p) is enough to ensure that the same product is not already in the list, because you overrided "equals" method. In "equals" you can use shorter construction:

     Product other = (Product)o; return this.code == other.code; 
  2. For sort ArrayList with java.util.Collections class two options possible:

    • Collections.sort(List list, Comparator c). You have to write own Comparator class and pass as second parameter.

    • Collections.sort(List list) and class Product must implement Comparable interface

Yes, Contain(p) is enough to ensure that the same product is not already in the list BUT that is NOT efficient. Use a Set instead of ArrayList.

For question 2, you have to decide the when the two products are equal and code that in your equals method like you did for 'product code'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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