一:set接口的实现类
(1)set接口常用的实现类:HashSet和TreeSet
(2)声明方式:
Set<String> set1=new HashSet<String>();
set<String> set2=new TreeSet<String>();
由于set集合是无序的,遍历set集合的结果与插入set集合的顺序并不相同。
二:set集合的常用方法
add()添加元素(返回值Boolean)
addAll()将集合中所有的元素添加到set集合的尾部(set集合中不许存在重复值,使用addAll()方法,可以将元素存入到
set集合中,并去掉的重复值。)
remove()将指定的参数对象移除集合(Boolean)
retainAll()只保留set集合中包含在指定的Collection集合中的内容
removeAll()在set集合中移除包含在指定Collection中的元素
clear()移除此set中的所有元素(void)
iterator()返回set中元素上进行迭代的迭代器(Iterator)
size()返回set集合中所有的元素数(int)
isEmpty()判断set是否为空(Boolean)
实例一:创建list集合对象,并向list集合中添加元素,在创建set集合,利用addAll()方法将list集合对象存入到set集合中
并去掉重复值,最后打印set集合的元素。
public static void main(String[] args){
List<String> list=new ArrayList<String>();
list.add("apple");
list.add("pear");
list.add("banana");
list.add("apple");
Set<String> set=new HashSet<String>();
set.addAll(list);
Iterator<String> it=set.iterator();
System.out.println("集合中的元素是:");
while(it.hasNext()){
System.out.println(it.next()+"\t");
}
}
运行结果:
实例二:遍历HashSet中的全部元素(建立2个类People类和CollectionDemo类)
public class People{
private String name;
private long id_card;
public People(String name,long id_card){
this.name=name;
this.id_card=id_card;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getId_card() {
return id_card;
}
public void setId_card(long id_card) {
this.id_card = id_card;
}
}
public class CollectionDemo{
public static void main(String[] args){
Set<People> hashSet=new HashSet<People>();
hashSet.add(new People("陈同学",201101));
hashSet.add(new People("王同学",201102));
hashSet.add(new People("李同学",201123));
Iterator(People> it=hashSet.iterator();
System.out.println("集合中的元素是:");
while(it.hashNext()){
People person=it.next();
System.out.println(person.getName()+" "+people.getId_card());
}
}
三:ArrayList和HashSet的区别(顺序和重复性)
1:是否有顺序:ArrayList有顺序,HashSet无顺序
(HashSet的具体顺序,既不是按照插入顺序,也不是按照hashcode的顺序。关于hashcode有专门的章节讲解)
2:能否重复
list中的数据可以重复,Set中的数据不能够重复。
重复判断的标准:首先看hashcode是否相同,如果不同,则认为是不同的数据。
如果hashcode相同,在比较equals,如果equals相同,则认为是相同的数据,否则是不同的数据。
代码一:看ArrayList和HashSet的顺序。
public static void main(String[] args) {
ArrayList<Integer> l=new ArrayList<Integer>();
//按照插入的顺序存放
System.out.println("_______List 1 7 3_________");
l.add(1);
l.add(7);
l.add(3);
System.out.println(l);
HashSet<Integer> s=new HashSet<Integer>();
System.out.println("_____HashSet 1 7 3________");
s.add(1);
s.add(7);
s.add(3);
System.out.println(s);
}
三:几种Set:HashSet,LinkedHashSet ,TreeSet
1:HashSet 无序
2:LinkedHashSet 按照性顺序排序
3:TreeSet 从小到大的排序
package collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.TreeSet;
public class TestCollection {
public static void main(String[] args) {
HashSet<Integer> numberSet1 =new HashSet<Integer>();
//HashSet中的数据不是按照插入顺序存放
numberSet1.add(88);
numberSet1.add(8);
numberSet1.add(888);
System.out.println(numberSet1);
LinkedHashSet<Integer> numberSet2 =new LinkedHashSet<Integer>();
//LinkedHashSet中的数据是按照插入顺序存放
numberSet2.add(88);
numberSet2.add(8);
numberSet2.add(888);
System.out.println(numberSet2);
TreeSet<Integer> numberSet3 =new TreeSet<Integer>();
//TreeSet 中的数据是进行了排序的
numberSet3.add(88);
numberSet3.add(8);
numberSet3.add(888);
System.out.println(numberSet3);
}
}
四:Set和Map的关联之处。
1:Map集合是Set集合的扩展。
2:Set和Map的关系,Map接口的实现类和Set接口的实现类的类名完全相似,只需把Map后缀改为Set后缀。
set——>EnumSet,SortedSet,HashSet
——>(SortSet)Navigableset,(HashSet)LinkedHashSet
——>(NavigableSet)TreeMap。
map集合的继承体系。
map——>EnumMap ,SortedMap,HashMap,Hashtable,WeakHashMap,IdentityHashMap。
——>(SortMap)NavigaleMap,(HashMap)LinkedHashMap, (HashTable)Properties
——>(NavigableMap)TreeMap。
3:(***)Map集合的key的特征:所有的key不能重复,key之间没有顺序。
——》将Map集合的key集中起来,那么这些key组成了一个Set集合。
——》Map集合提供了一个方法返回所有key组成的Set集合。Set<K> keySet()。
——》Map集合的所有的key将具有Set集合的特征。只有把Map的所有key集中起来看,那就是一个Map。这就实现了Map到Set的转换。
——》如何将一个Set集合扩展为Map集合(将Map集合中的key-value对捆绑在一起)