JAVA Collections Framework Set Interfaces Implementations JAVA Collections

  • Slides: 14
Download presentation
JAVA Collections Framework Set Interfaces & Implementations ﺳﺎﺧﺘﻤﺎﻥ ﺩﺍﺩﻩ ﻫﺎ ﻭ ﺍﻟگﻮﺭﻳﺘﻢ ﻫﺎ

JAVA Collections Framework Set Interfaces & Implementations ﺳﺎﺧﺘﻤﺎﻥ ﺩﺍﺩﻩ ﻫﺎ ﻭ ﺍﻟگﻮﺭﻳﺘﻢ ﻫﺎ

JAVA Collections Framework ، ﻣﺠﻤﻮﻋﻪ ﺍﺑﺰﺍﺭﻫﺎ : JAVA Collections Framework ﻗﺮﺍﺭﺩﺍﺩﻫﺎ ﻭ چﺎﺭچﻮﺑﻬﺎﻱ ﻳکﺴﺎﻥ

JAVA Collections Framework ، ﻣﺠﻤﻮﻋﻪ ﺍﺑﺰﺍﺭﻫﺎ : JAVA Collections Framework ﻗﺮﺍﺭﺩﺍﺩﻫﺎ ﻭ چﺎﺭچﻮﺑﻬﺎﻱ ﻳکﺴﺎﻥ ﺑﺮﺍﻱ پﻴﺎﺩﻩ ﺳﺎﺯﻱ ﺍﻧﻮﺍﻉ ﻣﺠﻤﻮﻋﻪ ﻫﺎﻱ پﻮﻳﺎ Stack, queue, list, hash table, map l Collections Framework – Interfaces l – Implementation l – Abstract Data Type Representation of Collections JAVA class definition of the ADT ‘s Algorithms l How to sort, search. . ? – l

Collection Interface public interface Collection<E> extends Iterable<E> { // Basic operations int size(); boolean

Collection Interface public interface Collection<E> extends Iterable<E> { // Basic operations int size(); boolean is. Empty(); boolean contains(Object element); boolean add(E element); //optional boolean remove(Object element); //optional Iterator<E> iterator(); // Bulk operations boolean contains. All(Collection<E> c); boolean add. All(Collection<E> c); //optional boolean remove. All(Collection<E> c); //optional boolean retain. All(Collection<E> c); //optional void clear(); //optional // Array operations Object[] to. Array(); <T> T[] to. Array(T[] a); }

Set Interface public interface Set<E> extends Collection<E> { // Basic operations int size(); boolean

Set Interface public interface Set<E> extends Collection<E> { // Basic operations int size(); boolean is. Empty(); boolean contains(Object element); boolean add(E element); //optional boolean remove(Object element); //optional Iterator<E> iterator(); // Bulk operations boolean contains. All(Collection<? > c); boolean add. All(Collection<? extends E> c); //optional boolean remove. All(Collection<? > c); //optional boolean retain. All(Collection<? > c); //optional void clear(); //optional // Array Operations Object[] to. Array(); <T> T[] to. Array(T[] a); }

 ﻣﺠﻤﻮﻋﻪ ﺍﻱ ﺍﺯ ﺭﺷﺘﻪ ﻫﺎ : 1 ﻣﺜﺎﻝ Set<String> s = new Hash.

ﻣﺠﻤﻮﻋﻪ ﺍﻱ ﺍﺯ ﺭﺷﺘﻪ ﻫﺎ : 1 ﻣﺜﺎﻝ Set<String> s = new Hash. Set<String>(); for (String a : args) if (!s. add(a)) System. out. println(a); System. out. println(s. size() + " distinct words: " + s);

 ﺗﻔﺎﺿﻞ ﻣﺘﻘﺎﺭﻥ : 2 ﻣﺜﺎﻝ l l l l Set <String> s 1=

ﺗﻔﺎﺿﻞ ﻣﺘﻘﺎﺭﻥ : 2 ﻣﺜﺎﻝ l l l l Set <String> s 1= new Hash. Set<String>() ; Set <String> s 2 = new Hash. Set<String>() ; Set <String> union = new Hash. Set<String>(s 1); union. add. All(s 2) ; Set <String> inter = new Hash. Set<String>(s 1); Inter. retain. All(s 2) ; Set <String>diff = new Hash. Set<String>(union); Diff. remove. All(inter);