HashMap<String,HashMap<String,String[]>> map = new HashMap<String,HashMap<String,String[]>>(); //存储省份,文件名字,用户名及密码
//省份ID
String provId="811";
//省份文件列表
String file1 = "12345678979.dat";
String file2 = "22345678989.dat";
String file3 = "32345678999.dat";
//省份文件列表对应的上传用户名以及密码
String a1[] = new String[]{"user1","23456"};
String a2[] = new String[]{"user2","34567"};
String a3[] = new String[]{"user3","45678"};
//Map存储 key:文件名 value:用户名及密码 length=2 array[0]="username",array[1]="password"
HashMap<String,String[]> map1 = new HashMap<String,String[]> ();
map1.put(file1, a1);
map1.put(file2, a2);
map1.put(file3, a3);
//Map存储 Key:省份ID value: 存储map1
map.put("811", map1);
想要的结果:
811
12345678979.dat "user1","23456"
22345678989.dat "user2","34567"
32345678999.dat "user3","45678"
------解决方案--------------------
package Test;
import java.util.*;
public class HashMapTest {
public static void main(String[] args)
{
TreeMap<String,TreeMap<String,String[]>> map = new TreeMap<String,TreeMap<String,String[]>>();
//省份ID
//String provId="811";
//省份文件列表
String file1 = "12345678979.dat";
String file2 = "22345678989.dat";
String file3 = "32345678999.dat";
//省份文件列表对应的上传用户名以及密码
String a1[] = new String[]{"user1","23456"};
String a2[] = new String[]{"user2","34567"};
String a3[] = new String[]{"user3","45678"};
//Map存储 key:文件名 value:用户名及密码 length=2 array[0]="username",array[1]="password"
TreeMap<String,String[]> map1 = new TreeMap<String,String[]> ();
map1.put(file1, a1);
map1.put(file2, a2);
map1.put(file3, a3);
//Map存储 Key:省份ID value: 存储map1
map.put("811", map1);//.put("811", map1);
Iterator<String> it = map.keySet().iterator();
while(it.hasNext())
{
String provId = it.next();
System.out.println(provId);
Map<String,String[]> value = map.get(provId);
for(Map.Entry<String, String[]> me : value.entrySet())
{
String fileName = me.getKey();
String[] user_password = me.getValue();
System.out.println(fileName+" "+Arrays.toString(user_password));
}
}
}
}
LZ想要取出来得顺序,把HashMap换成了TreeMap 原理是一样的,因为TreeMap会个ulimian的元素做自然排序,如火用HashMap取出的话,是无顺序的,这样就是LZ想要的结果了
------解决方案--------------------
LinkedHashMap
会按照你添加的顺序取出