-
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
Showing
5 changed files
with
166 additions
and
6 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
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,48 @@ | ||
package collections; | ||
|
||
// Entity / model / POJO | ||
public class Person implements Comparable<Person>{ | ||
String name; | ||
int age; | ||
|
||
public Person(String name, int age) { | ||
this.name = name; | ||
this.age = age; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public int getAge() { | ||
return age; | ||
} | ||
|
||
public void setAge(int age) { | ||
this.age = age; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Person{" + | ||
"name='" + name + '\'' + | ||
", age=" + age + | ||
'}'; | ||
} | ||
|
||
@Override | ||
public int compareTo(Person o) { | ||
if (this.getName().compareTo(o.getName()) == 0) { | ||
if (this.getAge() > (o.getAge())) return 1; | ||
else return -1; | ||
} else if (this.getName().compareTo(o.getName()) > 0){ | ||
return 1; | ||
} else { | ||
return -1; | ||
} | ||
} | ||
} |
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
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
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,47 @@ | ||
package collections; | ||
|
||
import java.util.HashMap; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
|
||
public class mapDemo { | ||
|
||
public static void main(String[] args) { | ||
Map<String, Integer> personAge = new HashMap<>(); | ||
personAge.put("Ramadan", 32); | ||
// personAge.put("Ramadan", 34); | ||
personAge.put("Dawan", 31); | ||
personAge.put("Sheriffo", 35); | ||
|
||
// personAge.keySet().forEach(System.out::println); | ||
// personAge.values().forEach(System.out::println); | ||
|
||
// iterative approach | ||
// for (Map.Entry<String, Integer> entry: personAge.entrySet()) { | ||
// System.out.println(entry.getKey() + " : " + entry.getValue()); | ||
// } | ||
|
||
// functional | ||
System.out.println("*** HashMap ***"); | ||
personAge.forEach((k, v) -> System.out.println(k + " : " + v)); | ||
|
||
Map<String, Integer> personAgeLinkedMap = new LinkedHashMap<>(); | ||
personAgeLinkedMap.put("Ramadan", 32); | ||
// personAge.put("Ramadan", 34); | ||
personAgeLinkedMap.put("Dawan", 31); | ||
personAgeLinkedMap.put("Sheriffo", 35); | ||
|
||
System.out.println("*** LinkedHashMap ***"); | ||
personAgeLinkedMap.forEach((k, v) -> System.out.println(k + " : " + v)); | ||
|
||
Map<String, Integer> personAgeOrderedMap = new TreeMap<>(); | ||
personAgeOrderedMap.put("Ramadan", 32); | ||
// personAge.put("Ramadan", 34); | ||
personAgeOrderedMap.put("Dawan", 31); | ||
personAgeOrderedMap.put("Sheriffo", 35); | ||
|
||
System.out.println("*** TreeMap ***"); | ||
personAgeOrderedMap.forEach((k, v) -> System.out.println(k + " : " + v)); | ||
} | ||
} |