collections
play

Collections Let's Take a Quick Break Organizing Data We have many - PowerPoint PPT Presentation

Collections Let's Take a Quick Break Organizing Data We have many ways of storing and organizing data in our programs: String s for holding sequences of characters. ArrayList s for holding sequences of general objects. Arrays for


  1. Collections

  2. Let's Take a Quick Break

  3. Organizing Data ● We have many ways of storing and organizing data in our programs: ● String s for holding sequences of characters. ● ArrayList s for holding sequences of general objects. ● Arrays for holding fixed-sized sequences. ● HashMap s for associating data with one another. ● Are there other ways of organizing data? ● What do they look like?

  4. The Collections Framework ● Java has a variety of collections classes for holding groups of data.

  5. The Collections Framework ● Java has a variety of collections classes for holding groups of data. ● The three major ways of organizing data are ● Set s, which store unordered data

  6. The Collections Framework ● Java has a variety of collections classes for holding groups of data. ● The three major ways of organizing data are ● Set s, which store unordered data, ● List s, which store sequences

  7. The Collections Framework ● Java has a variety of collections classes for holding groups of data. ● The three major ways of organizing data are ● Set s, which store unordered data, ● List s, which store sequences, and ● Map s, which store key/value pairs.

  8. The Collections Framework Java has a variety of collections classes for holding groups of data. The three major ways of organizing data are ● Set s, which store unordered data, List s, which store sequences, and Map s, which store key/value pairs.

  9. What is a Set? ● A set is a collection of distinct elements. ● Similar to an ArrayList , but elements are not stored in a sequence. ● Major operations are: ● Adding an element. ● Removing an element. ● Checking whether an element exists. ● Useful for answering questions of the form “have I seen this before?”

  10. HashSet<String> mySet = new HashSet<String>();

  11. CS106A CS106A HashSet<String> mySet = new HashSet<String>(); mySet.add("CS106A"); To add a value to a HashSet , use the syntax set .add( value )

  12. CS106A CS106A CS106A Ibex HashSet<String> mySet = new HashSet<String>(); mySet.add("CS106A"); mySet.add("Ibex");

  13. CS106A CS106A CS106A Ibex CS106A 137 HashSet<String> mySet = new HashSet<String>(); mySet.add("CS106A"); mySet.add("Ibex"); mySet.add("137");

  14. CS106A CS106A CS106A Ibex CS106A 137 HashSet<String> mySet = new HashSet<String>(); mySet.add("CS106A"); mySet.add("Ibex"); If you add a value mySet.add("137"); mySet.add("CS106A"); pair where the value exists, nothing happens.

  15. CS106A CS106A CS106A Ibex CS106A 137 HashSet<String> mySet = new HashSet<String>(); mySet.add("CS106A"); mySet.add("Ibex"); mySet.add("137"); mySet.add("CS106A"); mySet.contains("Ibex");

  16. CS106A CS106A CS106A Ibex CS106A 137 HashSet<String> mySet = new HashSet<String>(); mySet.add("CS106A"); To see if a value mySet.add("Ibex"); mySet.add("137"); exists: mySet.add("CS106A"); mySet.contains("Ibex"); set .contains( value )

  17. CS106A CS106A CS106A Ibex CS106A 137 HashSet<String> mySet = new HashSet<String>(); mySet.add("CS106A"); mySet.add("Ibex"); mySet.add("137"); mySet.add("CS106A"); mySet.contains("Ibex"); mySet.contains("CS106A");

  18. CS106A CS106A CS106A Ibex CS106A 137 HashSet<String> mySet = new HashSet<String>(); mySet.add("CS106A"); mySet.add("Ibex"); mySet.add("137"); mySet.add("CS106A") mySet.contains("Ibex"); mySet.contains("CS106A");

  19. CS106A CS106A CS106A Ibex CS106A 137 HashSet<String> mySet = new HashSet<String>(); mySet.add("CS106A"); mySet.add("Ibex"); mySet.add("137"); mySet.add("CS106A") mySet.contains("Ibex"); mySet.contains("CS106A"); mySet.contains("<(^_^)>");

  20. CS106A CS106A CS106A Ibex CS106A 137 HashSet<String> mySet = new HashSet<String>(); mySet.add("CS106A"); mySet.add("Ibex"); mySet.add("137"); mySet.add("CS106A") mySet.contains("Ibex"); mySet.contains("CS106A"); mySet.contains("<(^_^)>");

  21. Basic Set Operations ● To insert an element: set .add( value ) ● To check whether a value exists: set .contains( value ) ● To remove an element: set .remove( value )

  22. Word Walks CO DE DE SI RE RE WRI TE TE MPERA TE TE ATI ME ME MEN TO TO RRENT

  23. Word Skips CARRO T T OMAT O O KR A A SPARAGU S S QUAS H H ORSERADISH

  24. Word Skips ● Begin with any word you'd like. ● Choose a word whose first letter is the same as the last letter of your current word. ● Repeat until you get bored.

  25. Iterators ● To visit every element of a collection, you can use the “for each” loop: for ( ElemType elem : collection ) { … } ● Alternatively, you can use an iterator , an object whose job is to walk over the elements of a collection. ● The iterator has two commands: ● hasNext() , which returns whether there are any more elements to visit, and ● next() , which returns the next element and moves the iterator to the next position.

  26. Java Iterators ArrayList<Integer> myList = /* … */ Iterator<Integer> iter = myList.iterator(); while (iter.hasNext()) { Integer curr = iter.next(); /* … use curr … */ }

  27. Java Iterators ArrayList<Integer> myList = /* … */ Iterator<Integer> iter = myList.iterator(); while (iter.hasNext()) { Integer curr = iter.next(); /* … use curr … */ }

  28. Java Iterators 137 42 2718 ArrayList<Integer> myList = /* … */ Iterator<Integer> iter = myList.iterator(); while (iter.hasNext()) { Integer curr = iter.next(); /* … use curr … */ }

  29. Java Iterators 137 42 2718 ArrayList<Integer> myList = /* … */ Iterator<Integer> iter = myList.iterator(); while (iter.hasNext()) { Integer curr = iter.next(); /* … use curr … */ }

  30. Java Iterators iter 137 42 2718 ArrayList<Integer> myList = /* … */ Iterator<Integer> iter = myList.iterator(); while (iter.hasNext()) { Integer curr = iter.next(); /* … use curr … */ }

  31. Java Iterators iter 137 42 2718 ArrayList<Integer> myList = /* … */ Iterator<Integer> iter = myList.iterator(); while (iter.hasNext()) { Integer curr = iter.next(); /* … use curr … */ }

  32. Java Iterators hasNext()? iter 137 42 2718 ArrayList<Integer> myList = /* … */ Iterator<Integer> iter = myList.iterator(); while (iter.hasNext()) { Integer curr = iter.next(); /* … use curr … */ }

  33. Java Iterators iter 137 42 2718 ArrayList<Integer> myList = /* … */ Iterator<Integer> iter = myList.iterator(); while (iter.hasNext()) { Integer curr = iter.next(); /* … use curr … */ }

  34. Java Iterators next()! iter 137 42 2718 ArrayList<Integer> myList = /* … */ Iterator<Integer> iter = myList.iterator(); while (iter.hasNext()) { Integer curr = iter.next(); /* … use curr … */ }

  35. Java Iterators next()! iter 137 42 2718 ArrayList<Integer> myList = /* … */ Iterator<Integer> iter = myList.iterator(); while (iter.hasNext()) { Integer curr = iter.next(); /* … use curr … */ }

  36. Java Iterators iter 137 42 2718 ArrayList<Integer> myList = /* … */ Iterator<Integer> iter = myList.iterator(); while (iter.hasNext()) { Integer curr = iter.next(); /* … use curr … */ }

  37. Java Iterators iter 137 42 2718 ArrayList<Integer> myList = /* … */ Iterator<Integer> iter = myList.iterator(); while (iter.hasNext()) { Integer curr = iter.next(); /* … use curr … */ }

  38. Java Iterators hasNext()? iter 137 42 2718 ArrayList<Integer> myList = /* … */ Iterator<Integer> iter = myList.iterator(); while (iter.hasNext()) { Integer curr = iter.next(); /* … use curr … */ }

  39. Java Iterators iter 137 42 2718 ArrayList<Integer> myList = /* … */ Iterator<Integer> iter = myList.iterator(); while (iter.hasNext()) { Integer curr = iter.next(); /* … use curr … */ }

  40. Java Iterators next()! iter 137 42 2718 ArrayList<Integer> myList = /* … */ Iterator<Integer> iter = myList.iterator(); while (iter.hasNext()) { Integer curr = iter.next(); /* … use curr … */ }

  41. Java Iterators next()! iter 137 42 2718 ArrayList<Integer> myList = /* … */ Iterator<Integer> iter = myList.iterator(); while (iter.hasNext()) { Integer curr = iter.next(); /* … use curr … */ }

  42. Java Iterators iter 137 42 2718 ArrayList<Integer> myList = /* … */ Iterator<Integer> iter = myList.iterator(); while (iter.hasNext()) { Integer curr = iter.next(); /* … use curr … */ }

  43. Java Iterators iter 137 42 2718 ArrayList<Integer> myList = /* … */ Iterator<Integer> iter = myList.iterator(); while (iter.hasNext()) { Integer curr = iter.next(); /* … use curr … */ }

Recommend


More recommend