Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 46 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
46
Dung lượng
1,44 MB
Nội dung
Trần Thị Thanh Nga ngattt@hcmuaf.edu.vn Khoa Công nghệ thông tin, ĐH Nông Lâm HCM Set Collection Linear: set, stack, queue, hashtable… Non-linear: tree, graph… Non-Linear collection Linear collection Set Java Collection Set Method in the Java Collection interface No Method Description public boolean add(Object element) Insert an element in this collection public boolean addAll(Collection c) Insert the specified colletion elements in the collection public boolean remove(Object element) Delete an element from this collection public boolean removeAll(Collection c) Delete all the elements of invoking collection except the specified collection public boolean retainAll(Collection c) Delete all the elements of invoking collection except the specified collection public int size() Return the total number of elements in the collection public void clear() Removes the total no of element from the collection Set Method in the Java Collection interface No Method Description public boolean contains(Object element) Search an element public boolean containsAll(Collection c) Search the specified collection in this collection 10 public Iterator iterator() Return an iterator 11 public Object[] toArray() Convert collection into array 12 public boolean isEmpty() Check if collection is empty 13 public boolean equals(Object o) Match collection 14 public int hashCode() Return the hashcode number for collection Set Set A set is an abstract data type that can store certain values, without any particular order, and no repeated values It is a computer implementation of the mathematical concept of a finite set Example: we use set to capture: The courses someone has registered for, The destinations served by an airline, Or someone’s friends in a social network Set Set Sets have two key characteristics ( properties): They contain no duplicates The items within the set are unordered Set Set Abstract Data Type Set public interface SetADT { // Add one element to this set, ignoring duplicates public void add(T element); // Add all elments of set into this set, ignoring duplicates public void addAll(SetADT set); // Removes and returns a random element from this set public T removeRandom(); //Removes and returns the specified element from this set public T remove(T element); Set // Return the union of this set and the parameter public SetADT union(SetADT set); // Return true if this set contains the parameter public boolean contains (T target); //Return true if this set and the parameter contain exactly the same elements public boolean equals(SetADT set); // Return true if this set is empty public boolean isEmpty(); // Return the number of elements in the set public int size(); //Return an iterator for the elements in the set public Iterator iterator(); //Return a string representation of the set public String toString(); } Set ArrayIterator class //The remove operation is not supported in this collection public void remove() throws UnsupportedOperationException { throw new UnsupportedOperationException(); } } Set Array-based implementation SetADT interface ArraySet class ArrayIterator class EmptySetException class and ElementNotFoundException class Set EmptySetException class public class EmptySetException extends RuntimeException{ public EmptySetException(){ super ("The set is empty."); } // Creates the exception with the specified message public EmptySetException (String message){ super (message); } } Set ElementNotFoundException class public class ElementNotFoundException extends RuntimeException{ // Sets up this exception with an appropriate message public ElementNotFoundException (String collection) { super ("The target element is not in this " + collection); } } Set Linkedlist-based implementation Set Java Collection HashSet class LinkedHashSet class Set Java HashSet Uses hashtable to store the elements It extends AbstractSet class and implements Set interface Contains unique elements only Set Java HashSet Difference between List and Set List can contain duplicate elements whereas Set contains unique elements only Set Hierarchy of HashSet class Set Example HashSet al = new HashSet(); al.add("Ravi"); al.add("Vijay"); al.add("Ravi"); al.add("Ajay"); Iterator itr = al.iterator(); while (itr.hasNext()) { System.out.println(itr.next()); } Set Java Collection HashSet class LinkedHashSet class Set Java LinkedHashSet class Contains unique elements only like HashSet It extends HashSet class and implements Set interface Maintains insertion order Set Example LinkedHashSet al=new LinkedHashSet(); al.add("Ravi"); al.add("Vijay"); al.add("Ravi"); al.add("Ajay"); Iterator itr=al.iterator(); while(itr.hasNext()){ System.out.println(itr.next()); } Set Reference http://www.tutorialspoint.com/ http://www.javapoint.com https://en.wikipedia.org Set Question? Set ... NOT_FOUND); } Set public boolean equals (SetADT set) { boolean result = false; T obj; ArraySet temp1 = new ArraySet(); ArraySet temp2 = new ArraySet(); if (size() == set. size()){... specified element from this set public T remove(T element); Set // Return the union of this set and the parameter public SetADT union(SetADT set) ; // Return true if this set contains the parameter... implementation SetADT interface: above ArraySet class ArrayIterator class EmptySetException and ElementNotFoundException class Set ArraySet class public class ArraySet implements SetADT{