Skip to content

Commit 3fd04dc

Browse files
committed
java map and list
java map and list
1 parent 38b5564 commit 3fd04dc

File tree

5 files changed

+233
-0
lines changed

5 files changed

+233
-0
lines changed

java-list-map/.classpath

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src/test/java" output="target/test-classes" including="**/*.java"/>
4+
<classpathentry kind="src" path="src/main/java" including="**/*.java"/>
5+
<classpathentry kind="output" path="target/classes"/>
6+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
7+
<classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar"/>
8+
</classpath>

java-list-map/pom.xml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.hmkcode</groupId>
6+
<artifactId>java-list-map</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>java-list-map</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>junit</groupId>
20+
<artifactId>junit</artifactId>
21+
<version>3.8.1</version>
22+
<scope>test</scope>
23+
</dependency>
24+
</dependencies>
25+
</project>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.hmkcode;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.LinkedList;
6+
import java.util.List;
7+
8+
import com.hmkcode.vo.Person;
9+
10+
public class ListApp
11+
{
12+
13+
public static void main( String[] args )
14+
{
15+
16+
// ( 1 ) List
17+
18+
// A. Initiate
19+
List<Person> arrayList = new ArrayList<Person>();
20+
List<Person> linkedList = new LinkedList<Person>();
21+
22+
// B. Populate
23+
for(Person person:getPersons()){
24+
arrayList.add(person);
25+
linkedList.add(person);
26+
}
27+
28+
// --> print
29+
System.out.println("--------- Print All -------------");
30+
31+
System.out.println("ArrayList: "+arrayList);
32+
System.out.println("LinkedList: "+linkedList);
33+
34+
// C. Iterate
35+
System.out.println("--------- Print Iterate by index -------------");
36+
37+
for(int i = 0; i < linkedList.size(); i++){
38+
System.out.println("LinkedList[ "+i+" ]"+linkedList.get(i));
39+
}
40+
41+
System.out.println("--------- Print Iterate by for each -------------");
42+
43+
//this is an efficient way to iterate
44+
for(Person person:linkedList){
45+
System.out.println("LinkedList: "+person);
46+
}
47+
48+
// D. Sort
49+
Collections.sort(linkedList);
50+
51+
// --> print
52+
System.out.println("--------- Print Sorted List -------------");
53+
54+
System.out.println("Sorted LinkedList: "+linkedList);
55+
}
56+
57+
private static Person[] getPersons(){
58+
Person[] persons = new Person[5];
59+
60+
persons[0] = new Person("Brit", 29);
61+
persons[1] = new Person("John", 32);
62+
persons[2] = new Person("Jack", 27);
63+
persons[3] = new Person("Jenifer", 24);
64+
persons[4] = new Person("Brit", 37);
65+
66+
return persons;
67+
}
68+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.hmkcode;
2+
3+
import java.util.ArrayList;
4+
import java.util.Comparator;
5+
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
import java.util.Map.Entry;
9+
import java.util.TreeMap;
10+
11+
12+
import com.hmkcode.vo.Person;
13+
14+
15+
public class MapApp
16+
{
17+
18+
public static void main( String[] args )
19+
{
20+
21+
22+
// ( 1 ) Map
23+
24+
// A. Initiate
25+
Map<String,Person> hashMap = new HashMap<String,Person>();
26+
Map<String,Person> treeMap = new TreeMap<String,Person>();
27+
28+
// B. Populate
29+
int k = 0 ;
30+
for(Person person:getPersons()){
31+
hashMap.put(""+k,person);
32+
treeMap.put(""+k++,person);
33+
}
34+
35+
// --> print
36+
System.out.println("--------- Print All -------------");
37+
System.out.println("HashMap: "+hashMap);
38+
System.out.println("TreeMap: "+treeMap);
39+
40+
41+
// C. Iterate
42+
// --> print
43+
System.out.println("--------- Print Iterate by get(key) -------------");
44+
for(String key:treeMap.keySet()){
45+
46+
System.out.println("treeMap: [key: "+key+" , value: "+treeMap.get(key));
47+
}
48+
// --> print
49+
System.out.println("--------- Print Iterate by Entry -------------");
50+
for(Entry<String, Person> entry:treeMap.entrySet()){
51+
52+
System.out.println("treeMap: [key: "+entry.getKey()+" , value: "+entry.getValue());
53+
}
54+
55+
// D. Sort by value
56+
TreeMap<String,Person> sorted_map = new TreeMap<String,Person>(new ValueComparator());
57+
sorted_map.putAll(hashMap);
58+
59+
60+
// --> print
61+
System.out.println("--------- Print Sorted Map by Value -------------");
62+
System.out.println("Sorted HashMap: "+sorted_map);
63+
64+
65+
// E. Convert Map to List
66+
67+
List<Person> persons = new ArrayList<Person>(sorted_map.values());
68+
69+
// --> print
70+
System.out.println("--------- Print List<Person> -------------");
71+
System.out.println("List<Person>: "+persons);
72+
}
73+
74+
private static Person[] getPersons(){
75+
Person[] persons = new Person[5];
76+
77+
persons[0] = new Person("Brit", 29);
78+
persons[1] = new Person("John", 32);
79+
persons[2] = new Person("Jack", 27);
80+
persons[3] = new Person("Jenifer", 24);
81+
persons[4] = new Person("Brit", 37);
82+
83+
return persons;
84+
}
85+
}
86+
class ValueComparator implements Comparator {
87+
88+
Map map;
89+
90+
public ValueComparator(){
91+
92+
}
93+
public int compare(Object keyA, Object keyB){
94+
95+
return ((String) keyA).compareTo((String) keyB);
96+
97+
}
98+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.hmkcode.vo;
2+
3+
public class Person implements Comparable<Person> {
4+
5+
private int age;
6+
private String name;
7+
8+
public Person(String name, int age){
9+
this.name = name;
10+
this.age = age;
11+
}
12+
public int getAge() {
13+
return age;
14+
}
15+
public void setAge(int age) {
16+
this.age = age;
17+
}
18+
public String getName() {
19+
return name;
20+
}
21+
public void setName(String name) {
22+
this.name = name;
23+
}
24+
@Override
25+
public String toString() {
26+
return "Person [age=" + age + ", name=" + name + "]";
27+
}
28+
@Override
29+
public int compareTo(Person person) {
30+
return this.age-person.getAge();
31+
}
32+
33+
34+
}

0 commit comments

Comments
 (0)