-
Notifications
You must be signed in to change notification settings - Fork 5
/
ListtoMap.java
69 lines (54 loc) · 1.26 KB
/
ListtoMap.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Java program for list convert in map
// with the help of Object method
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
// create a list
class Student {
// id will act as Key
private Integer id;
// name will act as value
private String name;
// create curstuctor for reference
public Student(Integer id, String name)
{
// assign the value of id and name
this.id = id;
this.name = name;
}
// return private variable id
public Integer getId()
{
return id;
}
// return private variable name
public String getName()
{
return name;
}
}
// main class and method
public class ListtoMap {
// main Driver
public static void main(String[] args)
{
// create a list
List<Student>
lt = new ArrayList<Student>();
// add the member of list
lt.add(new Student(1, "Geeks"));
lt.add(new Student(2, "For"));
lt.add(new Student(3, "Geeks"));
// create map with the help of
// Object (stu) method
// create object of Map class
Map<Integer, String> map = new HashMap<>();
// put every value list to Map
for (Student stu : lt) {
map.put(stu.getId(), stu.getName());
}
// print map
System.out.println("Map : " + map);
}
}