set.list.map接口

时间:2015-04-19 16:29:58
【文件属性】:
文件名称:set.list.map接口
文件大小:18KB
文件格式:DOCX
更新时间:2015-04-19 16:29:58
接口 1. Set(集合)里面的元素是无序的,但没有重复的元素 2. 两个实现类HashSet(LinkHashSet)和TreeSet,TreeSet有排序功能(Set set=new TreeSet();set.add(new Integer(8)); set.add(new Integer(4)); set.add(new Integer(7));)输出后的结果是:4 7 8 Eg: package test; import java.util.*; public class Set{ public static void main(String[] args) { //先实例化一个set Set stringSet=new HashSet(); //向set里面添加元素 stringSet.add("123"); stringSet.add("wer"); stringSet.add("345"); //将set里的元素取出 Iterator stringIter=stringSet.iterator(); while(stringIter.hasNext()){ String str=stringIter.next(); System.out.println(str); System.out.println("~~~~~~~~~~~"); } System.out.println("stringSet里面有"+stringSet.size()+"元素"); } } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~2.List(列表<接口>)以线性方式存储,有序,允许重复主要实现类有LinkList(采用链表数据结构)和ArrayList(代表可大可小的数组) Eg: package test; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; public class Map { public static void main(String[] args) { List list=new ArrayList(); list.add(10); list.add(2); list.add(34); //对list数组进行自然排序 Collections.sort(list); //依次检索输出list的所有对象 // for(int i=0;i)是无序的,是一种把键对象和值对象进行映射的集合,它每一个元素都包含一对键对象和值对象,给出键对象就可以得到值对象,键对象不允许重复,对值没有要求,多个任意键对象可以映射到一个值对象上;如果有相同键对象,最后一次加入的键对象和值对象将会覆盖以前的; Eg: package test; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.TreeMap; public class NewMap { public static void main(String[] args) { //向map里添加键值对 //如果要对键进行排序Map map=new TreeMap(); Map map=new TreeMap(); //Map map=new HashMap(); map.put(1, "yi"); map.put(23, "er"); map.put(12, "san"); map.put(3, "si"); //遍历map Set keys=map.keySet(); Iterator stringIter=keys.iterator(); while(stringIter.hasNext()){ int key=stringIter.next(); String value=(String) map.get(key);//根据键key得到value的值 System.out.println(key+"---"+value); } } }

网友评论