Set the value of a get(); on a map

Asked

Viewed 79 times

-2

How do I put the value of ipOrigem being a string? And also, without refactoring the method that has objects used in several others.

List<FileVO> arquivos = digitalizacaoVO.getArquivos();
Map<Long, List<FileVO>> arquivosMap = digitalizacaoVO.getArquivosMap();
String ipOrigem = digitalizacaoVO.getIpOrigem(); 


    if(arquivos != null && !arquivos.isEmpty()) {
        Long documentoId = documento != null ? documento.getId() : null;
        arquivosMap = new HashMap<>();
        arquivosMap.put(documentoId, arquivos);
    }
  • What type of variable digitalizacaoVO? Simply digitalizacaoVO.ipOrigem = novoIp or digitalizacaoVO.setIpOrigem(novoIp) doesn’t work?

  • works, but what I need is for the value of this getIpOrigem(), to be mapped inside the fileMap. Right below I load the mapped values, play them inside an array list and then save the metadata of the image that was loaded. I don’t know if it’s clear...

  • 1

    Do you want to set the file and IP in Hashmap? Create a class with these two properties and use it

1 answer

0


If you want this value inside your map, without modifying the map, you could simply add a field to the class FileVO:

public class FileVO{
    private String ipOrigem;

//outros métodos bla blu

public void setIpOrigem(String ip){
this.ipOrigem = ip;
}

public String getIpOrigem(){
return this.ipOrigem;
}
}

After that, just use a loop and fill each object in the list. Using Java >8:

List<FileVO> arquivos = digitalizacaoVO.getArquivos();
String ipOrigem = digitalizacaoVO.getIpOrigem(); 
arquivos.forEach(a -> a.setIpOrigem(ipOrigem));

So your Hashmap would already be worth.

  • It worked man! In fact I already had a VO with the values, just could not map them. Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.