Difference between revisions of "Collections"

From Suhrid.net Wiki
Jump to navigationJump to search
Line 16: Line 16:
 
There are four flavours of Collections:
 
There are four flavours of Collections:
  
# Sorted
+
* Sorted -> Means that the order in the collection is determined according to the sort order i.e. properties of the object. Most common sort oder is the natural sort order. e.g. ascending for Integer objects, alphabetical for String objects.
# Unsorted
+
* 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:
+
* 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">
 
<syntaxhighlight lang="java">
Line 25: Line 25:
 
import java.util.*;
 
import java.util.*;
  
class Foo {
+
class Foo implements Comparable<Foo> {
 
private String str;
 
private String str;
 
 
Line 69: Line 69:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
# Unordered ->
+
So despite Foo having defined a comparator, ordering is performed according to the element's index position.
 +
 
 +
LinkedHashSet is ordered by insertion, so the last element inserted is the last element in the LinkedHashSet.
 +
 
 +
* Unordered ->
  
 
[[Category:OCPJP]]
 
[[Category:OCPJP]]

Revision as of 00:47, 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 -> Means that the order in the collection is determined according to the sort order i.e. properties of the object. Most common sort oder is the natural sort order. e.g. ascending for Integer objects, alphabetical for String objects.
  • 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 implements Comparable<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
	}
}

So despite Foo having defined a comparator, ordering is performed according to the element's index position.

LinkedHashSet is ordered by insertion, so the last element inserted is the last element in the LinkedHashSet.

  • Unordered ->