java - 如何迭代hashmap并将实体存储到mysql中

标签 java url hashmap loops

抱歉,我是一个新手,我一直在寻找有关如何迭代 HashMap 并将实体存储到 MySQL 数据库中的示例。下面的代码下载我想存储到数据库中的货币汇率。输出是

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

如何迭代 HashMap 并将其存储到我的数据库中?最好有一个插图或类似于此场景的来源。

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 提供了 entrySet 方法,该方法对于迭代 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

代码-

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

关于java - 如何迭代hashmap并将实体存储到mysql中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14425590/

相关文章:

JavaFX2.2(稳定)忽略 "socksProxyHost"和 "socksProxyPort"的设置属性?

java - 采用 Enum 参数的静态方法

Angular/Spring Boot/Azure Ingress - URL 重写

java - 初始化有序 map ?

Java - 根据条件迭代 map 列表

java - 将 HashMap 转换为排序的 ArrayList

java - 使用 long 的字符串 CharAt 方法

java - 如何通过SQL语句从数据库表中获取两个特定日期(由用户指定)之间的数据?

python - 如何导航到其中包含\u 的 URL?

java - Tomcat URL 认证例如 : https://user:password@app. wibble.com