Java 对于由属性文件控制的循环迭代?
我有一个应用程序,其中包含一个Java 对于由属性文件控制的循环迭代?,java,loops,for-loop,hashmap,properties-file,Java,Loops,For Loop,Hashmap,Properties File,我有一个应用程序,其中包含一个方法,该方法用于使用For循环来循环通过哈希映射: public void iterateHashMap(Map<Dog, List<String>> mapOfDogsAndDescriptions){ for (Map.Entry<Dog, List<String>> entry : mapOfDogIdsAndStrings.entrySet()) { String ke
方法
,该方法用于使用For循环来循环通过哈希映射
:
public void iterateHashMap(Map<Dog, List<String>> mapOfDogsAndDescriptions){
for (Map.Entry<Dog, List<String>> entry : mapOfDogIdsAndStrings.entrySet()) {
String key = entry.getKey().getId();
List<String> StringList = entry.getValue();
//do something
}
}
public void iterateHashMap(dogsanddescriptions的映射){
对于(Map.Entry:MapOfLogidSandStrings.entrySet()){
String key=entry.getKey().getId();
List StringList=entry.getValue();
//做点什么
}
}
我希望能够根据我的properties
文件中的属性定义循环的迭代次数
例如,如果属性设置为3,则它仅迭代哈希映射中的前3个键
这是我第一次在Java应用程序中使用属性文件
,我该怎么做?您是否使用任何框架?在Spring Framework中,您可以按以下方式执行:
@Resource(name = "properties")
private Properties properties;
在应用程序上下文中:
<bean id="properties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="classpath:urls.properties"></property>
</bean>
您是否使用任何框架?在Spring Framework中,您可以按以下方式执行:
@Resource(name = "properties")
private Properties properties;
在应用程序上下文中:
<bean id="properties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="classpath:urls.properties"></property>
</bean>
我会这样做:
public void iterateHashMap(Map<Dog, List<String>> mapOfDogsAndDescriptions){
int count = Integer.parseInt(System.getProperty("count"));
for (Map.Entry<Dog, List<String>> entry : mapOfDogIdsAndStrings.entrySet()) {
if (count-- <= 0)
break;
String key = entry.getKey().getId();
List<String> StringList = entry.getValue();
//do something
}
}
public void iterateHashMap(dogsanddescriptions的映射){
int count=Integer.parseInt(System.getProperty(“count”);
对于(Map.Entry:MapOfLogidSandStrings.entrySet()){
如果(count--我会这样做:
public void iterateHashMap(Map<Dog, List<String>> mapOfDogsAndDescriptions){
int count = Integer.parseInt(System.getProperty("count"));
for (Map.Entry<Dog, List<String>> entry : mapOfDogIdsAndStrings.entrySet()) {
if (count-- <= 0)
break;
String key = entry.getKey().getId();
List<String> StringList = entry.getValue();
//do something
}
}
public void iterateHashMap(dogsanddescriptions的映射){
int count=Integer.parseInt(System.getProperty(“count”);
对于(Map.Entry:MapOfLogidSandStrings.entrySet()){
如果(count--在纯java中,您可以
Properties prop = new Properties();
String propFileName = "config.properties";
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
if (inputStream != null) {
prop.load(inputStream);
} else {
throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
}
int cpt = Integer.valueOf(prop.getProperty("counter"));
int cptLoop = 0;
for (Map.Entry<Dog, List<String>> entry : mapOfDogIdsAndStrings.entrySet()) {
if (cptLoop == cpt)
break;
String key = entry.getKey().getId();
List<String> StringList = entry.getValue();
//do something
cptLoop++;
}
Properties prop=新属性();
String propFileName=“config.properties”;
InputStream InputStream=getClass().getClassLoader().getResourceAsStream(propFileName);
如果(inputStream!=null){
属性加载(输入流);
}否则{
抛出新的FileNotFoundException(“在类路径中找不到属性文件“”+propFileName+”);
}
int cpt=Integer.valueOf(prop.getProperty(“计数器”);
int cptLoop=0;
对于(Map.Entry:MapOfLogidSandStrings.entrySet()){
if(cptLoop==cpt)
打破
String key=entry.getKey().getId();
List StringList=entry.getValue();
//做点什么
cptLoop++;
}
在纯java中,您可以
Properties prop = new Properties();
String propFileName = "config.properties";
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
if (inputStream != null) {
prop.load(inputStream);
} else {
throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
}
int cpt = Integer.valueOf(prop.getProperty("counter"));
int cptLoop = 0;
for (Map.Entry<Dog, List<String>> entry : mapOfDogIdsAndStrings.entrySet()) {
if (cptLoop == cpt)
break;
String key = entry.getKey().getId();
List<String> StringList = entry.getValue();
//do something
cptLoop++;
}
Properties prop=新属性();
String propFileName=“config.properties”;
InputStream InputStream=getClass().getClassLoader().getResourceAsStream(propFileName);
如果(inputStream!=null){
属性加载(输入流);
}否则{
抛出新的FileNotFoundException(“在类路径中找不到属性文件“”+propFileName+”);
}
int cpt=Integer.valueOf(prop.getProperty(“计数器”);
int cptLoop=0;
对于(Map.Entry:MapOfLogidSandStrings.entrySet()){
if(cptLoop==cpt)
打破
String key=entry.getKey().getId();
List StringList=entry.getValue();
//做点什么
cptLoop++;
}
您是否已经设法从属性文件中读取了该值(即3
)?没有,我刚刚开始解决这个“第一个N”是一个有小问题的需求。对于HashMap,它的键是无序的,因此前N个未确定。对于TreeMap,它是所有已排序键中的前N个。对于LinkedHashMap,它是将它们添加到映射中的第一个N。您是否已设法读取该值(代码>3
)从属性文件?没有,我只是开始解决这个“第一个N”是一个有小问题的需求。对于HashMap,它的键是无序的,所以第一个N是不确定的。对于TreeMap,它是所有排序键中的第一个N。对于LinkedHashMap,它是按顺序将它们添加到映射中的第一个N。好的,谢谢,但是我如何让循环只遍历HashMap中的前3个条目值?使用一个计数器,每次迭代递增1,与属性进行比较,然后使用break跳出循环OK谢谢,但是我如何让循环只迭代HashMap中的前3个条目值呢?在每次迭代中使用一个计数器,递增1,与属性进行比较,然后使用break跳出您可能需要的循环开始减少计数。是的,是的。谢谢。你能解释一下吗,我听不懂?Thanks@java123999“int count”是一个变量,它告诉我们应该执行多少次迭代。它使用从默认属性文件读取的值进行初始化,该文件使用Integer.parseInt()进行解析方法。之后我们进入循环,在我们做任何事情之前,我们检查count是否小于或等于0,如果是,我们就跳出循环。我使用了“count--”,因为它将count减少0,并返回原始值。这样做是因为如果我们使用--count,我们将少进行一次迭代(如果count是1,它会减少,所以现在它是0,“if”语句中的条件为true,所以我们执行“break”您可能忘记了减少count
。是的,是的。谢谢。您能解释一下吗,我听不懂?Thanks@java123999“整数计数”是一个变量,它告诉我们应该执行多少次迭代。它使用从默认属性文件读取的值进行初始化,该文件使用Integer.parseInt()方法进行分析。在进入循环之后,在执行任何操作之前,我们检查计数是否小于或等于0,如果是,则中断循环。我使用了“计数--”因为它将count减少0,并返回原始值。这样做是因为如果我们使用--count,我们将少做一次迭代(如果count为1,则减少,所以现在为0,“if”语句中的条件为true,所以我们执行“break”