Java 映射中的映射vs拆分字符串类型的键,哪种执行速度更快?
我的地图是Java 映射中的映射vs拆分字符串类型的键,哪种执行速度更快?,java,string,collections,split,hashmap,Java,String,Collections,Split,Hashmap,我的地图是map 这些条目类似于map.put(“c_09.01--x28”,“OTH”)。在本例中,我使用拆分键并使用x28将其更改为OTH。所以我的问题是,我应该在map和map.put(“c_09.01”,newMap)中使用split操作还是在map和map.put(“x28”,OTH')中使用map。哪一个能给我更好的表现?我使用的示例代码是 for (Entry<String,String> sheetEntry : this.getSheetCD().getUserDe
map
这些条目类似于map.put(“c_09.01--x28”,“OTH”)。在本例中,我使用拆分键并使用x28
将其更改为OTH
。所以我的问题是,我应该在map和map.put(“c_09.01”,newMap)
中使用split操作还是在map和map.put(“x28”,OTH')
中使用map。哪一个能给我更好的表现?我使用的示例代码是
for (Entry<String,String> sheetEntry : this.getSheetCD().getUserDefinedSheetCodeMap().entrySet()) {
String Key = sheet.getKey().split("--")[1];
int sheetIndex = template.getSheetIndex(sheetKey);
if(sheetEntry.getKey().toUpperCase().startsWith(getFileName()){
String newSheetName = sheetEntry.getValue();
template.setSheetName(sheetIndex, newSheetName);
}
}
for(条目sheetEntry:this.getSheetCD().getUserDefinedSheetCodeMap().entrySet()){
字符串键=sheet.getKey().split(“--”[1];
int sheetIndex=template.getSheetIndex(sheetKey);
如果(sheetEntry.getKey().toUpperCase().StartWith(getFileName()){
字符串newSheetName=sheetEntry.getValue();
模板.setSheetName(sheetIndex,newSheetName);
}
}
如果需要更多信息,请告诉我。您应该使用拆分操作,因为如果您正在使用拆分操作,那么在
迭代时,您可以使用单个forEach循环
迭代贴图中的对象
示例代码
String str[] = "c_09.01--x28".split("--")[1];
Map<String, String> map = new HashMap<>();
map.put(str, "OTH");
for(Map.Entry eMap:map.entrySet()){ //single forEach loop
System.out.println("Key : "+eMap.getKey());
System.out.println("Value : "+eMap.getValue());
}
String str[] = "c_09.01--x28".split("--");
Map<String, String> map = new HashMap<>();
map.put(str[1], "OTH");
Map<String, Map> map1 = new HashMap<>();
map1.put(str[0], map);
for(Map.Entry eMap : map1.entrySet()){ //First forEach Loop
System.out.println("Key : "+eMap.getKey()); //getiing key "c_09.01"
Map<String, String> map2 = (Map<String, String>) eMap.getValue(); //getting map and need to be cast because it return object type
for(Map.Entry eMap1 : map2.entrySet()){ // Second forEach loop
System.out.println("Key Using map.put(String, newMAp) : "+eMap1.getKey());
System.out.println("ValueUsing map.put(String, newMAp) : "+eMap1.getValue());
}
}
如果你使用map.put(“c_09.01,newMap”)
这一个,那么你需要迭代
两次。像第一次一样,你需要迭代
使用键c_09.01
,然后你需要迭代
OTH
使用键x28
示例代码
String str[] = "c_09.01--x28".split("--")[1];
Map<String, String> map = new HashMap<>();
map.put(str, "OTH");
for(Map.Entry eMap:map.entrySet()){ //single forEach loop
System.out.println("Key : "+eMap.getKey());
System.out.println("Value : "+eMap.getValue());
}
String str[] = "c_09.01--x28".split("--");
Map<String, String> map = new HashMap<>();
map.put(str[1], "OTH");
Map<String, Map> map1 = new HashMap<>();
map1.put(str[0], map);
for(Map.Entry eMap : map1.entrySet()){ //First forEach Loop
System.out.println("Key : "+eMap.getKey()); //getiing key "c_09.01"
Map<String, String> map2 = (Map<String, String>) eMap.getValue(); //getting map and need to be cast because it return object type
for(Map.Entry eMap1 : map2.entrySet()){ // Second forEach loop
System.out.println("Key Using map.put(String, newMAp) : "+eMap1.getKey());
System.out.println("ValueUsing map.put(String, newMAp) : "+eMap1.getValue());
}
}
所以我想你应该使用拆分操作。为什么不试试并让我们知道结果呢?它用于一个我无法单独运行的项目中。但这将是我回到我的位置的第一件事。地图会变得多大?因为如果它永远不会太大,你将无法分辨区别。但这是一个简单的问题nchmark执行。但是如果你在文章中阅读,你会知道每个操作的成本。你执行GET的频率如何?首先,GET的值不会超过10k,其次,我将在整个事务中执行此操作一次。我认为你应该使用生成更清晰、更易于维护和更易于理解的c的版本ode。如果你后来意识到你需要调整性能,你仍然可以这样做。但是没有什么比不必要的复杂和难以理解的代码更令人沮丧的了,这些代码是像tha那样编写的,因为程序员认为“它会有更好的性能”