Skip to main content

2024-08-07

One-liner

"Love is actually simple; the hard part is accepting it." — "Shaman King" · hitokoto


Java Map.getOrDefault

You can achieve this by performing a null check directly in the vo.setA() method. Here's an example demonstrating how to check if map.get("A") is null, set it to 0 if it is, or set it to the value of map.get("A") otherwise.

Assuming your VO class is named MyVO and its a property has a setA method, here’s the code:

import java.util.Map;

public class Main {
public static void main(String[] args) {
// Create a sample VO object
MyVO vo = new MyVO();

// Create a sample Map
Map<String, Integer> map = Map.of("A", 10, "B", 20);

// Use map.getOrDefault to set the value of 'a'
vo.setA(map.getOrDefault("A", 0));

// Print the value of 'a' in VO
System.out.println("Value of 'a' in VO: " + vo.getA());
}
}

class MyVO {
private int a;

public int getA() {
return a;
}

public void setA(int a) {
this.a = a;
}
}

In this example:

  1. map.getOrDefault("A", 0) returns the value associated with key "A" if it exists, or 0 if it doesn’t.
  2. vo.setA(map.getOrDefault("A", 0)) directly sets the a property.

Java list to map

To place count and exp into another class Fin, and add a List<Fin> field to MyVO class, and then calculate the total sum of count and exp for each status, you can do it as follows:

Define Fin class and the modified MyVO class:

import java.math.BigDecimal;
import java.util.List;

class Fin {
private BigDecimal count;
private BigDecimal exp;

// Constructors, getters, and setters
public Fin(BigDecimal count, BigDecimal exp) {
this.count = count;
this.exp = exp;
}

public BigDecimal getCount() {
return count;
}

public void setCount(BigDecimal count) {
this.count = count;
}

public BigDecimal getExp() {
return exp;
}

public void setExp(BigDecimal exp) {
this.exp = exp;
}
}

enum Status {
PLAN,
IN,
COM
}

class MyVO {
private String name;
private Status status;
private List<Fin> fins;

// Constructors, getters, and setters
public MyVO(String name, Status status, List<Fin> fins) {
this.name = name;
this.status = status;
this.fins = fins;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Status getStatus() {
return status;
}

public void setStatus(Status status) {
this.status = status;
}

public List<Fin> getFins() {
return fins;
}

public void setFins(List<Fin> fins) {
this.fins = fins;
}
}

Convert List<MyVO> to Map<Status, BigDecimal> and calculate the total sum of count and exp for all Fin objects:

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

public class Main {
public static void main(String[] args) {
// Create sample Fin objects
List<Fin> fins1 = Arrays.asList(
new Fin(new BigDecimal("10"), new BigDecimal("5")),
new Fin(new BigDecimal("20"), new BigDecimal("10"))
);

List<Fin> fins2 = Arrays.asList(
new Fin(new BigDecimal("15"), new BigDecimal("7")),
new Fin(new BigDecimal("25"), new BigDecimal("12"))
);

List<Fin> fins3 = Arrays.asList(
new Fin(new BigDecimal("30"), new BigDecimal("15"))
);

// Create sample VO objects
List<MyVO> list = Arrays.asList(
new MyVO("A", Status.PLAN, fins1),
new MyVO("B", Status.IN, fins2),
new MyVO("C", Status.PLAN, fins3)
);

// Convert List<MyVO> to Map<Status, BigDecimal>, summing count and exp for all Fin objects
Map<Status, BigDecimal> statusSumMap = list.stream()
.collect(Collectors.groupingBy(
MyVO::getStatus, // Group by status
Collectors.reducing(
BigDecimal.ZERO,
vo -> vo.getFins().stream()
.map(fin -> fin.getCount().add(fin.getExp()))
.reduce(BigDecimal.ZERO, BigDecimal::add), // Sum count and exp
BigDecimal::add // Combine results for the same status
)
));

// Print the results
statusSumMap.forEach((status, totalSum) -> System.out.println("Status: " + status + ", Total Sum: " + totalSum));
}
}

In this example:

  1. list.stream() converts the list to a stream.
  2. collect(Collectors.groupingBy(...)) collects the stream into a Map:
    • MyVO::getStatus groups by status.
    • Collectors.reducing(...) sums count and exp for each MyVO object’s fins list and combines results for the same status.

Complete Example Code:

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

class Fin {
private BigDecimal count;
private BigDecimal exp;

// Constructors, getters, and setters
public Fin(BigDecimal count, BigDecimal exp) {
this.count = count;
this.exp = exp;
}

public BigDecimal getCount() {
return count;
}

public void setCount(BigDecimal count) {
this.count = count;
}

public BigDecimal getExp() {
return exp;
}

public void setExp(BigDecimal exp) {
this.exp = exp;
}
}

enum Status {
PLAN,
IN,
COM
}

class MyVO {
private String name;
private Status status;
private List<Fin> fins;

// Constructors, getters, and setters
public MyVO(String name, Status status, List<Fin> fins) {
this.name = name;
this.status = status;
this.fins = fins;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Status getStatus() {
return status;
}

public void setStatus(Status status) {
this.status = status;
}

public List<Fin> getFins() {
return fins;
}

public void setFins(List<Fin> fins) {
this.fins = fins;
}
}

public class Main {
public static void main(String[] args) {
// Create sample Fin objects
List<Fin> fins1 = Arrays.asList(
new Fin(new BigDecimal("10"), new BigDecimal("5")),
new Fin(new BigDecimal("20"), new BigDecimal("10"))
);

List<Fin> fins2 = Arrays.asList(
new Fin(new BigDecimal("15"), new BigDecimal("7")),
new Fin(new BigDecimal("25"), new BigDecimal("12"))
);

List<Fin> fins3 = Arrays.asList(
new Fin(new BigDecimal("30"), new BigDecimal("15"))
);

// Create sample VO objects
List<MyVO> list = Arrays.asList(
new MyVO("A", Status.PLAN, fins1),
new MyVO("B", Status.IN, fins2),
new MyVO("C", Status.PLAN, fins3)
);

// Convert List<MyVO> to Map<Status, BigDecimal>, summing count and exp for all Fin objects
Map<Status, BigDecimal> statusSumMap = list.stream()
.collect(Collectors.groupingBy(
MyVO::getStatus, // Group by status
Collectors.reducing(
BigDecimal.ZERO,
vo -> vo.getFins().stream()
.map(fin -> fin.getCount().add(fin.getExp()))
.reduce(BigDecimal.ZERO, BigDecimal::add), // Sum count and exp
BigDecimal::add // Combine results for the same status
)
));

// Print the results
statusSumMap.forEach((status, totalSum) -> System.out.println("Status: " + status + ", Total Sum: " + totalSum));
}
}

This code shows how to convert a List<MyVO> to a Map<Status, BigDecimal> and calculate the total sum of count and exp for each status.