-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit acacc9b
Showing
19 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>SortedValueMap</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
eclipse.preferences.version=1 | ||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled | ||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 | ||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve | ||
org.eclipse.jdt.core.compiler.compliance=1.6 | ||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate | ||
org.eclipse.jdt.core.compiler.debug.localVariable=generate | ||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate | ||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error | ||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error | ||
org.eclipse.jdt.core.compiler.source=1.6 |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
public interface IPair<K, V> extends Comparable<IPair<K, V>> { | ||
public K getKey(); | ||
public V getValue(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
|
||
public abstract class Pair<K,V> implements IPair<K,V>{ | ||
private K key; | ||
private V value; | ||
|
||
public Pair(K key, V value) { | ||
super(); | ||
this.key = key; | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public K getKey() { | ||
return key; | ||
} | ||
|
||
@Override | ||
public V getValue() { | ||
return value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
public interface PairFactory<K, V, R extends IPair<K, V>>{ | ||
public R create(K key, V value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
|
||
public class Person { | ||
private Long id; | ||
private String email; | ||
private int age; | ||
|
||
public Person(){} | ||
|
||
public Person(Long id, String email, int age) { | ||
super(); | ||
this.id = id; | ||
this.email = email; | ||
this.age = age; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getEmail() { | ||
return email; | ||
} | ||
public void setEmail(String email) { | ||
this.email = email; | ||
} | ||
public int getAge() { | ||
return age; | ||
} | ||
public void setAge(int age) { | ||
this.age = age; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
public class PersonPairByEmail extends Pair<Long, Person>{ | ||
|
||
public PersonPairByEmail(Long key, Person value) { | ||
super(key, value); | ||
} | ||
|
||
@Override | ||
public int compareTo(IPair<Long, Person> o) { | ||
return this.getValue().getEmail().compareTo(o.getValue().getEmail()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
public class PersonPairFactory implements PairFactory<Long, Person, PersonPairByEmail>{ | ||
|
||
@Override | ||
public PersonPairByEmail create(Long key, Person value) { | ||
return new PersonPairByEmail(key, value); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
|
||
public class SortedMap <K, V>{ | ||
private Map<K, IPair<K, V>> map; | ||
private PairFactory<K, V, ? extends IPair<K, V>> factory; | ||
|
||
public SortedMap(PairFactory<K, V, ? extends IPair<K, V>> factory){ | ||
this.factory = factory; | ||
map = new HashMap<K, IPair<K, V>>(); | ||
} | ||
|
||
public IPair<K, V> put(K key, V value) { | ||
return map.put(key, factory.create(key, value)); | ||
//return map.put(key, new Pair<K, V>(key, value)); | ||
//http://stackoverflow.com/questions/75175/create-instance-of-generic-type-in-java | ||
} | ||
|
||
public List<IPair<K, V>> sortByValue(){ | ||
List<IPair<K, V>> l = new ArrayList<IPair<K, V>>(map.values()); | ||
Collections.sort(l); | ||
return l; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import java.util.List; | ||
|
||
|
||
public class Test { | ||
|
||
public static void main(String[] args) { | ||
Person p1 = new Person (1l, "doe@gmail.com", 47); | ||
Person p2 = new Person (2L, "andre@gmail.com", 21); | ||
Person p3 = new Person (3L, "zanetti@gmail.com", 34); | ||
|
||
SortedMap<Long, Person> s = new SortedMap<Long, Person>(new PersonPairFactory()); | ||
|
||
s.put(p1.getId(), p1); | ||
s.put(p2.getId(), p2); | ||
s.put(p3.getId(), p3); | ||
|
||
List<IPair<Long, Person>> sortByValue = s.sortByValue(); | ||
|
||
for (IPair<Long, Person> i : sortByValue){ | ||
System.out.println(i.getValue().getEmail()); | ||
} | ||
} | ||
|
||
} |