sd2054 software development. the limitation of arrays once an array is created it must be sized, and...
TRANSCRIPT
![Page 1: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/1.jpg)
SD2054
Software Development
![Page 2: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/2.jpg)
![Page 3: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/3.jpg)
![Page 4: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/4.jpg)
![Page 5: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/5.jpg)
The Limitation of Arrays
Once an array is created it must be sized, and this size is fixed!
![Page 6: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/6.jpg)
The Limitation of Arrays
An array contains no useful pre-defined methods!
![Page 7: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/7.jpg)
The Limitation of Arrays
Use the classes provided in the Java Collections Framework!
![Page 8: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/8.jpg)
By the end of this lecture you should be able to:
• use the ArrayList class to store a list of objects;
• use the HashSet class to store a set of objects
• use the HashMap class to store objects in a map;
• fix the type of elements within a collection using the generics mechanism;
• use objects of your own classes in conjunction with Java's collection classes.
The Java Collections Framework
![Page 9: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/9.jpg)
The classes in the JCF are all found in the java.util package.
import java.util.*
![Page 10: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/10.jpg)
Three important interfaces from this group are:
List Set Map
![Page 11: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/11.jpg)
The List interface
The List interface specifies the methods required to process an ordered list of objects.
Such a list may contain duplicates.
![Page 12: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/12.jpg)
The List interface
Two implementations provided :
ArrayList & LinkedList
![Page 13: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/13.jpg)
Using an ArrayList to store a queue of jobs waiting for a printer
![Page 14: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/14.jpg)
The ArrayList constructor creates an empty list:
ArrayList printQ = new ArrayList();
![Page 15: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/15.jpg)
The ArrayList constructor creates an empty list:
ArrayList<String> printQ = new ArrayList<String>();
What’s that stuff in angled brackets?
![Page 16: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/16.jpg)
It’s a new Java feature called generics!
The ArrayList constructor creates an empty list:
ArrayList<String> printQ = new ArrayList<String>();
![Page 17: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/17.jpg)
Use the interface type instead of the implementation type!
![Page 18: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/18.jpg)
ArrayList<String> printQ = new ArrayList<String>();List<String> printQ = new ArrayList<String>();
A method that receives a printQ object, would now be declared as follows
public void someMethod (List<String> printQIn){
// some code here}
![Page 19: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/19.jpg)
List<String> printQ = new ArrayList<String>();
A method that receives a printQ object, would now be declared as follows
public void someMethod (List<String> printQIn){
// some code here}
![Page 20: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/20.jpg)
List<String> printQ = new HashList<String>();
A method that receives a printQ object, would now be declared as follows
public void someMethod (List<String> printQIn){
// some code here}
![Page 21: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/21.jpg)
List methods - add
printQ.add("myLetter.doc");
myLetter.doc
printQ
printQ.add("myFoto.jpg");
printQ.add("results.xls");
printQ.add("chapter.doc");
myFoto.jpg results.xls chapter.doc
0 1 2 3
![Page 22: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/22.jpg)
List methods - toString
All the Java collection types have a toString method defined
System.out.println(printQ);
Lists are displayed as follows.
[myLetter.doc, myFoto.jpg, results.xls, chapter.doc]
![Page 23: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/23.jpg)
List methods – add revisited
printQ.add(0, "importantMemo.doc");
printQ
myLetter.doc myFoto.jpg results.xls chapter.doc
importantMemo.doc
0 1 2 30 1 2 3 4
![Page 24: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/24.jpg)
List methods – set
myLetter.doc myFoto.jpg results.xls chapter.docimportantMemo.doc
printQ.set(4, "newChapter.doc");
newChapter.doc
printQ
0 1 2 3 4
![Page 25: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/25.jpg)
List methods – size
printQ.set(printQ.size()-1, "newChapter.doc");
myLetter.doc myFoto.jpg results.xls chapter.docimportantMemo.doc newChapter.doc
printQ
0 1 2 3 4
![Page 26: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/26.jpg)
List methods – indexOf
Example: finding “myFoto.jpg”
int index = printQ.indexOf("myFoto.jpg"); if (index != -1) { System.out.println("myFoto.jpg is at index position: " + index); }else { System.out.println("myFoto.jpg not in list");}
![Page 27: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/27.jpg)
List methods – remove
Example : removing "myFoto.jpg"
myFoto.jpgmyLetter.docimportantMemo.doc results.xls chapter.docnewChapter.doc
printQ
printQ.remove(2);
printQ.remove("myFoto.jpg");
0 1 2 3 42 3
![Page 28: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/28.jpg)
List methods – get
System.out.println("First job is " + printQ.get(0));
myLetter.doc myFoto.jpg results.xls chapter.docimportantMemo.doc newChapter.doc
printQ
First job is importantMemo.doc
0 1 2 3 4
![Page 29: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/29.jpg)
List methods – contains
if (printQ.contains(“poem.doc”))
{
System.out.println(“poem.doc is in the list”);
}
else
{
System.out.println(“poem.doc is not in the list”);
}
![Page 30: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/30.jpg)
Using the enhanced ‘for’ loop with collection classes
![Page 31: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/31.jpg)
Example:
Iterating through the printQ list to find and display those jobs that end with a “.doc” extension:
for (String item: printQ) { if (item.endsWith(".doc")) {
System.out.println(item); }}
![Page 32: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/32.jpg)
The Set interface
The Set interface defines the methods required to process a collection of objects in which there is no repetition, and ordering is unimportant.
![Page 33: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/33.jpg)
Which of these are sets?
a queue of people waiting to see a doctor:
![Page 34: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/34.jpg)
Which of these are sets?
a list of number one records for each of the 52 weeks of a particular year :
![Page 35: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/35.jpg)
Which of these are sets?
car registration numbers allocated parking permits.
AK346NYQC771OD
AZ885JL
![Page 36: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/36.jpg)
There are 2 implementations provided for the Set interface in the JCF.
They are HashSet and TreeSet.
![Page 37: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/37.jpg)
Using a HashSet to store a collection of vehicle registration numbers
AK346NYQC771OD
AZ885JL
![Page 38: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/38.jpg)
The constructor creates an empty set:
Set<String> regNums = new HashSet<String>();
![Page 39: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/39.jpg)
Set<String> regNums = new HashSet<String>();
Remember: use the interface type!
The constructor creates an empty set:
![Page 40: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/40.jpg)
Set<String> regNums = new HashSet<String>();
Remember: use generics!
The constructor creates an empty set:
![Page 41: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/41.jpg)
Set methods - add
The add method allows us to insert objects into the set
regNums.add(“V53PLS”);
regNums.add(“X85ADZ”);
regNums.add(“L22SBG”);
regNums.add(“W79TRV”);
![Page 42: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/42.jpg)
Set methods - toString
We can display the entire set as follows:
System.out.println(regNums);
The set is displayed in the same format as a list:
[W79TRV, X85ADZ, V53PLS, L22SBG]
![Page 43: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/43.jpg)
Set methods - size
As with a list, the size method returns the number of items in the set
System.out.println(“Number of items in set: ” + regNums.size() );
![Page 44: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/44.jpg)
Set methods - remove
The remove method deletes an item from the set if it is present.
regNums.remove(“X85ADZ”);
If we now display the set, the given registration will have been removed:
[W79TRV, V53PLS, L22SBG]
![Page 45: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/45.jpg)
Set interface also includes contains & isEmpty methods
![Page 46: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/46.jpg)
Using the enhanced ‘for’ loop to iterate through a set
![Page 47: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/47.jpg)
The following enhanced for loop will allow us to iterate through the registration numbers and display all registrations after ‘T’.
for (String item: regNums) { if (item.charAt(0)> 'T') { System.out.println(item); }}
[W79TRV, V53PLS, L22SBG]W79TRVV53PLS
![Page 48: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/48.jpg)
Iterator objects
![Page 49: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/49.jpg)
An Iterator object allows the items in a collection to be retrieved by providing three methods defined in the Iterator interface:
Methods of the Iterator interface
Method Description Inputs Outputs
hasNext Returns true if there are more elements in the collection to retrieve and false otherwise.
None An item of type boolean.
next Retrieves one element from the collection.
None An item of the given element type.
remove Removes from the collection the element that is currently retrieved
None None
![Page 50: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/50.jpg)
To obtain an Iterator object from a set, call the iterator method..
Iterator<String> elements = regNums.iterator();
![Page 51: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/51.jpg)
Using an Iterator object with a 'while' loop..
![Page 52: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/52.jpg)
The following removes all registration prior to and including 'T'
Iterator<String> elements = regNums.iterator();
while (elements.hasNext())
{
String item = elements.next();
if (item.charAt(0)<= 'T')
{
elements.remove();
}
}
![Page 53: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/53.jpg)
The Map interface
The Map interface defines the methods required to process a collection consisting of pairs of objects (keys and values).
“00165328670”
BookISBN
![Page 54: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/54.jpg)
The Map interface
The Map interface defines the methods required to process a collection consisting of pairs of objects (keys and values).
“NH03456”
PatientNHS number
![Page 55: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/55.jpg)
The Map interface
There are two implementations provided for the Map interface.
They are HashMap and TreeMap
![Page 56: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/56.jpg)
Using a HashMap to store a collection of user names and passwords
username passwordLauraHaliwell monkey
SunaGuven television
BobbyMann elephant
LucyLane monkey
BernardAnderson velvet
![Page 57: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/57.jpg)
The constructor creates the empty map:
Map<String, String> users;
users = new HashMap<String, String>( );
![Page 58: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/58.jpg)
The constructor creates the empty map:
Map<String, String> users;
users = new HashMap<String, String>( );
Remember: use the interface type!
![Page 59: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/59.jpg)
The constructor creates the empty map:
Map<String, String> users;
users = new HashMap<String, String>( );
Remember: use generics!
![Page 60: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/60.jpg)
The constructor creates the empty map:
Map<String, String> users;
users = new HashMap<String, String>( );
KEY type
![Page 61: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/61.jpg)
The constructor creates the empty map:
Map<String, String> users;
users = new HashMap<String, String>( );
VALUE type
![Page 62: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/62.jpg)
Map methods - put
users.put(“LauraHaliwell", “monkey");
users
“ LauraHaliwell " “monkey"
![Page 63: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/63.jpg)
Map methods - put
users.put(“LauraHaliwell", “popcorn");
users
“ LauraHaliwell " “monkey"“popcorn"
![Page 64: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/64.jpg)
users.put(“CharlieSheen", “crazy");
users
“ LauraHaliwell " “popcorn"
Map methods - put
“ CharlieSheen " “crazy"
![Page 65: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/65.jpg)
Map methods - containsKey
The containsKey method accepts an object and returns true if the object is a key in the map and false otherwise:
if (users.containsKey(“LauraHaliwell")) { System.out.println(“user ID already taken”);}
There is also a containsValue method to check for the presence of a value in a map.
![Page 66: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/66.jpg)
Map methods - get
String password = users.get(“LauraHaliwell);
if (password != null)
{
// valid key
}
else // no such user
{
System.out.println ("INVALID USERNAME!");
}
![Page 67: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/67.jpg)
Map methods - toString
Maps are displayed in the following output:
{LauraHaliwell=popcorn,
CharlieSheen=crazy,
BobbyMann=elephant}
![Page 68: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/68.jpg)
Map methods - remove
The remove method accepts a key value and, if the key is present in the map, both the key and value pair are removed:
users.remove(“LauraHaliwell");
Displaying the map now shows the user’s ID and password have been removed:
{CharlieSheen=crazy, BobbyMann=elephant }
![Page 69: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/69.jpg)
Iterating over the elements of a map
In order to scan the items in the map, the keySet method can be used to return the set of keys.
Set<String> theKeys = users.keySet();
The set of keys can then be processed in the ways discussed previously for sets.
![Page 70: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/70.jpg)
Using your own classes with Java’s collection classes
![Page 71: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/71.jpg)
The constructor creates an empty set:
Set<Book> books = new HashSet<Book>();
![Page 72: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/72.jpg)
When storing user-defined objects in any of the JCF collections, these objects should have three specific methods defined:
• toString• equals• hashCode
![Page 73: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/73.jpg)
Defining a toString method
Here is one possible toString method we could provide for our Book class:
public String toString() { return "(" + isbn +", "+ author + ", " + title +")\n"; }
![Page 74: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/74.jpg)
Defining an equals method
One possible interpretation of two books being equal is simply that their ISBNs are equal, so the following equals method could be added to the Book class:
public boolean equals (Object objIn) {
}
Book bookIn = objIn; Wrong type!
![Page 75: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/75.jpg)
Defining an equals method
public boolean equals (Object objIn) {
}
Book bookIn = (Book) objIn; return isbn.equals(bookIn.isbn);
One possible interpretation of two books being equal is simply that their ISBNs are equal, so the following equals method could be added to the Book class:
![Page 76: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/76.jpg)
The hashCode method
The hashCode method returns an integer value from an object.
What number can I generate from a book?
![Page 77: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/77.jpg)
The hashCode method
The hashCode method returns an integer value from an object.
How about using the number of pages!
![Page 78: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/78.jpg)
The hashCode method
The hashCode method returns an integer value from an object.
212
![Page 79: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/79.jpg)
The hashCode method
The hashCode method returns an integer value from an object.
OK, what’s the point of this number?
![Page 80: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/80.jpg)
The hashCode method
The hashCode method returns an integer value from an object.
The number makes it quicker to find items!
![Page 81: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/81.jpg)
The hashCode method
![Page 82: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/82.jpg)
The hashCode method
212
244244
![Page 83: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/83.jpg)
The hashCode method
All of Java’s predefined classes (such as String) have a meaningful hashCode method defined.
For Book equality we check the ISBN only.
This ISBN is a String, so all we need to do is to return the hashCode number of this String:
![Page 84: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/84.jpg)
The hashCode method
public int hashCode(){
return isbn.hashCode();}
![Page 85: SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!](https://reader035.vdocuments.us/reader035/viewer/2022062417/5518d294550346991f8b5d8d/html5/thumbnails/85.jpg)