java集合中map的有关问题

版权声明: https://blog.csdn.net/if_i_were_a/article/details/82777423

Map是一个接口,hashmap和treemap是map的实现类,Map是通过key来管理value的

map中的key是一个set,所以map中的key值不能重复,同样的treemap是一个有序的map,其中key要实现comparable接口,重写compareto方法,如果是自定义的排序的类的话需要实现comprator接口,重写compare方法

理解map思路一:把map中的key看成一个set不能重复

public class Demo01 {

public static void main(String[] args) {

//把map中的key看成是一个set

Map map=new HashMap();

map.put("lisi","陕西");

map.put("lisi","陕西2");

map.put(2,"ddd");

map.put(false,new Date());

//map中的key是一个set

Set keySet=map.keySet();

Iterator it=keySet.iterator();

while(it.hasNext())

{

Object key=it.next();

Object value=map.get(key);

System.out.println(key+"------"+value);

}

}

}

理解map的思路二:把map看成一个set,set中间的元素看成是一种key和value的数据结构

public class Demo02 {

public static void main(String[] args) {

//把map看成是一种set

Map map=new Hashtable();

map.put("lisi","陕西");

map.put("lisi","陕西2");

map.put("cc","ddd");

Set<Map.Entry<String,String>> set=map.entrySet();

Iterator<Map.Entry<String,String >> it=set.iterator();

while(it.hasNext())

{

Map.Entry<String,String> entry= it.next();

System.out.println(entry.getKey()+"---"+entry.getValue());

}

}

}

关于java.utils.properties也是一个map的实现类

只是他的key和value都是字符串,经常我们将外部的文本文件(.properties)和java.utils.properties关联,properties表示了一个持久的属性集,可以保存在流中,或从流中加载。

public class Demo01 {

public static void main(String[] args)throws Exception {

Properties p=new Properties();

p.setProperty("aa","lisi");

p.put("bb","wangwu");

p.setProperty("cc","wangwu");

Iterator it=(p.keySet().iterator());

while (it.hasNext())

{

String key=(String)it.next();

String value=p.getProperty(key);

System.out.println(key+"------"+value);

}

System.out.println("从外部加载到内存...");

Properties p1=new Properties();

p1.load(new FileInputStream("e:\\aaa.properties"));

p1.list(System.out);

}

}

国际化I18N

就是我们写的一个软件在不同的国家和地区显示不同的语言

本地化L10N

一个国家化的程序,根据本地区的区位信息来显示本地化的语言,方法:

根据本地区信息,加载不同的资源来显示软件界面

public class Demo02 {

public static void main(String[] args) {

//java.util.Local类,表示国际和语言信息

Locale[] all=Locale.getAvailableLocales();

//返回所有已安装环境的数组

for(Locale l:all)

{

System.out.println(l);

}

System.out.println(Locale.CANADA);

System.out.println(new Locale("en","US"));

//获得java虚拟机实例的当前默认语言环境值

System.out.println(Locale.getDefault());

}

}

猜你喜欢

转载自blog.csdn.net/if_i_were_a/article/details/82777423