0
点赞
收藏
分享

微信扫一扫

HashMap前后顺序put的数值教训总结


1、层级结构是Map>需要进行排序

使用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:1
key:2 value:2
key:3 value:3
key:4 value:4
old
key:1 value:1
key:2 value:2
key:3 value:3
key: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:3233333333333
key:3 value:5555555555555
key:4 value:4444444444444
-------------重新排序--------------
key:1 value:3233333333333
key:2 value:2
key:3 value:5555555555555
key:4 value:4444444444444

Process 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:3233333333333
key:3 value:5555555555555
key:4 value:4444444444444
-------------重新排序--------------
key:1 value:3233333333333
key:2 value:2
key:3 value:5555555555555
key:4 value:4444444444444

Process 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:1
key:2 value:2
key:3 value:5555555555555
key:4 value:4444444444444
key:5 value:5
key:6 value:6
key:7 value:7
key:8 value:3233333333333
Process finished with exit code 0
*/



举报

相关推荐

0 条评论