Lists
The usage with List Collections is quite straightforward as most of the java developers are familier with its API & its methods. Example of a code involving List interface is as follows:
List
MyObject a=new MyObject();
myList.add(a);
...
...
//Use this code to iterate
Iterator
while(iterator.hasNext()){
MyObject temp=iterator.next();
System.out.print(""+temp.toString());
}
//Or use the foll code
for(MyObject ss:myList){
MyObject temp=iterator.next();
System.out.print(""+temp.toString());
}
..
...
Sets
Set
System.out.println("Added an element"+s.add(new MyObject()));
s.add(new MyObject());
....
for(MyObject st:s)
System.out.println(st.toString());
Using TreeSets
Set
Now, class Power must be comparable, i.e. You can compare its elements .Eg. You can compare 1 & 2, "A" & "B", but how will you compare boy("Sumit") & boy("Rahul") ?
In order to do this, use java.util.Comparator interface.
Class Power implements Comparable
public int FactorValue;
public int OtherValue;
.... other code including constructors
.
..
public int compareTo(Power t){ //Overriding the method through programmer implementation
if(this.FactorValue==t.FactorValue)
return 0;
if(this.FactorValue
return -1;
if(this.FactorValue>t.FactorValue)
return 1;
else
return 0;//Maybe throw an Assertion Error or an exception
}
//OR, You Could Also Use The Following Override
public int compareTo(Power t){ //Overriding through delegation
return(this.FactorValue.compareTo(t.FactorValue()));
/*
Works because Comparison is delegated to the int variable FactorValue in our class;
*/
}
}
If you wish to sort your object in a different manner, then use a Comparator. You can use it to sort even those class that you cannot modify. In such a case, create a (3rd Party) class like:
class OtherSort implements Comparator
public int compare(MyObjectme,MyObject you){
return(me.OtherValue.compareTo(you.OtherValue));
}
}
Maps
Any class you use in a Map must implement the hashCode() and equals() methods. If you don't do so, then no compile/run time errors will occur, the code will just would'nt function the way it was supposed to.
Usage:
Map
Type1=Type Of KEY
Type2=Type Of VALUE
eg. Map
class Human{
String name;
Human(String s){
name=s;
}
public boolean equals(Object o){
if((o instanceof Human)&& ((Human)o).name==name)
return true;
else
return false;
}
public int hashCode(){ //More unique the hashCode returns a value,
return 5; //More efficient your class will be in retrieveing tho object from Map
}
public static void main(String[] args){
Map
map.put("we",new Human("Sumit"));
...
...
Human us;
us=map.get("we");
System.out.print(""+us.name);
}
}
Also, this is a valid code segment
Map
PriorityQueue
Queue
int[] values={1,6,3,8,34,55};
for(int i:values)
queue.offer(i); //Populate
System.out.println(""+queue.peek());//Print highest priority int
System.out.println(""+queue.size());
for(int i:values)
System.out.print(""+queue.poll(i));//Print and remove
A case where Mixing of Generic & non Generic collection creates Havoc
class Old{ //Old, Pre-Generic code
void InsertInt(List lst){
lst.add(new Integer(42));
}
}
class New{ //Newer class, under study
void go(){
List
Old oldobject=new Old();
oldobject.InsertInt(l); //Adds Integer in an
}
void nogo(){
List
Old oldobject=new Old();
oldobject.InsertInt(l); //Adds Integer in an
}
}
This is primary reason why the compiler 'unhappily' chunks out an -Xlint:unchecked warning on compilation.
Polymorphism in Generics- Not Allowed
Hence you can't use
List
No comments:
Post a Comment