Java-学习日记(集合、Java8、转义)
今天在遍历HashMap的使用引申出了HashMap无序、LinkedHashMap有序、Map.Entry遍历等知识点,还有一些其他知识点,比较琐屑,但很实用,总体来说就是Java基础与Spring注解还有Java8特性.
-
Java8新特性
-
Java中转义字符
-
HashMap与LinkedHashMap
Java8新特性
/*ThreadLocal与IntStream生成线程(SonarLint推荐使用线程池)*/
public static ThreadLocal<String> CHANNEL = new ThreadLocal<String>();
Random random = new Random();
IntStream.range(0, 5).forEach(temp -> new Thread(()->{
CHANNEL.set(temp + " " + random.nextInt(10));
System.out.println("Channel 和 local 分别是" + CHANNEL.get());
}).start());
Java转义字符
/*Java中分割.与|要转义 */
String.Spilt("\\.")
String.Spilt("\\|")
/*Java中json转Object/List<Object>时候转义 */
Hero hero = new Gson().fromJson("{\"id\": 123,\"name\":\"123\"}", Hero.class);
System.out.println(hero);
List<Hero> heroes = new Gson().fromJson("[{\"id\": 123,\"name\":\"123\"}]", new TypeToken<List<Hero>>(){}.getType());
heroes.stream().forEach(System.out::println);
HashMap与LinkedHashMap
/*HashMap与LinkedHashMap遍历与存储、提取*/
Map<String, String> hashMap = new HashMap<String, String>();
hashMap.put("2", "josan1");
hashMap.put("1", "josan2");
hashMap.put("3", "josan3");
hashMap.put("88", "josan88");
hashMap.put("54", "josan54");
hashMap.put("6", "josan6");
/*推荐---大容量时使用*/
for(Map.Entry<String, String> entry : hashMap.entrySet()){
System.out.println(entry.getKey() + entry.getValue());
}
System.out.println("=============Iterator遍历");
Iterator<Map.Entry<String, String>> iterator = hashMap.entrySet().iterator();
while (iterator.hasNext()){
Map.Entry<String, String> entry = iterator.next();
System.out.println(entry.getKey() + entry.getValue());
}
System.out.println("=============LinkedHashmap遍历");
Map<String, String> linkedHashMap = new LinkedHashMap<>();
linkedHashMap.put("name1", "josan1");
linkedHashMap.put("name2", "josan2");
linkedHashMap.put("name3", "josan3");
Iterator<Map.Entry<String, String>> iteratorLinedHashMap = linkedHashMap.entrySet().iterator();
while (iteratorLinedHashMap.hasNext()){
Map.Entry<String, String> entry1 = iteratorLinedHashMap.next();
System.out.println(entry1.getKey() + entry1.getValue());
}
HashMap与LinkedHashMap
/*HashMap与LinkedHashMap遍历与存储、提取*/
Map<String, String> hashMap = new HashMap<String, String>();
hashMap.put("2", "josan1");
hashMap.put("1", "josan2");
hashMap.put("3", "josan3");
hashMap.put("88", "josan88");
hashMap.put("54", "josan54");
hashMap.put("6", "josan6");
/*推荐---大容量时使用*/
for(Map.Entry<String, String> entry : hashMap.entrySet()){
System.out.println(entry.getKey() + entry.getValue());
}
System.out.println("=============Iterator遍历");
Iterator<Map.Entry<String, String>> iterator = hashMap.entrySet().iterator();
while (iterator.hasNext()){
Map.Entry<String, String> entry = iterator.next();
System.out.println(entry.getKey() + entry.getValue());
}
System.out.println("=============LinkedHashmap遍历");
Map<String, String> linkedHashMap = new LinkedHashMap<>();
linkedHashMap.put("name1", "josan1");
linkedHashMap.put("name2", "josan2");
linkedHashMap.put("name3", "josan3");
Iterator<Map.Entry<String, String>> iteratorLinedHashMap = linkedHashMap.entrySet().iterator();
while (iteratorLinedHashMap.hasNext()){
Map.Entry<String, String> entry1 = iteratorLinedHashMap.next();
System.out.println(entry1.getKey() + entry1.getValue());
}
下面就是Java集合框架的体系图,感觉很有用!
我曾七次鄙视自己的灵魂:
第一次,当它本可进取时,却故作谦卑;
第二次,当它在空虚时,用爱欲来填充;
第三次,在困难和容易之间,它选择了容易;
第四次,它犯了错,却借由别人也会犯错来宽慰自己;
第五次,它自由软弱,却把它认为是生命的坚韧;
第六次,当它鄙夷一张丑恶的嘴脸时,却不知那正是自己面具中的一副;
第七次,它侧身于生活的污泥中,虽不甘心,却又畏首畏尾。