• Map的遍历方法及String和其它类型的相互转化


    • Map的遍历方法:

    package com.lky.test;
    
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Map.Entry;
    
    import org.junit.Test;
    
    public class mapTest {
        
        public static String []keys=new String[]{"name","sex","email","phone"};
        public static String []values=new String[]{"lky","male","1411074230@qq.com","016515151"};
        public static Map<String,String> info=new HashMap<String,String>();
        static{
              for(int i=0;i<values.length;++i){
                  info.put(keys[i], values[i]);
              }
        }
        
        
        @Test
        public void testMap(){
            for(Entry<String, String> entry:info.entrySet()){
                System.out.println(entry.getKey()+"---"+entry.getValue());
            }
            System.out.println("---------------");
        }
        
        @Test
        public void testMap1(){
            for(String key:info.keySet()){
                System.out.println(key+"--------"+info.get(key));
            }
            System.out.println("---------------");
        }
        
        @Test
        public void testMap2(){
            Iterator<Entry<String, String>> iterator=info.entrySet().iterator();
            while(iterator.hasNext()){
                Entry<String, String> s=iterator.next();
                System.out.println(s.getKey()+"----------"+s.getValue());
            }
        }
    }
    • String和其它数据类型的相互转化

    package com.lky.test;
    
    import java.io.UnsupportedEncodingException;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import org.junit.Test;
    
    public class stringToElse {
        
        //字符串和byte数组之间的转化
        @Test
        public void test() throws UnsupportedEncodingException{
            String s="lky";
            byte[] info=s.getBytes();
            
            String s1=new String(info,"utf-8");
            System.out.println(s1);
        }
        
        //字符串和Date之间的转化
        @Test
        public void test1() throws ParseException{
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            Date date=  sdf.parse("2015-10-14");//将字符串转化成Date类型
            System.out.println(date);
            
            System.out.println(sdf.format(date));//将日期类型转化成字符串类型
        }
        
        //字符串和int之间的转化
        @Test
        public void test2(){
            String s="1000";
            int integer=Integer.parseInt(s);
            System.out.println(integer);
            
            System.out.println(Integer.valueOf(integer).toString());
        }
        
    
    }
  • 相关阅读:
    设计模式----单例模式
    C++ 派生类
    C++ 操作符
    构造,清理,拷贝和移动
    php的yii框架开发总结10
    php的yii框架开发总结9
    php的yii框架开发总结8
    php的yii框架开发总结7
    php的yii框架开发总结6
    php的yii框架开发总结5
  • 原文地址:https://www.cnblogs.com/dmir/p/4878470.html
Copyright © 2020-2023  润新知