Split Generic Array Lists in Sub-lists on basis of Object properties
/*
*@ CopyRight : 2016
*@ Author : RAJEEVKUMAR
*@ Date : Jan 17, 2017
*@ File Name : UniqueListInMap.java
*@ Package : codding.time.array
*
*/
package codding.time.array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
public class UniqueListInMap {
public static void main(String args[]) {
Map<Integer, ArrayList<Student>> parentMap = new HashMap<Integer, ArrayList<Student>>();
Map<Integer, ArrayList<ArrayList<Student>>> majorMap = new HashMap<Integer, ArrayList<ArrayList<Student>>>();
UniqueListInMap obj = new UniqueListInMap();
ArrayList<Student> listOfStudent = obj.addStudentToList();
parentMap.put(1, listOfStudent);
for (Map.Entry<Integer, ArrayList<Student>> entry : parentMap.entrySet()) {
System.out.println(entry.getKey() + "/" + entry.getValue());
int studentAge = entry.getKey();
ArrayList<Student> studentList = entry.getValue();
HashSet<String> set = new HashSet<String>();
ArrayList<ArrayList<Student>> majorStudentList = new ArrayList<ArrayList<Student>>();
for (Student student : studentList) {
ArrayList<Student> newStudentList = new ArrayList<Student>();
int age = student.getAge();
String city = "";
for (int i = 0; i < studentList.size(); i++) {
if (student.getCity().equalsIgnoreCase(studentList.get(i).getCity())) {
city = city + "," + student.getCity();
newStudentList.add(studentList.get(i));
}
}
if (set.add(city)) {
majorStudentList.add(newStudentList);
System.out.println("String : " + city);
}
}
majorMap.put(1, majorStudentList);
}
System.out.println("majorMap--------------" + majorMap);
for (int k : majorMap.keySet()) {
ArrayList<ArrayList<Student>> list = majorMap.get(k);
System.out.println("Multi List------" + list.size());
for (ArrayList<Student> list1 : list) {
// System.out.println(list1);
for (Student object : list1) {
System.out.println(object.getAge() + "," + object.getCity() + "," + object.getName());
}
}
}
}
private ArrayList<Student> addStudentToList() {
ArrayList<Student> list = new ArrayList<Student>();
Student stu1 = new Student();
Student stu2 = new Student();
Student stu3 = new Student();
Student stu4 = new Student();
Student stu5 = new Student();
Student stu6 = new Student();
Student stu7 = new Student();
Student stu8 = new Student();
Student stu9 = new Student();
Student stu10 = new Student();
stu1.setAge(1);
stu1.setName("AMIT");
stu1.setCity("DELHI");
stu2.setAge(2);
stu2.setName("VIVEK");
stu2.setCity("NOIDA");
stu3.setAge(3);
stu3.setName("RAJEEV");
stu3.setCity("DELHI");
stu4.setAge(4);
stu4.setName("KUMAR");
stu4.setCity("NOIDA");
stu5.setAge(5);
stu5.setName("VINEET");
stu5.setCity("DELHI");
stu6.setAge(6);
stu6.setName("RAHUL");
stu6.setCity("GURGAON");
stu7.setAge(7);
stu7.setName("GOVIND");
stu7.setCity("DELHI");
stu8.setAge(8);
stu8.setName("REENA");
stu8.setCity("NOIDA");
stu9.setAge(9);
stu9.setName("ISACK");
stu9.setCity("GURGAON");
stu10.setAge(10);
stu10.setName("GUNJAN");
stu10.setCity("MORADABAD");
list.add(stu1);
list.add(stu2);
list.add(stu3);
list.add(stu4);
list.add(stu5);
list.add(stu6);
list.add(stu7);
list.add(stu8);
list.add(stu9);
list.add(stu10);
/*
* stu1.setAge(1); stu1.setName("A"); stu1.setCity("DL");
*
*/ return list;
}
}
/*
*@ CopyRight : 2016
*@ Author : RAJEEVKUMAR
*@ Date : Jan 17, 2017
*@ File Name : Student.java
*@ Package : codding.time.array
*
*/
package codding.time.array;
public class Student {
private int age;
private String name;
private String city;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
/*
* @Override public String toString() { return name;
*
* }
*/
}
post a comment