Sets, For-Each Loops, Iterators - Enhancing Data Structures Usage

Sets, For-Each Loops, Iterators - Enhancing Data Structures Usage
Slide Note
Embed
Share

Dive into the world of sets, for-each loops, and iterators in CSE 122 Winter 2025 lecture. Explore the tradeoffs with different data structures, practical problem-solving, and how to leverage these concepts in programming assignments. Get ready for a hands-on learning experience with announcements, practice problems, and insightful reviews. Don't miss the opportunity to improve your coding skills and understand the power of unique words counting through a large text file. Join the session to elevate your understanding of data manipulation techniques.

  • Data Structures
  • Programming
  • Sets
  • Iterators
  • CSE 122

Uploaded on Apr 12, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. LEC 08: Sets, For-Each Loops, Iterators CSE 122 Winter 2025 BEFORE WE START Slido chat only: LEC 08 What s your best snow day memory? CSE 122 CSE 122 122 25wi Lecture Tunes Music: 122 25wi Lecture Tunes Sets, For-Each Loops, Iterators Instructor: Elba Garza Anya Ashley Cady Caleb Carson Chaafen Colin Connor Dalton Daniel Ryan Diya Elizabeth Hannah Harshitha Ivory Izak Jack Jacob Ken Kuhu Kyle Leo Logan Maggie Mahima Marcus Minh Nicole Nicole Niyati Sai Steven Yang Zach TAs: Questions during Class? Raise hand or send here sli.do

  2. LEC 08: Sets, For-Each Loops, Iterators CSE 122 Winter 2025 Lecture Outline Announcements Practice Problem Sets Review Tradeoffs with Different Data Structures For-Each Loop Iterators

  3. LEC 08: Sets, For-Each Loops, Iterators CSE 122 Winter 2025 Announcements Programming Assignment 1 (P1) due tomorrow! - Stacks, Queues, Exceptions Resubmission Cycle 1 was due yesterday - Remember that grades from a resubmission completely replace your previous grades for that assignment. - Resubmission Cycle 2 will open tomorrow Heads up: Quiz 1 scheduled for Tuesday, February 18 - ArrayLists, Reference Semantics, Stacks and Queues, Sets, Maps How to Use the IPL Programming Assignment 2 released on Friday, February 7th - Yes, two Programming Assignments in a row! - BUT, you have two weeks to complete this assignment

  4. LEC 08: Sets, For-Each Loops, Iterators CSE 122 Winter 2025 Lecture Outline Announcements Practice Problem Sets Review Tradeoffs with Different Data Structures For-Each Loop Iterators

  5. LEC 08: Sets, For-Each Loops, Iterators CSE 122 Winter 2025 Practice Problem: Write a program that, given a Scanner over a large text file (e.g., Moby Dick or the King James Bible), counts the number of unique wordsin the text.

  6. LEC 08: Sets, For-Each Loops, Iterators CSE 122 Winter 2025 Lecture Outline Announcements Practice Problem Sets Review Tradeoffs with Different Data Structures For-Each Loop Iterators

  7. LEC 08: Sets, For-Each Loops, Iterators CSE 122 Winter 2025 (PCM) Sets (ADT) A collection of unique values (no duplicates allowed) that can perform the following operations efficiently: - add - remove - search (contains) "hi" "hola" "hello" "bonjour" "konnichiwa" We don t think of a set as having indices; we just add things to the set in general and don t worry about order

  8. LEC 08: Sets, For-Each Loops, Iterators CSE 122 Winter 2025 (PCM) Sets in Java Set is an interface in Java - In java.util HashSet and TreeSet are classes that implement the Set interface in Java - HashSet: Very fast! Implemented using a hash table array - Elements are stored in an unpredictable order - TreeSet: Pretty fast! Implemented using a binary search tree - Elements are stored in sorted order

  9. LEC 08: Sets, For-Each Loops, Iterators CSE 122 Winter 2025 Set Methods Method Description Adds the given value to the set, returns whether or not the given value was added successfully add(value) Returns true if the given value is found in this set contains(value) Removes the given value from the set; returns true if the set contained the value, false if not remove(value) Removes all elements from the set clear() Returns the number of elements in list size() Returns trueif the set s size is 0; false otherwise isEmpty() Returns a String representation of the set such as "[3, 42, -7, 15]" toString()

  10. LEC 08: Sets, For-Each Loops, Iterators CSE 122 Winter 2025 Lecture Outline Announcements Practice Problem Sets Review Tradeoffs with Different Data Structures For-Each Loop Iterators

  11. LEC 08: Sets, For-Each Loops, Iterators CSE 122 Winter 2025 Choosing a Data Structure: Tradeoffs You got a bit of practice with this in your quiz sections on Tuesday! - Solving the same problem with an ArrayList, a Stack, and a Queue Things to consider: - Functionality - If you need duplicates or indexing, Sets are not for you! - Efficiency - Different data structures are good at different things!

  12. LEC 08: Sets, For-Each Loops, Iterators CSE 122 Winter 2025 Lecture Outline Announcements Practice Problem Sets Review Tradeoffs with Different Data Structures For-Each Loop Iterators

  13. LEC 08: Sets, For-Each Loops, Iterators CSE 122 Winter 2025 For-Each Loop A new kind of loop! Set<String> words = new HashSet<>(); for (String s : words) { System.out.println(s); } BUT, you cannot modify the data structure inside a for-each loop - You will get a ConcurrentModificationException - They are read-only

  14. LEC 08: Sets, For-Each Loops, Iterators CSE 122 Winter 2025 Practice : Think sli.do #cse122 What output is produced by this code? A. -2 0 3 9 Set<Integer> nums = new TreeSet<>(); nums.add(3); nums.add(9); nums.add(3); nums.add(-2); nums.add(0); B. 3 9 3 -2 0 C. 9 3 0 -2 D. -2 0 3 3 9 for (int n : nums) { System.out.print(n + " "); } E. ConcurrentModificationException

  15. LEC 08: Sets, For-Each Loops, Iterators CSE 122 Winter 2025 Practice : Pair sli.do #cse122 What output is produced by this code? A. -2 0 3 9 Set<Integer> nums = new TreeSet<>(); nums.add(3); nums.add(9); nums.add(3); nums.add(-2); nums.add(0); B. 3 9 3 -2 0 C. 9 3 0 -2 D. -2 0 3 3 9 for (int n : nums) { System.out.print(n + " "); } E. ConcurrentModificationException

  16. LEC 08: Sets, For-Each Loops, Iterators CSE 122 Winter 2025 Lecture Outline Announcements Practice Problem Sets Review Tradeoffs with Different Data Structures For-Each Loop Iterators

  17. LEC 08: Sets, For-Each Loops, Iterators CSE 122 Winter 2025 Iterators A new object that has access to all of the elements of a given structure and can give them to you, one at a time.

  18. LEC 08: Sets, For-Each Loops, Iterators CSE 122 Winter 2025 Iterators Returned by the iterator() method Methods Description Returns true if there are more elements for the iterator to return hasNext() Returns the next element in the iteration next() Removes and returns the element that was last returned by next() remove() You must use the iterator s remove() method to remove things from what you re iterating over otherwise you will get a ConcurrentModificationException

More Related Content