本文主要是介绍HashMap前后顺序put的数值教训总结,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、层级结构是Map<Object,Map<o,list>>需要进行排序
使用HashMap赋值,最前面的放在前面put,后面的不需要展示的放在后面put,理论上是可以有顺序的,可是结果并不是这样:
public class ReflectTest {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {test2();}public static void test2(){HashMap<String ,String> old=new HashMap();HashMap<String ,String> newHash=new HashMap();HashMap newOrderHash=new HashMap();old.put("1","1");old.put("2","2");old.put("3","3");old.put("4","4");
// newHash.put("5","5");
// newHash.put("3","5555555555555");
// newHash.put("6","6");
// newHash.put("7","7");newHash.put("3","323333");newHash.put("4","44444444");newHash.putAll(old);System.out.println("newHash:" +" value:" );for (String key:newHash.keySet()){System.out.println("key:" +key+" value:" +newHash.get(key));}old.putAll(newHash);System.out.println("old" );for (String key:old.keySet()){System.out.println("key:" +key+" value:" +old.get(key));}}/**** 结论D:\root\JDK8\bin\java.exe "-javaagent: newHash: value:key:1 value:1key:2 value:2key:3 value:3key:4 value:4oldkey:1 value:1key:2 value:2key:3 value:3key:4 value:4*/
也就是说putALL并不会把原来的数值覆盖掉
2、现在的HashMap会对put数值进行重新排序
public class ReflectTest {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {test2();}public static void test2(){HashMap<String ,String> old=new HashMap();HashMap<String ,String> newHash=new HashMap();HashMap<String ,String> newOrderHash=new HashMap();old.put("1","1");old.put("2","2");old.put("3","3");old.put("4","4");newHash.put("5","5");newHash.put("3","5555555555555");newHash.put("6","6");newHash.put("7","7");newHash.put("1","3233333333333");newHash.put("4","4444444444444");System.out.println("-------------如何重新排序--------------" );for (String key:old.keySet()){if(newHash.containsKey(key)){newOrderHash.put(key,newHash.get(key));}}for (String key:newOrderHash.keySet()){System.out.println("key:" +key+" value:" +newOrderHash.get(key));}System.out.println("-------------重新排序--------------" );for (String key:old.keySet()){newOrderHash.putIfAbsent(key,old.get(key));}for (String key:newOrderHash.keySet()){System.out.println("key:" +key+" value:" +newOrderHash.get(key));}}/**** 结论-------------如何重新排序--------------key:1 value:3233333333333key:3 value:5555555555555key:4 value:4444444444444-------------重新排序--------------key:1 value:3233333333333key:2 value:2key:3 value:5555555555555key:4 value:4444444444444Process finished with exit code 0*/
3、即使乱序,目前HashMap依然会对put数值进行排序整理
public class ReflectTest {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {test2();}public static void test2(){HashMap<String ,String> old=new HashMap();HashMap<String ,String> newHash=new HashMap();HashMap<String ,String> newOrderHash=new HashMap();old.put("3","3");old.put("4","4");old.put("1","1");old.put("2","2");newHash.put("5","5");newHash.put("3","5555555555555");newHash.put("6","6");newHash.put("7","7");newHash.put("1","3233333333333");newHash.put("4","4444444444444");System.out.println("-------------如何重新排序--------------" );for (String key:old.keySet()){if(newHash.containsKey(key)){newOrderHash.put(key,newHash.get(key));}}for (String key:newOrderHash.keySet()){System.out.println("key:" +key+" value:" +newOrderHash.get(key));}System.out.println("-------------重新排序--------------" );for (String key:old.keySet()){newOrderHash.putIfAbsent(key,old.get(key));}for (String key:newOrderHash.keySet()){System.out.println("key:" +key+" value:" +newOrderHash.get(key));}}/**** 结论-------------如何重新排序--------------key:1 value:3233333333333key:3 value:5555555555555key:4 value:4444444444444-------------重新排序--------------key:1 value:3233333333333key:2 value:2key:3 value:5555555555555key:4 value:4444444444444Process finished with exit code 0*/
4、教训总结是:HashMap依旧会对key值进行排序
public class ReflectTest {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {test2();}public static void test2(){HashMap<String ,String> newHash=new HashMap();newHash.put("1","1");newHash.put("2","2");newHash.put("7","7");newHash.put("8","3233333333333");newHash.put("5","5");newHash.put("3","5555555555555");newHash.put("6","6");newHash.put("4","4444444444444");System.out.println("-------------如何重新排序--------------" );for (String key:newHash.keySet()){System.out.println("key:" +key+" value:" +newHash.get(key));}}/***-------------如何重新排序--------------key:1 value:1key:2 value:2key:3 value:5555555555555key:4 value:4444444444444key:5 value:5key:6 value:6key:7 value:7key:8 value:3233333333333Process finished with exit code 0*/
这篇关于HashMap前后顺序put的数值教训总结的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!