Java里面没有提供像C,C++那样的Sizeof()方法,所以不可以直接取得内存资源大小
但是有提供RuntimegetRuntime ()totalMemory ()和RuntimegetRuntime ()freeMemory ()这样的方法
所以内存使用大小 = totalMemory () - freeMemory();
但是又不能直接传送Hashmap对象去计算必须在创建Haspmap之前gc一次,然后得到大小,创建HaspMap之后再gc一次,得到内存大小,然后相减
===================================
是的,这种方法统计的内存大小是Java使用的内存但是Java垃圾回收机制,所以不能精确到KB那就是我们在计算得时候必须GC得原因
还有一个办法就是上面这么兄弟说得,用序列化把HaspMap序列化(其实就是写入到一个临时文件)然后这个临时文件得大小,就是内存占用得大小这个可以精确到KB,但是效率上,值得考究
MapActionjava
Java代码
package comzxdemoaction;
import javautilArrayList;
import javautilHashMap;
import javautilList;
import javautilMap;
import comopensymphonyxwork2ActionSupport;
import comzxdemomodelProduct;
import comzxdemomodelStudent;
public class MapAction extends ActionSupport
{
private Map<String,String> map;
private Map<String,Student> studentMap;
private Map<String,String[]> arrayMap;
private Map<String,List<Student>> listMap;
public String testMap()
{
map=new HashMap<String,String>();
mapput("1", "one");
mapput("2", "two");
studentMap=new HashMap<String,Student>();
studentMapput("student1",new Student(new Long(1),"20034140201","张三1","男",25));
studentMapput("student2",new Student(new Long(2),"20034140202","张三2","女",26));
studentMapput("student3",new Student(new Long(3),"20034140202","张三3","男",27));
arrayMap=new HashMap<String,String[]>();
arrayMapput("arr1", new String[]{"1","2003401","leejie","male","20"});
arrayMapput("arr2", new String[]{"2","2003402","huanglie","male","25"});
arrayMapput("arr3", new String[]{"3","2003403","lixiaoning","male","21"});
listMap=new HashMap<String,List<Student>>();
List<Student> list1=new ArrayList<Student>();
list1add(new Student(new Long(1),"20034140201","张三1","男",25));
list1add(new Student(new Long(2),"20034140202","张三2","男",25));
list1add(new Student(new Long(3),"20034140203","张三3","男",25));
listMapput("class1", list1);
List<Student> list2=new ArrayList<Student>();
list2add(new Student(new Long(1),"20034140301","李四1","男",20));
list2add(new Student(new Long(2),"20034140302","李四2","男",21));
list2add(new Student(new Long(3),"20034140303","李四3","男",22));
list2add(new Student(new Long(4),"20034140304","李四4","男",23));
listMapput("class2", list2);
return SUCCESS;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
thismap = map;
}
public Map<String, Student> getStudentMap() {
return studentMap;
}
public void setStudentMap(Map<String, Student> studentMap) {
thisstudentMap = studentMap;
}
public Map<String, String[]> getArrayMap() {
return arrayMap;
}
public void setArrayMap(Map<String, String[]> arrayMap) {
thisarrayMap = arrayMap;
}
public Map<String, List<Student>> getListMap() {
return listMap;
}
public void setListMap(Map<String, List<Student>> listMap) {
thislistMap = listMap;
}
}
2testMapjsp
Java代码
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>struts2中的map遍历总结</title>
</head>
<body>
<b>1map中的value为String字符串</b><br>
<s:iterator value="map" id="column">
<s:property value="#column"/><br>
key: <s:property value="key"/><br>
value:<s:property value="value"/><br>
<br>
</s:iterator>
<b>2map中的value为Student对象</b>
<table border="1" width="50%" cellspacing="0" cellpadding="0">
<tr>
<td>key=value</td>
<td>ID</td>
<td>num</td>
<td>name</td>
<td>sex</td>
<td>age</td>
</tr>
<s:iterator value="studentMap" id="column">
<tr>
<td><s:property value="#column"/></td>
<td><s:property value="valueid"/></td>
<td><s:property value="valuenum"/></td>
<td><s:property value="valuename"/></td>
<td><s:property value="valuesex"/></td>
<td><s:property value="valueage"/></td>
</tr>
</s:iterator>
</table>
<p>
<b>3map中的value为String数组</b>
<table border="1" width="50%" cellspacing="0" cellpadding="0">
<tr>
<td>key=value</td>
<td>ID</td>
<td>num</td>
<td>name</td>
<td>sex</td>
<td>age</td>
</tr>
<s:iterator value="arrayMap" id="column">
<tr>
<td><s:property value="#column"/></td>
<td><s:property value="value[0]"/></td>
<td><s:property value="value[1]"/></td>
<td><s:property value="value[2]"/></td>
<td><s:property value="value[3]"/></td>
<td><s:property value="value[4]"/></td>
</tr>
</s:iterator>
</table>
<p>
<b>4map中的value为list集合</b>
<table border="1" width="50%" cellspacing="0" cellpadding="0">
<tr>
<td>class</td>
<td>ID</td>
<td>num</td>
<td>name</td>
<td>sex</td>
<td>age</td>
</tr>
<s:iterator value="listMap" id="column">
<s:set name="total" value="#columnvaluesize"/>
<s:iterator value="#columnvalue" status="s">
<tr>
<s:if test="#sfirst"><td rowspan="${total}"><s:property value="#columnkey"/></td></s:if>
<td><s:property value="id"/></td>
<td><s:property value="num"/></td>
<td><s:property value="name"/></td>
<td><s:property value="sex"/></td>
<td><s:property value="age"/></td>
</tr>
</s:iterator>
</s:iterator>
</table>
</body>
</html>
<s:iterator begin="1" end="stepflag-1" step="1" status="st">
<textarea name="reason" id="reason" cols="75" rows="3" readonly="readonly">${dataMap['reason'+stindex+1]}</textarea>
index是从0开始取值的
以上就是关于Java JSP如何取得HashMap的大小全部的内容,包括:Java JSP如何取得HashMap的大小、jsp页面如何对map集合遍历、jsp struts2标签循环的时候map根据key的变化动态取值 求各位大神帮忙~!!!等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)