This question already has answers here:
How do I efficiently iterate over each entry in a Java Map?
(43个答案)
4年前关闭。
我是Java的新手,正在尝试学习Maps的概念。
我想出了下面的代码。但是,我想同时打印出“键字符串”和“值字符串”。
ProcessBuilder pb1 = new ProcessBuilder();
Map<String, String> mss1 = pb1.environment();
System.out.println(mss1.size());
for (String key: mss1.keySet()){
System.out.println(key);
}
我只能找到只打印“键字符串”的方法。
java大神给出的解决方案
有多种方法可以实现此目的。这是三个。
Map<String, String> map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
System.out.println("using entrySet and toString");
for (Entry<String, String> entry : map.entrySet()) {
System.out.println(entry);
}
System.out.println();
System.out.println("using entrySet and manual string creation");
for (Entry<String, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + "=" + entry.getValue());
}
System.out.println();
System.out.println("using keySet");
for (String key : map.keySet()) {
System.out.println(key + "=" + map.get(key));
}
System.out.println();
输出量
using entrySet and toString
key1=value1
key2=value2
key3=value3
using entrySet and manual string creation
key1=value1
key2=value2
key3=value3
using keySet
key1=value1
key2=value2
key3=value3
Tomcat找不到直接放置在classes文件夹下的类 - java我有以下JSP:<%@ page import="foo.*" %> <html> <body> The page count is: <%=Counter.getCount()%> </body> </html> 我在包Counter中有一个foo类,该类存储在: …
DataSourceTransactionManager和JndiObjectFactoryBean和JdbcTemplate的用途是什么? - java以下的用途是什么:org.springframework.jdbc.core.JdbcTemplate org.springframework.jdbc.datasource.DataSourceTransactionManager org.springframework.jndi.JndiObjectFactoryBean <tx:annotatio…
如何修改休眠的SQL查询? - java我有点好奇,有没有办法修改hibernate的核心,以便我可以自定义生成的SQL query。例如,在生成的查询中添加功能以使用connect by prior(oracle)或我要自定义的任何其他子句。 java大神给出的解决方案 起初,这样的问题总是在我心中敲响警钟。你被警告了...AFAIK,hibernate使用所谓的dialects进行特定的优化。…
用Java构建大批量数据处理工具 - javaClosed. This question needs to be more focused。它当前不接受答案。 想改善这个问题吗?更新问题,使其仅通过editing this post专注于一个问题。 3年前关闭。 Improve this question 我正在尝试使用Java构建ETL工具。 ETL工具用于对大量数据(关系型和其他类型)进行批量读取,…
用Java封装对象? - javaprivate中的Java提供类级别的封装。可以封装一个对象吗?还是这样做徒劳?例如,如果我们将一个类定义为 public class Person { private String ssn; private ArrayList<Person> friends = new ArrayList<Person>(); public voi…