Offer

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集合框架的体系图,感觉很有用!

https://images2015.cnblogs.com/blog/1010726/201706/1010726-20170621004734695-988542448.png

posted @ 2020-08-11 20:16  Empirefree  阅读(257)  评论(0编辑  收藏  举报