简体   繁体   中英

how to iterate through hashmap and store entities into mysql

sorry I am a newbie I have been looking for an example on how I can iterate hashmap and store the entities into MySQL database. the blow code downloads currency rate which I would like to store into my database. the output is

{CCY=USD, RATE=1.5875}
{CCY=EUR, RATE=1.1919}
{CCY=ALL, RATE=166.2959}
{CCY=AMD, RATE=645.4025}

how can I iterate the HashMap and store it into my database? an illustration would be nice or source similar to this scenario.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class Rate {
    /** list of string array containing IOSCurrencyCodes*/
        final String[] CURRENCY = new String[] { "USD","EUR","ALL","AMD",};

        @SuppressWarnings("rawtypes")
        void checkRateAllAtEnd() throws Exception {     
                List<Callable<HashMap>> tasks = new ArrayList<Callable<HashMap>>();
                for (final String ccy : CURRENCY) {
                        tasks.add(new Callable<HashMap>() {
                                public HashMap<String, Comparable> call() throws Exception {
                                        return getRates(ccy);
                                }
                        });
                }
                ExecutorService executorPool = Executors.newCachedThreadPool();
                final List<Future<HashMap>> listRates = executorPool.invokeAll(tasks, 3600, TimeUnit.SECONDS);

                for (Future<HashMap> rate : listRates) {
                        HashMap ccyRate = rate.get();

                        System.out.println(ccyRate);
                     }
                }
                @SuppressWarnings("rawtypes")
                public HashMap<String, Comparable> getRates(String ccy) throws Exception {
                        URL url = new URL("http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s=GBP"
                                                        + ccy + "=X");
                        BufferedReader reader = new BufferedReader(new InputStreamReader(
                                        url.openStream()));

                        String data = reader.readLine();
                        String[] dataItems = data.split(",");
                        Double rate = Double.valueOf(dataItems[1]);

                        HashMap<String, Comparable> ccyRate = new HashMap<String, Comparable>();
                        ccyRate.put("CCY", ccy);
                        ccyRate.put("RATE", rate);

                        return ccyRate;           
                }
        public static void main(String[] args) {
            Rate ccyRate = new Rate();
                try {
                       //ccyConverter.checkRateSequential();
                       ccyRate.checkRateAllAtEnd();
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }
}

Map provides entrySet method which is very useful to iterate over a map.

Map#entrySet returns a Set view of the mappings contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress.

ref - link

Code -

HashMap<String, Comparable> ccyRate = new HashMap<String, Comparable>();
for(Map.Entry<String, Comparable> entry: ccyRate.entrySet){
    System.out.println(entry.getKey()+" - "+entry.getValue());
} 

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