Difference between revisions of "Collections"
From Suhrid.net Wiki
Jump to navigationJump to search (Created page with "'''Collections''' <u>equals() and hashcode() </u> * Mainly used for storing and retrieving objects from hashed collections. * First object's hashcode is used to figure out whic...") |
|||
Line 11: | Line 11: | ||
* unequal objects can have the same (or different) hashcode (e.g. same bucket but objects are different) | * unequal objects can have the same (or different) hashcode (e.g. same bucket but objects are different) | ||
* objects having different hashcodes MUST be unequal | * objects having different hashcodes MUST be unequal | ||
+ | |||
+ | <u> Collections </u> | ||
+ | |||
+ | There are four flavours of Collections: | ||
+ | |||
+ | # Sorted | ||
+ | # Unsorted | ||
+ | # Ordered -> Means that we can iterate through the collection in the same specific order every time. For e.g. all lists are ordered by index position. See example below: | ||
+ | |||
+ | <syntaxhighlight lang="java"> | ||
+ | package net.suhrid; | ||
+ | |||
+ | import java.util.*; | ||
+ | |||
+ | class Foo { | ||
+ | private String str; | ||
+ | |||
+ | public Foo(String str) { | ||
+ | this.str = str; | ||
+ | } | ||
+ | |||
+ | public String toString() { | ||
+ | return "Foo:" + str; | ||
+ | } | ||
+ | |||
+ | public int compareTo(Foo f) { | ||
+ | return str.compareTo(f.str); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | public class HelloWorld { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | List<String> list = new ArrayList<String>(); | ||
+ | list.add("D"); | ||
+ | list.add("A"); | ||
+ | list.add("C"); | ||
+ | list.add("B"); | ||
+ | |||
+ | for(Iterator<String> iter = list.iterator(); iter.hasNext();) { | ||
+ | System.out.println(iter.next()); | ||
+ | } | ||
+ | //Will print D,A,C,B each time | ||
+ | |||
+ | List<Foo> fooList = new ArrayList<Foo>(); | ||
+ | fooList.add(new Foo("D")); | ||
+ | fooList.add(new Foo("A")); | ||
+ | fooList.add(new Foo("C")); | ||
+ | fooList.add(new Foo("B")); | ||
+ | |||
+ | for(Iterator<Foo> iter = fooList.iterator(); iter.hasNext();) { | ||
+ | System.out.println(iter.next()); | ||
+ | } | ||
+ | //Will print Foo:D,Foo:A,Foo:C,Foo:B each time | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | # Unordered -> | ||
[[Category:OCPJP]] | [[Category:OCPJP]] |
Revision as of 00:22, 24 May 2011
Collections
equals() and hashcode()
- Mainly used for storing and retrieving objects from hashed collections.
- First object's hashcode is used to figure out which hash bucket the object is in
- Then the equals() method is used to compare objects in the same hash bucket.
This means that:
- equal objects MUST have the same hashcode
- unequal objects can have the same (or different) hashcode (e.g. same bucket but objects are different)
- objects having different hashcodes MUST be unequal
Collections
There are four flavours of Collections:
- Sorted
- Unsorted
- Ordered -> Means that we can iterate through the collection in the same specific order every time. For e.g. all lists are ordered by index position. See example below:
package net.suhrid;
import java.util.*;
class Foo {
private String str;
public Foo(String str) {
this.str = str;
}
public String toString() {
return "Foo:" + str;
}
public int compareTo(Foo f) {
return str.compareTo(f.str);
}
}
public class HelloWorld {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("D");
list.add("A");
list.add("C");
list.add("B");
for(Iterator<String> iter = list.iterator(); iter.hasNext();) {
System.out.println(iter.next());
}
//Will print D,A,C,B each time
List<Foo> fooList = new ArrayList<Foo>();
fooList.add(new Foo("D"));
fooList.add(new Foo("A"));
fooList.add(new Foo("C"));
fooList.add(new Foo("B"));
for(Iterator<Foo> iter = fooList.iterator(); iter.hasNext();) {
System.out.println(iter.next());
}
//Will print Foo:D,Foo:A,Foo:C,Foo:B each time
}
}
- Unordered ->