package demo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
public class Text1 {
public static void main(String[] args) {
query();
}
public static void query() {
// TODO Auto-generated method stub
HashMap<String,List<VO>> map = new HashMap<String,List<VO>>();
// VO vo = new VO("no1234",23234);
// vo.setName("no");
ArrayList<VO> nl = new ArrayList<VO>();
// nl.add(new VO("no1",1));
for( int i = 1 ;i<10;i++){
nl.add(new VO("no"+i,i));
}
for (VO VO : nl) {
System.out.println(VO);
}
List<VO> list1 = new ArrayList<VO>();
List<VO> list2 = new ArrayList<VO>();
//for(VO index : nl){
//int indexId = index.getId();
for(int i = 0;i < nl.size();i++){
int indexId = nl.get(i).getId();
switch (indexId) {
case 1:
list1.add(nl.get(i));
break;
case 2:
list1.add(nl.get(i));
break;
case 3:
list1.add(nl.get(i));
break;
case 4:
list1.add(nl.get(i));
break;
default:
list2.add(nl.get(i));
}
/* for(int i =0;i<nl.size();i++){
if (i<4){
list1.add(nl.get(i));
}else{
list2.add(nl.get(i));
}
}*/
map.put("table", list1);
map.put("list", list2);
}
for (String s:map.keySet()) {
System.out.println(s);
for (VO VO : map.get(s)) {
System.out.println(VO);
}
}
for(Entry<String, List<VO>> s :map.entrySet()){
System.out.println(s);
}
System.out.println(map);
}
static class VO {
public VO(String name,int id){
this.name = name;
this.id = id;
}
String name;
int id;
public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}
public String getName(){
return name;
}
public void setName(String name ){
this.name = name;
}
public String toString(){
return id+" "+name;
}
}
}
结果:
1 no1
2 no2
3 no3
4 no4
5 no5
6 no6
7 no7
8 no8
9 no9
list
5 no5
6 no6
7 no7
8 no8
9 no9
table
1 no1
2 no2
3 no3
4 no4
list=[5 no5, 6 no6, 7 no7, 8 no8, 9 no9]
table=[1 no1, 2 no2, 3 no3, 4 no4]
{list=[5 no5, 6 no6, 7 no7, 8 no8, 9 no9], table=[1 no1, 2 no2, 3 no3, 4 no4]}
摘抄:
public class MapUse{
public static void main(String[] args) {
Map<String,String> map = new HashMap<Integer,String>();
map.put(“1”, "a");
map.put(“2”, "b");
}
public static void run(Map<Integer,String> map) {
//遍历key
for (Integer key : map.keySet()) {
System.out.println("key:"+ key +" value:"+ map.get(key));
}
//迭代
/*Set<Map.Entry<Integer,String>> set = map.entrySet();
Iterator<Map.Entry<Integer,String>> it = set.iterator();*/
Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, String> entry = it.next();
System.out.println("key:"+ entry.getKey() +" value:"+ entry.getValue());
}
//遍历 key value
for (Map.Entry<Integer, String> entry : map.entrySet()) {
System.out.println("key"+ entry.getKey() +" value:"+ entry.getValue());
}
//遍历值
for (String values : map.values()) {
System.out.println("value:"+ values);
}
}
}