熟悉一下list,map,json,菜鸟温习

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);
        }
    }
   }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值