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()); } }
作者:奋斗的珞珞
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.