/**
* <pre>
* TODO:测试list和map的一些功能
* </pre>
*
* @author zhangjunchao
* @date 2019/7/9
*/
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);
}
/**
* list: 清除元素 clear() 无replace方法
* map :清除元素 clear() 有replace方法
*/
@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);
}
}
list和map方法测试
最新推荐文章于 2024-08-13 22:50:41 发布