Pergunta

I have data in the following format (both double)

Price1         Price2
1.2             5.1
3.2             4.2
1.4             1.4
1.3             1.1

I want price1 and price2 to be linked to each other. I need to sort price2 in descending order and then remove last record. I then need to sort price1 in descending.

What is the best approach to take? - which datatype?. I was thinking of using hashmap but there is no concept of position although i can do a work around but it would be messy. Can also create an object to store them and then do a comparable but was thinking if I could do this in a few lines of code?. Not sure if a better option is viable but curious :)

Foi útil?

Solução

Similar to @dasblinkenlight answer,

Declare new Object Prices to hold the values

class Prices {
    BigDecimal price1;
    BigDecimal price2;
    public Prices(BigDecimal price1, BigDecimal price2) {
        this.price1 = price1;
        this.price2 = price2;
    }
    public BigDecimal getPrice1() {
        return price1;
    }
    public void setPrice1(BigDecimal price1) {
        this.price1 = price1;
    }   
    public BigDecimal getPrice2() {
        return price2;
    }
    public void setPrice2(BigDecimal price2) {
        this.price2 = price2;
    }
    @Override
    public String toString(){
        return String.valueOf(price1).concat(" ").concat(String.valueOf(price2));
    }
}

Declare variable as Big Decimal since price involved. Lot of answers your can found in SO why you should use BigDecimal instead of double .

For your requirement, I don't think you need to Sort the elements based on price2. Your requirement is to remove the least price2 element and sort by price1

class PriceComparator implements Comparator<Prices>{
    @Override
    public int compare(Prices o1, Prices o2) {
        return o2.getPrice1().compareTo(o1.getPrice1());
    }

    public  int getLeastPrice2(List<Prices> collections){
        int i =0;
        BigDecimal lowValue = collections.get(0).getPrice2();
        for (int j = 1; j < collections.size(); j++) {
            if (collections.get(j).getPrice2().compareTo(lowValue) < 0 ) {
                lowValue =  collections.get(j).getPrice2();
                i = j;
            }
        }
        return i;

    }
}

Comparator based on price1 and additional method to find minimum position of price2

Now test

List<Prices> priceList = new ArrayList<Prices>();
        Prices p1 = new Prices(BigDecimal.valueOf(1.2), BigDecimal.valueOf(5.1));
        Prices p2 = new Prices(BigDecimal.valueOf(3.2), BigDecimal.valueOf(4.2));
        Prices p3 = new Prices(BigDecimal.valueOf(1.4), BigDecimal.valueOf(1.4));
        Prices p4 = new Prices(BigDecimal.valueOf(1.3), BigDecimal.valueOf(1.1));
        priceList.add(p1);
        priceList.add(p2);
        priceList.add(p3);
        priceList.add(p4);
        System.out.println(priceList);
        PriceComparator priceComparator = new PriceComparator();
        priceList.remove(priceComparator.getLeastPrice2(priceList));
        System.out.println(priceList);
        Collections.sort(priceList, priceComparator);
        System.out.println(priceList);

out put

[1.2 5.1, 3.2 4.2, 1.4 1.4, 1.3 1.1]
[1.2 5.1, 3.2 4.2, 1.4 1.4]
[3.2 4.2, 1.4 1.4, 1.2 5.1]

Outras dicas

In Java8 you can do it in this way:

int[] prices1 = ...
int[] prices2 = ...
IntStream.range(0, prices1.length)
        .map(i -> new Map.Entry<>(prices1[i], prices2[i]))
        .sorted((entry1, entry2) -> {
            return Integer.compare(entry1.getValue(), entry2.getValue())
        })
        .skip(1)
        .sorted((entry1, entry2) -> {
            return Integer.compare(entry1.getKey(), entry2.getKey())
        })
        ...
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top