Содержание
- 2. AGENDA Arrays in Java Collections in Java List Set Map
- 3. Arrays
- 4. Arrays in Java class Car{ }; // minimal dummy class Car[ ] cars1; // null reference
- 5. Arrays in Java Most efficient way to hold references to objects. Advantages An array know the
- 6. Collections
- 7. Java Collection Framework The Collection in Java is a framework that provides an architecture to store
- 8. Collections in Java Java Collection framework provides many interfaces (Set, List, Queue, Deque) and classes (ArrayList,
- 9. Benefits of collections reduces programming effort increases program speed and quality allows interoperability among unrelated APIs
- 10. Interfaces There are data types that represent collections. Classes that implement interfaces List and Set ,
- 11. Collections in Java List – a list of objects. Objects can be added to the list
- 12. Collections in Java The interface Collection defined methods: boolean add(E obj) – adds obj to the
- 13. Collections in Java boolean isEmpty() – returns true, if the collection is empty; Iterator iterator() –
- 14. Interfaces
- 15. Implementations There are the implementations of the collection interfaces. In essence, they are reusable data structures
- 16. Interface List A Lists are Ordered Collections (sometimes called a sequence). Lists can contain duplicate elements.
- 17. Class ArrayList Array Lists use a dynamic array to store the duplicate element of different data
- 18. Class LinkedList Linked Lists use a references to maintain an ordered lists of “nodes”. The “head”
- 19. List Since List is an interface you need to instantiate a concrete implementation of the interface
- 20. List Adding elements List list = new ArrayList (); list.add("First element"); list.add("Second element"); list.add(0, "One more
- 21. List Removing Elements remove(Object element) or remove(int index) Cleaning a list list.clear(); List size int size
- 22. List LinkedList has the same functionality as the ArrayList. Different way of implementation and efficiency of
- 23. Interface Set A Sets are Unordered Collection that cannot contain duplicate elements. This interface models the
- 24. Interface Set Element that are put in a set must override equals() method to establish uniqueness
- 25. Interface Set The HashSet class Uses Hash Tables to speed up finding, adding, and removing elements.
- 26. Class HashSet HashSet is unsorted and unordered set uses hashcode of the object being inserted; the
- 27. Class LinkedHashSet LinkedHashSet is an ordered version of HashSet maintains doubly-linked list across all elements if
- 28. Class TreeSet TreeSet is one of the two sorted collections (the other being TreeMap) uses Red-Black
- 29. Example import java.util.*; public class FindDups { public static void main(String args[ ]){ Set s =
- 30. equals() and hashCode() Contract The contract between equals() and hashCode() methods is: If two objects are
- 31. Iterator Iterator – a helper object. Used to iterate over collection of objects. Iterators are based
- 32. List Iterator When traversing a LinkedList collection, use a ListIterator class. List names = new ArrayList
- 33. Set Iterator Iterators are also used when processing sets, but sets don’t support ListIterator class. Instead,
- 34. Iterator and Loop Iterators are often used in while and “for-each” loops. hasNext() method returns true
- 35. Interface Map Map The Map is an object that maps keys to values. Map cannot contain
- 36. Interface Map A Map stores keys, values, and the associations between them. Keys - provides an
- 37. Map The most commonly used Map implementations are HashMap and TreeMap. Map mapA = new HashMap();
- 38. Classes HashMap and TreeMap A Map allows you to associate elements from a key set with
- 39. Map’s Iterator To iterate through the map, use a entrySet() method and object of Iterator class:
- 40. Map for (Iterator i = map.entrySet().iterator(); i.hasNext();){ Map.Entry entry = (Map.Entry) i.next(); System.out.println(entry.getKey() + " "
- 41. Big-O Notation In computer science, big O notation is used to classify algorithms according to how
- 42. Big-O Notation
- 43. Practical task 1 Declare collection myCollection of 10 integers and fill it (from the console or
- 44. Practical task 2 In the main() method declare map employeeMap of pairs . Add to employeeMap
- 45. Homework Write parameterized methods union(Set set1, Set set2) and intersect(Set set1, Set set2), realizing the operations
- 46. Homework Write class Student that provides information about the name of the student and his course.
- 48. Скачать презентацию