import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.Data;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
class LinearSearchDemo {
public static void main( String ... args ) {
List list = Arrays.asList(
Person.create("Oscar", 0x20),
Person.create("Reyes", 0x30),
Person.create("Java", 0x10),
Person.create("Java", 0x20),
Person.create("Java", 0x30),
Person.create("Java", 0x40)
);
// Based on Generic
List result = searchIn(list, new Matcher() {
public boolean matches(Person p) {
return p.getName().equals("Java");
}
});
System.out.println("Using Generic : " + result);
result = searchIn(list, new Matcher() {
public boolean matches(Person p) {
return p.getAge() > 16;
}
});
System.out.println(result);
// Based on Guava Multimap
ListMultimap peopleByFirstName = ArrayListMultimap.create();
for (Person person : list) {
peopleByFirstName.put(person.getName(), person);
}
System.out.println("Usign Guava : " + peopleByFirstName.get("Java"));
// Normal Coding
Map> map = new HashMap>();
for (Person person : list) {
String key = person.getName();
if (map.get(key) == null) {
map.put(key, new ArrayList());
}
map.get(key).add(person);
}
System.out.println("Normal Coding :" + map.get("Java"));
} // end of main
public static List searchIn( List list , Matcher m ) {
List r = new ArrayList();
for( T t : list ) {
if( m.matches( t ) ) {
r.add( t );
}
}
return r;
} // end of method
} // end of class
interface Matcher {
public boolean matches( T t );
} // end of interface
@Data
class Person {
String name;
int age;
static Person create( String name, int age ) {
Person p = new Person();
p.name = name;
p.age = age;
return p;
}
} // end of class
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.Data;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
class LinearSearchDemo {
public static void main( String ... args ) {
List
Person.create("Oscar", 0x20),
Person.create("Reyes", 0x30),
Person.create("Java", 0x10),
Person.create("Java", 0x20),
Person.create("Java", 0x30),
Person.create("Java", 0x40)
);
public boolean matches(Person p) {
return p.getName().equals("Java");
}
});
System.out.println("Using Generic : " + result);
result = searchIn(list, new Matcher
public boolean matches(Person p) {
return p.getAge() > 16;
}
});
System.out.println(result);
// Based on Guava Multimap
ListMultimap
for (Person person : list) {
peopleByFirstName.put(person.getName(), person);
}
System.out.println("Usign Guava : " + peopleByFirstName.get("Java"));
// Normal Coding
Map
for (Person person : list) {
String key = person.getName();
if (map.get(key) == null) {
map.put(key, new ArrayList
}
map.get(key).add(person);
}
System.out.println("Normal Coding :" + map.get("Java"));
} // end of main
public static
List
for( T t : list ) {
if( m.matches( t ) ) {
r.add( t );
}
}
return r;
} // end of method
} // end of class
interface Matcher
public boolean matches( T t );
} // end of interface
@Data
class Person {
String name;
int age;
static Person create( String name, int age ) {
Person p = new Person();
p.name = name;
p.age = age;
return p;
}
} // end of class
No comments:
Post a Comment