对象集合(Set) 盖 ■数学上“集合”瓶念的抽象,无序 A Set is a collection that cannot contain duplicate elements. ■ 集合与元素 集合之间 ■ 无序集合和有序集合 6
6 ◼ 数学上“集合”概念的抽象,无序 ◼ A Set is a collection that cannot contain duplicate elements. ◼ 集合与元素 ◼ 集合之间 ◼ 无序集合和有序集合 对象集合(Set)
对象集合(Set) 急 java.util.Set接口(集合与元素) public boolean add(Object o) Adds the specified element to this set if it is not already present. ■ public boolean remove(Object o) Removes the specified element from this set if it is present. public boolean contains(Object o) Returns true if this set contains the specified element. ■ public int size( Returns the number of elements in this set. 7
7 ◼ java.util.Set 接口 (集合与元素) ◼ public boolean add(Object o) Adds the specified element to this set if it is not already present. ◼ public boolean remove(Object o) Removes the specified element from this set if it is present. ◼ public boolean contains(Object o) Returns true if this set contains the specified element. ◼ public int size() Returns the number of elements in this set. 对象集合(Set)
D:\java FindDups1 i came i saw i left 对象集合(Set Duplicate:i Duplicate:i java.util.Hash$4 distinct words:[came,left,saw,il ■无序集合 D import java.util.*; public class SetDemo1 public static void main(String args[]){ Set s new HashSet(); for (int i=0;i<args.length;i++) if (!s.add(args[i])) System.out.println("Duplicate:"+args[i]); System.out.println(s.size()+"distinct words:"+s); 8
8 ◼ java.util.HashSet implement java.util.Set ◼ 无序集合 对象集合(Set) import java.util.*; public class SetDemo1 { public static void main(String args[]) { Set s = new HashSet(); for (int i=0; i<args.length; i++) if (!s.add(args[i])) System.out.println("Duplicate: "+args[i]); System.out.println(s.size()+" distinct words: "+s); } } D:\java FindDups1 i came i saw i left Duplicate: i Duplicate: i 4 distinct words: [came, left, saw, i] D:\
对象集合(Set) 急 java.util.Set接口(集合之间) ■ public boolean containsAll(Collection c) Returns true if this set contains all of the elements of the specified collection. public boolean addAll(Collection c) Adds all of the elements in the specified collection to this set if they're not already present. ■ public boolean removeAll(Collection c) Removes from this set all of its elements that are contained in the specified collection. public boolean retainAll(Collection c) Retains only the elements in this set that are contained in the specified collection. 9
9 ◼ java.util.Set接口 (集合之间) ◼ public boolean containsAll(Collection c) Returns true if this set contains all of the elements of the specified collection. ◼ public boolean addAll(Collection c) Adds all of the elements in the specified collection to this set if they're not already present. ◼ public boolean removeAll(Collection c) Removes from this set all of its elements that are contained in the specified collection. ◼ public boolean retainAll(Collection c) Retains only the elements in this set that are contained in the specified collection. 对象集合(Set)
D:\java FindDups2 i came i saw i left 对象集合(Set i Unique words:[came,left,saw] Duplicate words:[i] java.util.Hashs import java.util.*; D public class SetDemo2 public static void main(String args[]){ Set uniques new HashSet(); Set dups new HashSet(); for (int i=0;i<args.length;i++) if (!uniques.add(args[i])) dups.add(args[i]); uniques.removeAll(dups); System.out.println("Unique words:"uniques); System.out.printin("Duplicate words:"dups); 10
10 ◼ java.util.HashSet implement java.util.Set 对象集合(Set) import java.util.*; public class SetDemo2 { public static void main(String args[]) { Set uniques = new HashSet(); Set dups = new HashSet(); for (int i=0; i<args.length; i++) if (!uniques.add(args[i])) dups.add(args[i]); uniques.removeAll(dups); System.out.println("Unique words: " + uniques); System.out.println("Duplicate words: " + dups); } } D:\java FindDups2 i came i saw i left Unique words: [came, left, saw] Duplicate words: [i] D:\