728x90
Map의 순회
// Loop a Map
Map<String, String> cities = new HashMap<>();
cities.put("Tokyo", "Japan");
cities.put("Seoul", "Korea");
cities.put("Beijing", "China");
cities.put("Paris", "France");
cities.put("Washington", "USA");
cities.put("Brazilia", "Brazil");
// Normal ways
//1)
for (Map.Entry<String, String> entry : cities.entrySet()) {
System.out.println("Cities: " + entry.getKey() + ", " + entry.getValue());
}
//2)
for (String key : cities.keySet()) {
System.out.println("Cities: " + key + ", " + cities.get(key));
}
// Java8 forEach, Lambda
// 1)
cities.forEach((k, v) -> System.out.println("Cities: " + k + ", " + v));
// 2)
cities.forEach((k, v) -> {
System.out.println("Cities: " + k + ", " + v);
});
List의 순회
// Loop a List
List<String> fruits = new ArrayList<>();
fruits.add("banana");
fruits.add("apple");
fruits.add("peach");
fruits.add("lemon");
fruits.add("mango");
// Normal way
// 1)
for (String item : fruits) {
System.out.println("Fruits: " + item);
}
// Java8 forEach, Lambda, Stream
// 1)
fruits.forEach(item -> System.out.println("Fruits: " + item));
// 2)
fruits.forEach(item -> {
System.out.println("Fruits: " + item);
});
// 3)
fruits.stream()
.forEach(item -> System.out.println("Fruits: " + item));
List의 Map순회
List<Map<String, Object>> list = dao.getAllDeptListOrderByNoDesc();
for (Map<String, Object> map:list) {
System.out.println(map.get("DEPTNO") + " " + map.get("DNAME") + " " + map.get("LOC"));
}
List에서 특정 문자열이 들어있는지 확인
contains()
다음과 같이 List.contains()를 이용하면 간단히 특정 문자열을 찾을 수 있습니다.
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Kiwi");
list.add("Orange");
String fruit = "Orange";
if (list.contains(fruit)) {
System.out.println(fruit + " is in the List");
}
for
위의 코드는 for를 이용하여 간단히 구현할 수도 있습니다.
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Kiwi");
list.add("Orange");
String fruit = "Orange";
for (String item : list) {
if (item.equals(fruit)) {
System.out.println(fruit + " is in the List");
break;
}
}
for (int i = 0; i < list.size(); i++) {
String item = list.get(i);
if (item.equals(fruit)) {
System.out.println(fruit + " is in the List. The index is " + i);
break;
}
}
Iterator
for처럼 Iterator를 이용하여 Loop를 만들고 원하는 문자열을 찾을 수 있습니다.
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Kiwi");
list.add("Orange");
String fruit = "Orange";
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
String item = iterator.next();
if (item.equals(fruit)) {
System.out.println(fruit + " is in the List");
}
}
Stream
Stream을 이용하여 리스트에서 특정 문자열을 찾을 수 있습니다.
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Kiwi");
list.add("Orange");
String fruit = "Orange";
List<String> result = list.stream()
.filter(str -> str.trim().equals(fruit))
.collect(Collectors.toList());
if (result.size() > 0) {
System.out.println(fruit + " is in the List: " + result);
}
자바에서 List 객체를 Map으로 바꾸는 방법
public class ListToMap {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("aaaca");
list.add("aaa");
list.add("aaa");
list.add("bbbcccs");
list.add("ccc");
Map<String, Long> map = new HashMap<>();
// Java8 이전에 사용하던 방법입니다.
for (String target : list) {
Long num = map.get(target);
if (num == null) {
map.put(target, 1l);
} else {
map.put(target, num + 1);
}
}
System.out.println(map);
__________________________________________
// 리스트에서 map으로 바꾸는 방법 입니다.
map = list.stream().collect(Collectors.groupingBy(arg -> arg, HashMap::new,
Collectors.counting()));
System.out.println(map);
__________________________________________
//List에서 Map형태의 데이터가 있는 경우 입니다.
List<HashMap<Object, Object>> list2 = new ArrayList<>();
HashMap<Object, Object> a = new HashMap<Object, Object>();
a.put("key", "KA");
a.put("arg", 1234);
a.put("desc", "asdfsdaf");
list2.add(a);
a = new HashMap<Object, Object>();
a.put("key", "K");
a.put("arg", 675);
a.put("desc", "qreweraa");
list2.add(a);
a = new HashMap<Object, Object>();
a.put("key", "KA");
a.put("arg", 6745);
a.put("desc", "qAreweraa");
list2.add(a);
a = new HashMap<Object, Object>();
a.put("key", "KA");
a.put("arg", 32419);
a.put("desc", "qAreweraa");
list2.add(a);
__________________________________________
//그룹한 데이터를 LIST로 담았습니다.
LinkedHashMap<Object, List<HashMap<Object, Object>>> result = null;
result = list2.stream()
.collect(Collectors.groupingBy(arg -> arg.get("key")
, LinkedHashMap::new, Collectors.toList()));
result.forEach((key, value) -> {
System.out.println(key + " : " + value);
});
__________________________________________
//그룹한 데이터를 갯수를 세어 Long형태로 넣었습니다.
list2.stream().collect(Collectors.groupingBy(arg -> arg.get("key"),
LinkedHashMap::new, Collectors.counting()))
.forEach((key, value) -> {
System.out.println(key + "(count)" + " : " + value);
});
__________________________________________
//간단하게 최소값, 최대값을 가져오는 부분 입니다.
Optional<String> maxStyle1 = list.stream().collect(Collectors
.maxBy(Comparator.comparingInt(String::length)));
Optional<String> maxStyle2 = list.stream().max(Comparator
.comparingInt(String::length));
System.out.println("maxStyle1 : " + maxStyle1.get());
System.out.println("maxStyle2 : " + maxStyle2.get());
Optional<String> minStyle1 = list.stream().collect(Collectors
.minBy(Comparator.comparingInt(String::length)));
Optional<String> minStyle2 = list.stream().min(Comparator
.comparingInt(String::length));
System.out.println("minStyle1 : " + minStyle1.get());
System.out.println("minStyle2 : " + minStyle2.get());
}
}
728x90