[BAEL-9550] - Moved articles and codes from core-java-collections module to java-collections-convversions and java-collections-maps module
This commit is contained in:
+52
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.convertcollectiontoarraylist;
|
||||
|
||||
/**
|
||||
* This POJO is the element type of our collection. It has a deepCopy() method.
|
||||
*
|
||||
* @author chris
|
||||
*/
|
||||
public class Foo {
|
||||
|
||||
private int id;
|
||||
private String name;
|
||||
private Foo parent;
|
||||
|
||||
public Foo() {
|
||||
}
|
||||
|
||||
public Foo(int id, String name, Foo parent) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Foo getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public void setParent(Foo parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public Foo deepCopy() {
|
||||
return new Foo(
|
||||
this.id, this.name, this.parent != null ? this.parent.deepCopy() : null);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.convertlisttomap;
|
||||
|
||||
public class Animal {
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
public Animal(int id, String name) {
|
||||
this.id = id;
|
||||
this.setName(name);
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.convertlisttomap;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import org.apache.commons.collections4.MapUtils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ConvertListToMapService {
|
||||
|
||||
public Map<Integer, Animal> convertListBeforeJava8(List<Animal> list) {
|
||||
|
||||
Map<Integer, Animal> map = new HashMap<>();
|
||||
|
||||
for (Animal animal : list) {
|
||||
map.put(animal.getId(), animal);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public Map<Integer, Animal> convertListAfterJava8(List<Animal> list) {
|
||||
Map<Integer, Animal> map = list.stream().collect(Collectors.toMap(Animal::getId, animal -> animal));
|
||||
return map;
|
||||
}
|
||||
|
||||
public Map<Integer, Animal> convertListWithGuava(List<Animal> list) {
|
||||
|
||||
Map<Integer, Animal> map = Maps.uniqueIndex(list, Animal::getId);
|
||||
return map;
|
||||
}
|
||||
|
||||
public Map<Integer, Animal> convertListWithApacheCommons(List<Animal> list) {
|
||||
|
||||
Map<Integer, Animal> map = new HashMap<>();
|
||||
|
||||
MapUtils.populateMap(map, list, Animal::getId);
|
||||
|
||||
return map;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user