Java集合Set List Map

Set,List,Map的区别和功能到底是怎样的?其实它是与数组区分开来的。

数组:java数组的长度是固定的,在同一个数组中只能存放相同类型的数据,可以是基本数据类型,也可

以存放引用类型的数据。

java集合:存入于java.util包中,它不能存放基本类型数据,而只能存放对象的引用。

Set(集):集合中的对象不按特定方式排序,并且没有重复对象。它的有些实现类能对集合中的对象按

特定方式排序。与数学中的集合最接近,两者都不包含重复元素。

List(列表):集合中的对象按照索引位置排序,可以有重复对象,允许按照对象在集合中的索引位置检

索对象。List与数组有些相似。

Map(映射):集合中的第一个元素包含一对键对象和值对象,集合中没有重复的键对象,值可以重复。

它的有些实现类能对集合中的键对象进行排序。切记在用到MAP时一定需要传入两个参数

键和值。

例:

importjava.util.*;

publicstaticvoidprint(Collection<?extendsObject>c){

Iterator<?extendsObject>it=c.iterator();

//遍历集合中的所有元素

while(it.hasNext()){

Objectelement=it.next();

System.out.println(element);

}

}

publicstaticvoidmain(Stringargs[]){

Set<String>set=newHashSet<String>();//<String>指定集合中元素的类型。

set.add("Tom");

set.add("Mary");

set.add("Jack");

print(set);

List<String>list=newArrayList<String>();

list.add("Linda");

lis.add("Mary");

list.add("Rose");

print(list);

Map<String,String>map=newHashMap<String,String>();//两个参数,键和值

map.put("M","男");

map.put("F","女");//与实现了Collection接口的set,list加入元素的方法不一样。

print(map.entrySet());//entrySet()返回SET视图

}

}

要知道两个异常:ConcurrentModificatiojException和ClassCastException

1、对一个集合不能够一边修改一边操作(除了用Iterator进行修改)不然将抛出ConcurrentModificatiojException异常。

2、当试图将对象强制转换为不是实例的时,抛出ClassCastException异常。

Set类

由上图中可知实现Set接口的两个类有HashSet(无序)和TreeSet(实现了SortedSet接口、有序)

1)TreeSet支持两种排序:自然排序(默认)和客户排序

自然排序:实现了Comparable接口,通过compareTo(Objecto)方法比较。只能向TreeSet集合中加入同类

型的对象,并且这些对象要实现Comparable接口。

例1:

Set<Object>set=newTreeSet<Object>();

set.add(newInteger(8);

set.add(newString("9");//抛出ClassCastException异常

例2:

Test类没有实现了Comparable接口,带有一个String和int的成员变量。

Set<Object>set=newTreeSet<Object>();

set.add(newTest("A",5);

set.add(newTest("B",6);//抛出ClassCastException异常

这也是用TreeSet时要注意的地方

2)HashSet:按照哈希算法来存取集合中的对象,具有很好的查找性能。当向集合中加入一个对象时,HashSet会调用对象的hashCode()方法来获得哈希码,然后根据这个哈希码进一步计算出对象在集合中的存放位置。跟数据结构中的散列存储概念一样。

要明白一点的就是:当用户覆盖了object类的equals()方法,一定也要重写hashCode方法,而且TreetSet还要增加一个compareTo方法。

例:

importjava.util.*;

publicclassDemo2implementsComparable{

privateStringname;

privateintage;

publicDemo2(Stringa,intb){

name=a;

age=b;

}

publicintgetAge(){

returnage;

}

publicvoidsetAge(intage){

this.age=age;

}

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

this.name=name;

}

publicintcompareTo(Objecto){

Demo2other=(Demo2)o;

//先按照name属性排序

if(this.name.compareTo(other.getName())>0){System.out.println("1");return1;}

if(this.name.compareTo(other.getName())<0){System.out.println("2");return-1;}

//再按照age属性排序

if(this.age>other.getAge()){System.out.println("3");return1;}

if(this.age<other.getAge()){System.out.println("4");return-1;}

return0;

}

publicbooleanequals(Objecto){

if(this==o)returntrue;

if(!(oinstanceofDemo2))returnfalse;

finalDemo2other=(Demo2)o;

if(this.name.equals(other.getName())&&this.age==other.getAge())

returntrue;

else

returnfalse;

}

publicinthashCode(){

intresult;

result=(name==null?0:name.hashCode());

result=29*result+age;

returnresult;

}

publicstaticvoidmain(Stringagrs[]){

Set<Demo2>set=newTreeSet<Demo2>();

Demo2a=newDemo2("Tom",15);

Demo2b=newDemo2("Apple",16);

set.add(a);

set.add(b);

Iterator<Demo2>it=set.iterator();

while(it.hasNext()){

Demo2Demo2=it.next();

System.out.println(Demo2.getName()+""+Demo2.getAge());

}

}

}

以上三个方法的重写必须理解。

List类:主要特征是其元素以线性方式存储,集合中允许存放重复对象。

ArrayList可以说是线性表,LinkedList可以说是链表。并不是自动排序的而是要通过Collection接口中sort()方法

例:

importjava.util.*;

publicclassDemo1{

publicstaticvoidinsert(List<Integer>list,intdata){

ListIterator<Integer>it=list.listIterator();

while(it.hasNext()){

Integerin=it.next();

if(data<=in.intValue()){

it.previous();

it.add(newInteger(data));

break;

}

}

}

publicstaticvoidmain(String[]args){

List<Integer>list=newLinkedList<Integer>();

list.add(newInteger(3));

list.add(newInteger(2));

list.add(newInteger(5));

list.add(newInteger(9));

Collections.sort(list);

insert(list,6);

System.out.println(Arrays.toString(list.toArray()));//toArray()返回一数组,Arrays.toString()输出数组。

inta[]=newint[]{1,2,3};

System.out.print(Arrays.toString(a));

}

}

Map类:对容量,初始容量,大小,负载因子要知道这些知识点。

Mapmap=newHashMap();

for(inti=0;i<args.length;i++){

map.put(newInteger(i),args[i]);

}

System.out.println("HashMap的容量:"+map.size());

System.out.println(map);

相关推荐