JAVA Collections Framework Set Interfaces & Implementations

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
Collections CS3250. Sources  Slides by Professor Chuck Allison  Core Java, by Cay S. Horstmann and Gary Cornell  The Java Tutorial 
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
15-Jun-15 Lists in Java Part of the Collections Framework.
Professor Evan Korth (adapted from Sun’s collections documentation)
What Is a Collection?  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 8.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Data Structures & Java Collections Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
12-Jul-15 Lists in Java Part of the Collections Framework.
The Collections Framework A Brief Introduction. Collections A collection is a structured group of objects –An array is a kind of collection –A Vector.
The Java Collections Package C. DeJong Fall 2001.
Java's Collection Framework
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Java Collections Framework A presentation by Eric Fabricant.
Sets and Maps Part of the Collections Framework. The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
Advanced Java Session 3 New York University School of Continuing and Professional Studies.
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
Sets and Maps Chris Nevison. Set Interface Models collection with no repetitions subinterface of Collection –has all collection methods has a subinterface.
The Java Collections Framework Based on
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
1 Collections Framework A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain:
1 Interfaces in Java’s Collection Framework Rick Mercer.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
SETS AND MAPS Collections of Data. Advanced Data Structures Often referred to as the Java Collections Framework…. Set and map data types Hash tables Binary.
CS Ananda Gunawardena.  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
4-Mar-16 Introduction to Collections. Revision questions True false questions 0 for False 1 for True Please do not answer anything other than the above.
Collections Dwight Deugo Nesa Matic
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Slide 1 Collections – Part I. Slide 2 Lesson Overview Topics covered: (1) Introduction to the Java Collections framework (2) Collection interfaces: Collection.
CS2005 Week 7 Lectures Set Abstract Data Type.
Java Collections CHAPTER 3-February-2003
Java Collections OOP tirgul No
OOP Tirgul 5.
Java's Collection Framework
Sets Set is an unordered list of items
Efficiency of in Binary Trees
Programming & Data Structures
Interfaces in Java’s Collection Framework
Introduction to Collections
Introduction to Collections
TCSS 342, Winter 2006 Lecture Notes
Lecture 6: Collections.
Practical Session 3 Java Collections
Introduction to Collections
Introduction to Collections
Collections A First Glimpse.
Part of the Collections Framework
Practical Session 4 Java Collections
Collections in Java The objectives of this lecture are:
Introduction to Collections
Collections Framework
CSE 1020: The Collection Framework
Introduction to Collections
Computer Science 209 The Adapter Pattern.
Collections A First Glimpse 16-Apr-19.
Data Structures II AP Computer Science
Part of the Collections Framework
Java Generics & Iterators
Presentation transcript:

JAVA Collections Framework Set Interfaces & Implementations ساختمان داده ها و الگوريتم ها

Collection Collection : دسته اي از اشياء هم نوع عمليات روي Collection { 1و و2و 2و3و4و4و5و5} ، روزهاي هفته عمليات روي Collection مرتب سازي جستجو حذف و اضافه نمودن اعضا ذخيره و بازيابي Storage/Retrieve

مزاياي استفاده از Collections کاهش برنامه نويسي افزايش کيفيت و سرعت برنامه ها ارتباط آسانتر نرم افزارهاي مختلف کاهش زمان يادگيري توابع و متدهاي لازم کاهش زمان طراحي توابع و متدهاي جديد امکان استفاده مجدد از نرم افزارهاي مبتني بر Collections Framework

JAVA Collections Framework Stack, queue, list, hash table, map Collections Framework Interfaces Abstract Data Type Representation of Collections Implementation JAVA class definition of the ADT ‘s Algorithms How to sort, search ..?

Interface Interface == ADT قراردادهايي براي نحوه دسترسي به مجموعه پويا مثال: Stack ADT ساختمان داده پشته را تعريف مي کند. داده هاي Stack : آرايه اي از اعضا عمليات روي Stack :حذف و اضافه نمودن اعضا، بررسي پر يا خالي بودن Stack و... JAVA Collections Framework چندين Interface‌براي پشتيباني ساختمان داده هاي معروف تعريف مي کند

Core Collection Interfaces Map : تابع ( به مفهوم رياضي)‌را تعريف مي کند هر عضو دامنه به يک و فقط يک عضو برد اختصاص مي يابد.

Collection هاي خاص Set: يک مجموعه به مفهوم رياضي را تعريف مي کند SortedSet: نوع خاصي از Set است که اعضاي به صورت مرتب قرار گرفته اند. الگوريتم هاي جستجو روي اين مجموعه ها، بسيار سريعتر از مجموعه هاي نا مرتب کار مي کنند. List : دسته اي از اشياء به صورت مرتب است. برخلاف Set در list مي توان عضوي تکراري داشت

queue queue (صف)ساختار داده اي است که در آن تريب دسترسي به اعضا، با ترتيب ورود اعضا به queue معين مي شود هر عضوي که زودتر وارد queue زودتر در دسترس قرار مي گيرد اين ساختار First in First Out ‌ است

Collection Interface public interface Collection<E> extends Iterable<E> { // Basic operations int size(); boolean isEmpty(); boolean contains(Object element); boolean add(E element); //optional boolean remove(Object element); //optional Iterator<E> iterator(); // Bulk operations boolean containsAll(Collection<E> c); boolean addAll(Collection<E> c); //optional boolean removeAll(Collection<E> c); //optional boolean retainAll(Collection<E> c); //optional void clear(); //optional // Array operations Object[] toArray(); <T> T[] toArray(T[] a); }

Set Interface public interface Set<E> extends Collection<E> { // Basic operations int size(); boolean isEmpty(); boolean contains(Object element); boolean add(E element); //optional boolean remove(Object element); //optional Iterator<E> iterator(); // Bulk operations boolean containsAll(Collection<?> c); boolean addAll(Collection<? extends E> c); //optional boolean removeAll(Collection<?> c); //optional boolean retainAll(Collection<?> c); //optional void clear(); //optional // Array Operations Object[] toArray(); <T> T[] toArray(T[] a); }

Set Implementation HashSet : اعضاي يك مجموعه را در HashTable نگهداري و مديريت مي كند. ازنظر كارايي، HashSet بهترين پياده سازي است . اين روش ترتيب خاصي روي اعضا اعمال نمي كند TreeSet : اعضاي مجموعه را به صورت مرتب در يك Red-Black Tree نگهداري مي كند كندتر از HashSet عمل مي كند اما ترتيب اعضا را حفظ مي كند LinkedHashSet اعضاي مجموعه را در يك HashTable مجهز به Linked List نگهداري مي كند. تريب اعضا، بر اساس ترتيب اضافه شدن به مجموعه تعيين مي شود

مثال 1: مجموعه اي از رشته ها Set<String> s = new HashSet<String>(); for (String a : args) if (!s.add(a)) System.out.println(a); System.out.println(s.size() + " distinct words: " + s);

مثال 2 : تفاضل متقارن Set <String> s1= new HashSet<String>() ; Set <String> s2 = new HashSet<String>() ; Set <String> union = new HashSet<String>(s1); union.addAll(s2) ; Set <String> inter = new HashSet<String>(s1); Inter.retainAll(s2) ; Set <String>diff = new HashSet<String>(union); Diff.removeAll(inter);

تمرين با استفاده از Set و يكي از پياده سازي هاي آن ، برنامه اي بنويسيد كه آرايه اي از رشته ها را دريافت كرده و عناصر غير تكراري آن را چاپ كند مهلت ارسال : تا پايان خرداد