简体   繁体   中英

Java sorting with collections plus manual sorting

I am writing a console application that calculates using hashtables various prices. It writes prices with a class called Priceprint. I am using hashtables for the rest of the program because order is not particularly important, but it orders the keys before creating a list as output. It puts them in order by putting the keys in a vector, sorting the vector with Collections.sort() and manually swapping the first and second keys with entries with keys exchange and special. It then uses an Enumeration to get everything from the vector, and calls another function to write each entry to the screen.

public void out(Hashtable<String, Double> b, Hashtable<String, Double> d) {
            Vector<String> v;
            Enumeration<String> k;
            String te1, te2, e;
            int ex, sp;

            v = new Vector<String>(d.keySet());
            Collections.sort(v);

            te1 = new String(v.get(0));
            ex = v.indexOf("exchange");
            v.set(ex, te1); v.set(0, "exchange");

            te2 = new String(v.get(1));
            ex = v.indexOf("special");
            v.set(ex, te2); v.set(1, "special");

            if (msgflag == true)
                    System.out.println("Listing Bitcoin and dollar prices.");
            else {
                    System.out.println("Listing Bitcoin and dollar prices, "
                                       + message + ".");
                    msgflag = true;
            }

            k = v.elements();
            while (k.hasMoreElements()) {
                    e = new String(k.nextElement());
                    out(e, d.get(e), b.get(e));
            }
    }

Now the problem is I've ran into through lack of thought alone is that swap the entries and sort the list in alphabetical order of it's keys. So when I run the program exchange and special are at the top but the rest of the list is no longer in order. I might have to scrap the essential design where lists are outputted through the code for single entries with keys price and special coming to the top but having order with every other aspect of the list. It's a shame, because it pretty much all might need to go and I really liked the design.

Here is the full code, ignore the fact I'm using constructors on a class that evidently should be using static methods but overlooked that: http://pastebin.com/CdwhcV2L

Here is the code using Printprice to create a list of prices to test another part of the program but also Printprice lists: http://pastebin.com/E2Fq13zF

Output:

john@fekete:~/devel/java/pricecalc$ java backend.test.Test 
I test CalcPrice, but I also test Printprice(Hashtable, Hashtable, String).
Listing Bitcoin and dollar prices, for unit test, check with calculator.
Exchange rate is $127.23 (USDBTC).
Special is 20.0%.
privacy:    $2.0    0.0126BTC, for unit test, check with calculator.
quotaband:  $1.5    0.0094BTC, for unit test, check with calculator.
quotahdd:   $5.0    0.0314BTC, for unit test, check with calculator.
shells:     $5.0    0.0314BTC, for unit test, check with calculator.
hosting:    $10.0   0.0629BTC, for unit test, check with calculator.

The problem appears to be that you are putting the first and second elements back into the vector at the locations that "exchange" and "special" came from, instead of removing "exchange" and "special" from the vector and inserting them at the top of the vector.

Doing this correctly would be more efficient with a LinkedList instead of a Vector. To carry out the required operations, assuming v is a List :

v.add(0, v.remove(v.indexOf("special")));  
v.add(0, v.remove(v.indexOf("exchange")));

This should put "exchange" first, "special" second and the rest of the list will remain in sorted order afterwards.

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