set、map集合list接口实例
2014-07-07 14:51
369 查看
1.Set(集合)里面的元素是无序的,但没有重复的元素有两个实现类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<String> stringSet=new HashSet<String>();
//向set里面添加元素
stringSet.add("123");
stringSet.add("wer");
stringSet.add("345");
//将set里的元素取出
Iterator<String> 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<list.size();i++){
//System.out.println(list.get(i));
//}
Iterator Iter=list.iterator();
while(Iter.hasNext()){
System.out.println(Iter.next());
}
}
}
3.Map(映射<集合>)是无序的,是一种把键对象和值对象进行映射的集合,它每一个元素都包含一对键对象和值对象,给出键对象就可以得到值对象,键对象不允许重复,对值没有要求,多个任意键对象可以映射到一个值对象上;如果有相同键对象,最后一次加入的键对象和值对象将会覆盖以前的;
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<Integer,String> map=new TreeMap<Integer,String>();
//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<Integer> stringIter=keys.iterator();
while(stringIter.hasNext()){
int key=stringIter.next();
String value=(String) map.get(key);//根据键key得到value的值
System.out.println(key+"---"+value);
}
}
}
Eg:
package test;
import java.util.*;
public class Set{
public static void main(String[] args) {
//先实例化一个set
Set<String> stringSet=new HashSet<String>();
//向set里面添加元素
stringSet.add("123");
stringSet.add("wer");
stringSet.add("345");
//将set里的元素取出
Iterator<String> 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<list.size();i++){
//System.out.println(list.get(i));
//}
Iterator Iter=list.iterator();
while(Iter.hasNext()){
System.out.println(Iter.next());
}
}
}
3.Map(映射<集合>)是无序的,是一种把键对象和值对象进行映射的集合,它每一个元素都包含一对键对象和值对象,给出键对象就可以得到值对象,键对象不允许重复,对值没有要求,多个任意键对象可以映射到一个值对象上;如果有相同键对象,最后一次加入的键对象和值对象将会覆盖以前的;
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<Integer,String> map=new TreeMap<Integer,String>();
//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<Integer> stringIter=keys.iterator();
while(stringIter.hasNext()){
int key=stringIter.next();
String value=(String) map.get(key);//根据键key得到value的值
System.out.println(key+"---"+value);
}
}
}
相关文章推荐
- Collection集合之六大接口(Collection、Set、List、Map、Iterator和Comparable)
- 【java读书笔记】——Collection集合之六大接口(Collection、Set、List、Map、Iterator和Comparable)
- Collection集合之六大接口(Collection、Set、List、Map、Iterator和Comparable)
- JAVA集合 Set(集)、List(列表)、Map(映射)、Collection(接口)
- hibernate List,Set,Map集合的配置实例
- Spring集合 (List,Set,Map,Properties) 实例
- Java集合定义与用法实例总结【Set、List与Map】
- Java 中的集合接口——List、Set、Map
- 【java读书笔记】——Collection集合之六大接口(Collection、Set、List、Map、Iterator和Comparable)
- 【java读书笔记】——Collection集合之六大接口(Collection、Set、List、Map、Iterator和Comparable)
- 集合接口(List,Set,Map)List(序列)排序--5,6
- Collection集合之六大接口(Collection、Set、List、Map、Iterator和Comparable)
- Collection集合之六大接口(Collection、Set、List、Map、Iterator
- 【java随记】——Collection集合之六大接口(Collection、Set、List、Map、Iterator和Comparable)
- Java基础知识强化之集合框架笔记67:Map集合面试题之List,Set,Map等接口是否继承自Map接口
- 在创建实例变量的时候用Set,List,Map等接口声明类型而不是用具体的子类声明的原因之代码重构
- List,Set,Map是否继承自Collection接口? 答:List,Set是,Map不是。 Collection是最基本的集合接口,一个Collection代表一组Object,即Colle
- 【java读书笔记】——Collection集合之六大接口(Collection、Set、List、Map、Iterator和Comparable)
- Spring集合 (List,Set,Map,Properties) 实例
- Spring集合 (List,Set,Map,Properties) 实例