- GNU provides a collection API for Java for high performance.
- We can implement some collection interface to customize our own requirement.
- We can create procedure and function on each object of the collection.
- Download Link of Trove Library:-
http://sourceforge.net/projects/trove4j/files/
Project Structure:-
Trove HashMap (Procedure & Function):-
Create a Object Procedure:-
- To create a Procedure we need to override TObjectProcedure interface.
MyObjectProcedure.java
package com.sandeep.trove.collection;
import gnu.trove.procedure.TObjectProcedure;
public class MyObjectProcedure implements TObjectProcedure{
/*
*This method returns boolean
* false :- procedure is called only once
* true :- procedure is called for each value
*/
@Override
public boolean execute(String objvalue) {
System.out.println("Length of "+objvalue+" is : "+objvalue.length());
return true;
}
}
Create a Object Function:-
MyObjectFunction.java
package com.sandeep.trove.collection;
import gnu.trove.function.TObjectFunction;
public class MyObjectFunction implements TObjectFunction{
@Override
public String execute(String object) {
return "My Name : " + object;
}
}
Using Procedure & Function :-
package com.sandeep.trove.collection;
import gnu.trove.map.TMap;
import gnu.trove.map.hash.THashMap;
public class GnuTroveDemo {
public static void main(String[] args) {
TMaptrovemap = new THashMap ();
trovemap.put("Key1", "Sangeeta");
trovemap.put("Key2", "Surabhi");
System.out.println("Original Value of Trove Map");
for(String aValue: trovemap.values()){
System.out.print(aValue+"n");
}
/*Trove Procedure is called on each object*/
System.out.println("Procedure Called :- ");
trovemap.forEachValue(new MyObjectProcedure());
/*Trove Function is called on each value to manipulate it*/
System.out.println("Function Called :- ");
trovemap.transformValues(new MyObjectFunction());
for(String aValue: trovemap.values()){
System.out.println(aValue);
}
}
}
Output:-
Original Value of Trove Map
Surabhi
Sangeeta
Procedure is Called :-
Length of Surabhi is : 7
Length of Sangeeta is : 8
Function Called :-
My Name : Surabhi
My Name : Sangeeta