PR3_Ch03_Collections_and_Streams Java Lecture Notes PDF
Document Details
![WellMadeSerpentine1417](https://quizgecko.com/images/avatars/avatar-13.webp)
Uploaded by WellMadeSerpentine1417
Islamic University of Gaza
Dr. Abdelkareem Alashqar
Tags
Summary
These are lecture notes on Java Collections and Streams, covering topics such as Java Collections Framework, Collection Interface, the Collections Framework, and more. Includes information on functional programming, Lambda expressions, and Streams.
Full Transcript
Programming 3 CSCI 2308 Chapter 3 Java Collections and Streams Topics Covered Java Collections Framework Collection Interface and Collections Class The Collections Framework Collections Methods, Lists, ArrayLists, Sets, Maps Functional...
Programming 3 CSCI 2308 Chapter 3 Java Collections and Streams Topics Covered Java Collections Framework Collection Interface and Collections Class The Collections Framework Collections Methods, Lists, ArrayLists, Sets, Maps Functional Programming Technologies Functional Interfaces Lambda Expressions Streams Intermediate Stream Operations Terminal Stream Operations Jan 18, 2025 Created by: Dr. Abdelkareem Alashqar 2 Java Collections Framework The Java Collections Framework is a library of classes and interfaces for working with collections of objects. Collections cannot manipulate variables of primitive types. A collection is an object which can store other objects, called elements. Collections provide methods for adding and removing elements, and for searching for a particular element within the collection. Jan 18, 2025 Created by: Dr. Abdelkareem Alashqar 3 Some Collections Framework Interfaces Interface Description Collection The root interface in the collections hierarchy from which interfaces Set, Queue and List are derived. Set A collection that does not contain duplicates. List An ordered collection that can contain duplicate elements. Map A collection that associates keys to values and cannot contain duplicate keys. Map does not derive from Collection. Queue Typically a first-in, first-out collection that models a waiting line; other orders can be specified. Jan 18, 2025 Created by: Dr. Abdelkareem Alashqar 4 Java Collection Framework Hierarchy Jan 18, 2025 Created by: Dr. Abdelkareem Alashqar 5 Collection Interface and Collections Class Interface Collection contains methods for adding, clearing and comparing objects in a collection. A Collection can be converted to an array. Interface Collection provides a method that returns an Iterator object. Iterator object allows a program to walk through the collection and remove elements from the collection during the iteration. Class Collections provides static methods that search, sort and perform other operations on collections. Jan 18, 2025 Created by: Dr. Abdelkareem Alashqar 6 Some Methods in the Collection Interface Applied to Lists and Sets Method Description add(o : E) : boolean Adds an object o to the Collection. The method returns true if o is successfully added to the collection, false otherwise. clear() : void Removes all elements from the collection. contains(o : Object): Returns true if o is an element of the collection, false boolean otherwise. isEmpty() : boolean Returns true if there are no elements in the collection, false otherwise. iterator() : Iterator Returns an object called an iterator that can be used to examine all elements stored in the collection. remove(o : Object) : Removes the object o from the collection and returns true if boolean the operation is successful, false otherwise. size() : int Returns the number of elements currently stored in the collection. Jan 18, 2025 Created by: Dr. Abdelkareem Alashqar 7 Lists A List is an ordered Collection that can contain duplicate elements. In addition to the methods inherited from Collection, List provides methods for: Manipulating elements via their indices, Searching for elements and Obtaining a ListIterator to access the elements. List interface is implemented by several classes, such as ArrayList, LinkedList and Vector. Jan 18, 2025 Created by: Dr. Abdelkareem Alashqar 8 Lists (cont.) Class ArrayList and Vector are resizable-array implementations of List. Inserting an element between existing elements of an ArrayList or Vector is an inefficient غير فعالoperation. A LinkedList enables efficient insertion (or removal) of elements in the middle of a collection, but is much less efficient than an ArrayList for jumping to a specific element in the collection. The main difference between ArrayList and Vector is that operations on Vectors are synchronized by default, whereas those on ArrayLists are not. Unsynchronized collections provide better performance than synchronized ones. For this reason, ArrayList is typically preferred over Vector in programs that do not share a collection among threads. Jan 18, 2025 Created by: Dr. Abdelkareem Alashqar 9 The List Interface Methods Method Description add(index:int, el:E) : void Adds the element el to the collection at the given index. Throws IndexOutOfBoundsException if index is negative, or greater than the size of the list. get(index:int):E Returns the element at the given index, or throws IndexOutBoundsException if index is negative or greater than or equal to the size of the list. indexOf(o:Object):int Returns the least (first) index at which the object o is found; returns -1 if o is not in the list. lastIndexOf(o:Object):int Returns the greatest (last) index at which the object o is found; returns -1 if o is not in the list. listIterator():ListIterator Returns an iterator specialized to work with List collections. remove(index:int):E Removes and returns the element at the given index; throws IndexOutOfBoundsException if index is negative, or greater than or equal to the size of the list. set(index:int, el:E):E Replaces the element at index with the new element el. contains(o: Object): boolean Return true if the element o is exits, false otherwise. Jan 18, 2025 Created by: Dr. Abdelkareem Alashqar 10 public static void main(String[] args) { String[] Colors = {"RED","GREEN","BLUE"}; //List colorList = new ArrayList(); ArrayList colorList = new ArrayList(); //fetch array items into ArrayList List list= for(int i=0; i