public class list和map测试 {
@Test
public void testList() {
int size = 10;
double[] myList = new double[size];
myList[0] = 5.6;
myList[1] = 4.5;
myList[2] = 3.3;
myList[3] = 13.2;
myList[4] = 4.0;
myList[5] = 34.33;
myList[6] = 34.0;
myList[7] = 45.45;
myList[8] = 99.993;
myList[9] = 11123;
double total = 0;
for (int i = 0; i < size; i++) {
total += myList[i];
}
System.out.println("总和为: " + total);
double[] meList = {1.1, 2, 5.5, 6.3, 5.4, 56};
double total1 = 0;
for (double v : meList) {
total1 += v;
}
System.out.println(total1);
}
@Test
public void testMapList(){
ArrayList<HashMap<String, Object>> listMap = new ArrayList<>();
HashMap<String, Object> stringObjectHashMap = new HashMap<>();
stringObjectHashMap.put("name", "zhangsan");
stringObjectHashMap.put("age", "18");
stringObjectHashMap.put("date", new Date());
listMap.add(stringObjectHashMap);
listMap.forEach((HashMap<String, Object> map) -> {
map.replace("date", JSON.toJSONString(map.get("date"), SerializerFeature.WriteDateUseDateFormat));
});
String s = JSON.toJSONString(listMap);
System.out.println(s);
}
@Test
public void testMap() {
Map<String, String> map = new HashMap<>();
map.put("name", "zhangsan");
map.put("age", "18");
map.replace("name", "lisi");
String s1 = JSON.toJSONString(map);
System.out.println(s1);
map.clear();
String s = JSON.toJSONString(map);
System.out.println(s);
}
@Test
public void testListReplace() throws IOException {
List<Userinfo> list = new ArrayList<>(10);
list.add(new Userinfo("张", 15));
list.get(0).setName("王");
String s = JSON.toJSONString(list);
System.out.println(s);
list.clear();
String s1 = JSON.toJSONString(list);
System.out.println(s1);
}
}