java学习二(集合类)

package com.jihe.mytest;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

/*
* Java容器类类库分类:
Collection:一组独立的元素,通常这些元素都符合某种规则。(例如List必须保持元素特定的顺序,Set不能有重复元素)
Map:一组成对的“键值对”对象。

Collection – 对象之间没有指定的顺序,允许重复元素。
Set – 对象之间没有指定的顺序,不允许重复元素
List– 对象之间有指定的顺序,允许重复元素,并引入位置下标。
Map – 接口用于保存关键字(Key)和数值(Value)的集合,集合中的每个对象加入时 都提供数值和关键字。Map 接口既不继承 Set 也不继承 Collection。

List、Set、Map共同的实现基础是Object数组。

接口 实现 历史集合类
Set HashSet
TreeSet
List ArrayList Vector
LinkedList Stack
Map HashMap Hashtable
TreeMap Properties

*/

public class CollectionTest {
public static void main(String[] args){
//Collection
/*
集合(Collection):
Collection<String> collection1=new ArrayList<String>();//创建一个集合对象
collection1.add("000");//添加对象到Collection集合中
collection1.add("111");
collection1.add("222");
System.out.println("集合collection1的大小:"+collection1.size());
System.out.println("集合collection1的内容:"+collection1);
collection1.remove("000");//从集合collection1中移除掉 "000" 这个对象
System.out.println("集合collection1移除 000 后的内容:"+collection1);
System.out.println("集合collection1中是否包含000 :"+collection1.contains("000"));
System.out.println("集合collection1中是否包含111 :"+collection1.contains("111"));
Collection collection2=new ArrayList();
collection2.addAll(collection1);//将collection1 集合中的元素全部都加到collection2中
System.out.println("集合collection2的大小:"+collection2.size());
System.out.println("集合collection2的内容:"+collection2);
collection2.clear();//清空集合 collection1 中的元素
System.out.println("集合collection2是否为空 :"+collection2.isEmpty());
//将集合collection1转化为数组
Object s[]= collection1.toArray();
for(int i=0;i<s.length;i++){
System.out.println(s[i]);
}
*/
Collection<String> collection1=new ArrayList<String>();//创建一个集合对象
collection1.add("000");//添加对象到Collection集合中
collection1.add("111");
collection1.add("222");
System.out.println("集合collection1的大小:"+collection1.size());
System.out.println("集合collection1的内容:"+collection1);
collection1.remove("000");//从集合collection1中移除掉 "000" 这个对象
System.out.println("集合collection1移除 000 后的内容:"+collection1);
System.out.println("集合collection1中是否包含000 :"+collection1.contains("000"));
System.out.println("集合collection1中是否包含111 :"+collection1.contains("111"));
Collection<String> collection2=new ArrayList<String>();
collection2.addAll(collection1);//将collection1 集合中的元素全部都加到collection2中
System.out.println("集合collection2的大小:"+collection2.size());
System.out.println("集合collection2的内容:"+collection2);
collection2.clear();//清空集合 collection1 中的元素
System.out.println("集合collection2是否为空 :"+collection2.isEmpty());
//将集合collection1转化为数组
Object s[]= collection1.toArray();
for(int i=0;i<s.length;i++){
System.out.println(s[i]);
}

//Iterator
/*
迭代器(Iterator)本身就是一个对象,它的工作就是遍历并选择集合序列中的对象,而客户端的程序员不必知道或关心该序列底层的结构
*/
Collection<String> coll = new ArrayList<String>();
coll.add("s1");
coll.add("s2");
coll.add("s3");
Iterator iterator = coll.iterator();//得到一个迭代器
while (iterator.hasNext()) {//遍历
Object element = iterator.next();
System.out.println("iterator = " + element);
}
if(coll.isEmpty())
System.out.println("collection is Empty!");
else
System.out.println("collection is not empty!");


//List
/*
List:是容器的一种,表示列表的意思。对象之间有指定的顺序,允许重复元素,并引入位置下标。
void add(int index, Object element) :添加对象element到位置index上
boolean addAll(int index, Collection collection) :在index位置后添加容器collection中所有 的元素
Object get(int index) :取出下标为index的位置的元素
int indexOf(Object element) :查找对象element 在List中第一次出现的位置
int lastIndexOf(Object element) :查找对象element 在List中最后出现的位置
Object remove(int index) :删除index位置上的元素
Object set(int index, Object element) :将index位置上的对象替换为element 并返回老的元素
List的实现有两种方式:ArrayList和LinkedList
ArrayList提示快速的基于索引的成员访问,对尾部成员的增加和删除支持较好,成员可为任意Object子类的对象
LinkedList对列表中任何位置的成员的增加和删除支持较好,但对于索引的成员访问支持性能较差,成员可为任意Object子类的对象
因此,如果要支持随机访问,而不必在除尾部的任何位置插入或除去元素,那么,ArrayList 提供了可选的集合。但如果,您要频繁的从列表的中间位置添加和除去元素,而只要顺序的访问列表元素,那么,LinkedList 实现更好。

List接口处理集合的子集:
ListIterator listIterator() :返回一个ListIterator 跌代器,默认开始位置为0
ListIterator listIterator(int startIndex) :返回一个ListIterator 跌代器,开始位置为startIndex
List subList(int fromIndex, int toIndex) :返回一个子列表List ,元素存放为从 fromIndex 到 toIndex之前的一个元素
*/
List<String> ll = new ArrayList<String>();
ll.add("aaa");
ll.add("bbb");
ll.add("ccc");
ll.add("ddd");
System.out.println("下标0开始:"+ll.listIterator(0).next());//next()
System.out.println("下标1开始:"+ll.listIterator(1).next());
System.out.println("子List 1-3:"+ll.subList(1,3));//子列表
ListIterator<String> it = ll.listIterator();//默认从下标0开始
//隐式光标属性add操作 ,插入到当前的下标的前面
it.add("sss");
while(it.hasNext()){
System.out.println("next Index="+it.nextIndex()+",Object="+it.next());
}

//set属性
ListIterator<String> it1 = ll.listIterator();
it1.next();
it1.set("ooo"); //将上面设置的sss元素改成ooo
ListIterator<String> it2 = ll.listIterator(ll.size());//下标
while(it2.hasPrevious()){
System.out.println("previous Index="+it2.previousIndex()+",Object="+it2.previous());
}

//Map
/*
Map:不是 Collection 接口的继承,而是从自己的用于维护键-值关联的接口层次结构入手。按定义,该接口描述了从不重复的键到值的映射。
Object put(Object key,Object value):用来存放一个键-值对Map中
Object remove(Object key):根据key(键),移除一个键-值对,并将值返回
void putAll(Map mapping) :将另外一个Map中的元素存入当前的Map中
void clear() :清空当前Map中的元素
Object get(Object key) :根据key(键)取得对应的值
boolean containsKey(Object key) :判断Map中是否存在某键(key)
boolean containsValue(Object value):判断Map中是否存在某值(value)
int size():返回Map中 键-值对的个数
boolean isEmpty() :判断当前Map是否为空
public Set keySet() :返回所有的键(key),并使用Set容器存放。因为映射中键的集合必须是唯一的,就使用 Set 来支持。
public Collection values() :返回所有的值(Value),并使用Collection存放。因为映射中值的集合可能不唯一,就使用 Collection 来支持。
public Set entrySet() :返回一个实现 Map.Entry 接口的元素 Set

Map的实现:
HashMap:能满足用户对map的通用需求。键成员可以为任意的Object子类的对象,但如果覆盖了equals方法,同时注意修改hashCode方法。
TreeMap:支持对键有序的遍历,使用时建议先用HashMap增加和删除成员,最后从HashMap生成TreeMap;附加实现了SortedMap接口,支持子Map等要求顺序的操作。键成员要求实现Compareble接口,或者使用Comparator构造TreeMap,键成员一般为同一类型。
LinkedHashMap:保留键的插入顺序,用equals方法检查键和值的相等性。成员可为任意的Object子类对象,但如果覆盖了equals方法,同时注意修改hashCode方法。
*/
Map<String,String> map1 = new HashMap<String,String>();
Map<String,String> map2 = new HashMap<String,String>();
map1.put("1","aaa1");
map1.put("2","bbb2");
map2.put("10","aaaa10");
map2.put("11","bbbb11");
//根据键 "1" 取得值:"aaa1"
System.out.println("map1.get(1)="+map1.get("1"));
// 根据键 "1" 移除键值对"1"-"aaa1"
System.out.println("map1.remove(1)="+map1.remove("1"));
System.out.println("map1中键值对的个数:"+map1.size());
System.out.println("map1.get(1)="+map1.get("1"));
map1.putAll(map2);//将map2全部元素放入map1中
map2.clear();//清空map2
System.out.println("map1 IsEmpty?="+map1.isEmpty());
System.out.println("map2 IsEmpty?="+map2.isEmpty());
System.out.println("map1 中的键值对的个数size = "+map1.size());
System.out.println("KeySet="+map1.keySet());//set
System.out.println("values="+map1.values());//Collection
System.out.println("entrySet="+map1.entrySet());
System.out.println("map1 是否包含键:11 = "+map1.containsKey("11"));
System.out.println("map1 是否包含值:aaa1 = "+map1.containsValue("aaa1"));

//Map的排序

Map<Integer,String> m1 = new HashMap<Integer,String>();
Map m2 = new LinkedHashMap();
for(int i=0;i<3;i++){
double sr=Math.random()*100;//产生一个随机数,并将其放入Map中
m1.put(new Integer((int) sr),"第 "+i+" 个放入的元素:"+sr+"¥n");
m2.put(new Integer((int) sr),"第 "+i+" 个放入的元素:"+sr+"¥n");
}
System.out.println("未排序前HashMap:"+m1); //HashMap的存入顺序和输出顺序无关
System.out.println("未排序前LinkedHashMap:"+m2); //LinkedHashMap的输出顺序与存入顺序相同
//使用TreeMap来对另外的Map进行重构和排序
Map sortedMap = new TreeMap(m1); //TreeMap是对Map中元素进行排序
System.out.println("排序后:"+sortedMap);
System.out.println("排序后:"+new TreeMap(m2));


//Set
/*
* Set:继承 Collection 接口,而且它不允许集合中存在重复项。
public int size() :返回set中元素的数目,如果set包含的元素数大于Integer.MAX_VALUE,返回Integer.MAX_VALUE
public boolean isEmpty() :如果set中不含元素,返回true
public boolean contains(Object o) :如果set包含指定元素,返回true
public Iterator iterator() :返回set中元素的迭代器;元素返回没有特定的顺序,除非set是提高了该保证的某些类的实例
public Object[] toArray() :返回包含set中所有元素的数组
public Object[] toArray(Object[] a) :返回包含set中所有元素的数组,返回数组的运行时类型是指定数组的运行时类型
public boolean add(Object o) :如果set中不存在指定元素,则向set加入
public boolean remove(Object o) :如果set中存在指定元素,则从set中删除
public boolean removeAll(Collection c) :如果set包含指定集合,则从set中删除指定集合的所有元素
public boolean containsAll(Collection c) :如果set包含指定集合的所有元素,返回true。如果指定集合也是一个set,只有是当前set的子集时,方法返回true
public boolean addAll(Collection c) :如果set中中不存在指定集合的元素,则向set中加入 所有元素
public boolean retainAll(Collection c) :只保留set中所含的指定集合的元素(可选操作)。 换言之,从set中删除所有指定集合不包含的元素。 如果指定集合也是一个set,那么该操作修改set的效果是使它的值为两个set的交集
public void clear() :从set中删除所有元素

Set的常用实现类:
HashSet:外部无序地遍历成员。成员可为任意Object子类 的对象,但如果覆盖了equals方法,同时注意修改hashCode方法
TreeSet:外部有序地遍历成员;附加实现了SortedSet,支持子集等要求顺序的操作。成员要求实现 Comparable接口,或者使用Comparator构造 TreeSet。成员一般为同一类型。
LinkedHashSet:外部按成员的插入顺序遍历成员。成员与HashSet成员类似。
*/
Set set1 = new HashSet();
if (set1.add("a")) {//添加成功
System.out.println("1 add true");
}
if (set1.add("a")) {//添加失败
System.out.println("2 add true");
}else{
System.out.println("2 add failed");
}
set1.add("000");//添加对象到Set集合中
set1.add("111");
set1.add("222");
System.out.println("集合set1的大小:"+set1.size());
System.out.println("集合set1的内容:"+set1);
set1.remove("000");//从集合set1中移除掉 "000" 这个对象
System.out.println("集合set1移除 000 后的内容:"+set1);
System.out.println("集合set1中是否包含000 :"+set1.contains("000"));
System.out.println("集合set1中是否包含111 :"+set1.contains("111"));
Set set2=new HashSet();
set2.add("111");
set2.addAll(set1);//将set1 集合中的元素全部都加到set2中
System.out.println("集合set2的内容:"+set2);
set2.clear();//清空集合 set1 中的元素
System.out.println("集合set2是否为空 :"+set2.isEmpty());
Iterator iterator1 = set1.iterator();//得到一个迭代器
while (iterator1.hasNext()) {//遍历
Object element = iterator1.next();
System.out.println("iterator = " + element);
}
//将集合set1转化为数组
Object ss[]= set1.toArray();
for(int i=0;i<ss.length;i++){
System.out.println(ss[i]);
}
//Set排序
Set<Integer> s1 = new HashSet<Integer>();
Set<Integer> s2 = new LinkedHashSet<Integer>();
for(int i=0;i<5;i++){
int ssss=(int) (Math.random()*100); //产生一个随机数,并将其放入Set中
s1.add(new Integer(ssss));
s2.add(new Integer(ssss));
System.out.println("第 "+i+" 次随机数产生为:"+ssss);
}
System.out.println("未排序前HashSet:"+s1);
System.out.println("未排序前LinkedHashSet:"+s2);
//使用TreeSet来对另外的Set进行重构和排序
Set<Integer> sortedSet = new TreeSet<Integer>(s1);
System.out.println("排序后的HashSet :"+sortedSet);
System.out.println("排序后的LinkedHashSet :"+new TreeSet(s2));
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值