반응형
public class User{
/** 사용자 고유 ID */
private long id;
/** 영어 이름 */
private String name;
/** 한국어 이름 */
private String fullName;
/** 비밀번호 */
private String password;
/** 이메일 */
private String email;
/** 나이 */
private int age;
/** 가입 날짜 */
private Timestamp createDate;
}
위와 같은 User 객체에 값을 넣고, 나이가 같은 사람이 몇 명인지 세는 코드를 작성해보려고 한다.
package collection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import board.beans.User;
/**
* 나이가 동일한 사람들끼리 Count해서 출력
*
*
*/
public class GatheringTest {
public static void main(String[] args) {
// list에 값 추가하기
List<User> users = new ArrayList<User>(Arrays.asList(new User[] { new User("user1", 19), new User("user2", 19),
new User("user3", 20), new User("user4", 18), new User("user5", 20) }));
// Map 생성
Map<Integer, List<User>> ageUsersMap = new HashMap<Integer, List<User>>();
// users list 반복하기
for (User user : users) {
// 각 user의 나이를 가져오기
int age = user.getAge();
// 만약 Map에 age키가 포함되어 있다면
if (ageUsersMap.containsKey(age)) {
// 그 age키에 현재 user의 정보를 넣기
ageUsersMap.get(age).add(user);
} else { // 만약 Map에 age값이 포함되어 있지 않다면
// 새로운 리스트를 만들어서 추가해준다.
List<User> usersWithSameAge = new ArrayList<User>();
usersWithSameAge.add(user);
ageUsersMap.put(age, usersWithSameAge);
}
}
// Map 전체 출력하기
for (Map.Entry<Integer, List<User>> entry : ageUsersMap.entrySet()) {
int age = entry.getKey();
List<User> usersWithSameAge = entry.getValue();
System.out.println("Age " + age + " : " + usersWithSameAge.size());
}
}
}
이렇게 키 값으로 종류를 나눌 수 있다.
User.java
package board.beans;
import java.security.Timestamp;
/**
* 사용자
*
* @author 4ckdn
*
*/
public class User {
/** 사용자 고유 ID */
private long id;
/** 영어 이름 */
private String name;
/** 한국어 이름 */
private String fullName;
/** 비밀번호 */
private String password;
/** 이메일 */
private String email;
/** 나이 */
private int age;
/** 가입 날짜 */
private Timestamp createDate;
/**
* 생성자
*/
public User() {
// Do Nothing
}
/**
* 생성자
*
* @param name
* @param age
*/
public User(String name, int age) {
this.name = name;
this.age = age;
}
/**
* @param id
* @param name
* @param fullName
* @param password
* @param email
* @param age
* @param createDate
*/
public User(long id, String name, String fullName, String password, String email, int age, Timestamp createDate) {
super();
this.id = id;
this.name = name;
this.fullName = fullName;
this.password = password;
this.email = email;
this.age = age;
this.createDate = createDate;
}
/**
* @return the id
*/
public long getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(long id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the fullName
*/
public String getFullName() {
return fullName;
}
/**
* @param fullName the fullName to set
*/
public void setFullName(String fullName) {
this.fullName = fullName;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return the email
*/
public String getEmail() {
return email;
}
/**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
/**
* @return the createDate
*/
public Timestamp getCreateDate() {
return createDate;
}
/**
* @param createDate the createDate to set
*/
public void setCreateDate(Timestamp createDate) {
this.createDate = createDate;
}
}
반응형