在java中返回多个map <string,string>

时间:2017-08-23 05:30:26

标签: java java-8 hashmap hashset

我试图将void方法移动到它自己的类并返回map的键值对(debitCardDetails,creditCardDetails)。什么是在父类中返回多个映射并提取的正确方法,还是我需要合并Map<String, Map<String, String>> result并返回结果?

public class CardService {

    public void serviceCall(List<CardImplementation> mapDetails) {

        final Map<String, String> debitCardDetailsMap = new HashMap<String, String>();
        final Map<String, String> creditCardDetailsMap = new HashMap<String, String>();

        /*code implementation */
        debitCardDetailsMap.put("type", "VISA");
        debitCardDetailsMap.put("bank", "BofA");
        debitCardDetailsMap.put("rank", "2");

        creditCardDetailsMap.put("type", "VISA");
        creditCardDetailsMap.put("number","23345");
        creditCardDetailsMap.put("bank", "citi");
    }
}

parentClass看起来像这样

public class ServiceClass {

CardService cardService = new CardService();
  if ( debitCardDetailsMap.size() > 0 ) {
      checkAccount(debitCardDetailsMap);
  }
  if ( creditCardDetailsMap.size()>0 ) {
     checkScore(creditCardDetailsMap);
  }
}

3 个答案:

答案 0 :(得分:2)

有多种方法可以实现您想要做的事情,但我建议使用OOP方式来实现。

OOP方法的优点是,代码将是模块化的,易于理解,可扩展和可扩展,而不会影响现有功能。

  1. 创建一个抽象类Card,它可以是不同类型卡片的父级,即DebitCardCreditCard
  2. 创建两个java类DebitCardCreditCard,每个类扩展Card,每个类都有自己的属性(类型,数字等),getters-setter和构造函数。
  3. 有一个枚举CardType,用于定义一个字符串,用于定义卡片类型,如CREDIT_CARDDEBIT_CARD
  4. 在serviceCall方法中,您可以使用Map<String, Map<String, String>>来保存不同类型的卡,而不是Map<CardType, List<Card>>。填充它们如下......
  5. 鉴于地图中的枚举,您可以在ServiceClass中单独处理每张卡片,如下所示......
  6. public Map<CardType, List<Card>> populateData (){
        Map<CardType, List<Card>> cardMap = new HashMap<CardType, List<Card>>();
    
        List<Card> debitCards = new LinkedList<Card>();
        debitCards.add(new DebitCard("Visa", "Bofa", "2"));
        cardMap.put(CardType.DEBIT_CARD, debitCards);
    
        List<Card> creditCards = new LinkedList<Card>();
        creditCards.add(new CreditCard("Visa", "23345", "citi"));
        cardMap.put(CardType.CREDIT_CARD, creditCards);
    
        return cardMap;
    }
    
    public void handleData (Map<CardType, List<Card>> cardMap) {
        if (cardMap.get(CardType.CREDIT_CARD) != 0 && cardMap.get(CardType.CREDIT_CARD).size() > 0)) {
              // do something for credit cards list
        } else if (cardMap.get(CardType.CREDIT_CARD) != 0 && cardMap.get(CardType.DEBIT_CARD).size() > 0)) {
              // do something for debit cards list
        } // have as many else as enum types
    }
    

答案 1 :(得分:1)

您可以返回carddetailsDTO对象并使用DTO进行游戏。像下面的东西。

mport java.util.Map;

public class CardDetailsDTO {

    private Map<String,String> debitCardDetails;

    private Map<String,String> creditCardDetails;

    public Map<String,String> getCreditCardDetails() {
        return creditCardDetails;
    }

    public void setCreditCardDetails(Map<String,String> creditCardDetails) {
        this.creditCardDetails = creditCardDetails;
    }

    public Map<String,String> getDebitCardDetails() {
        return debitCardDetails;
    }

    public void setDebitCardDetails(Map<String,String> debitCardDetails) {
        this.debitCardDetails = debitCardDetails;
    }


}

现在在主类中你可以返回此dto。

如下所示

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CardService {

    public CardDetailsDTO serviceCall(List<?> mapDetails) {

        CardDetailsDTO cardDetailsDTO = new CardDetailsDTO();
        final Map<String, String> debitCardDetailsMap = new HashMap<String, String>();
        final Map<String, String> creditCardDetailsMap = new HashMap<String, String>();


        /*code implementation */
        debitCardDetailsMap.put("type", "VISA");
        debitCardDetailsMap.put("bank", "BofA");
        debitCardDetailsMap.put("rank", "2");

        cardDetailsDTO.setDebitCardDetails(debitCardDetailsMap);

        creditCardDetailsMap.put("type", "VISA");
        creditCardDetailsMap.put("number","23345");
        creditCardDetailsMap.put("bank", "citi");

        cardDetailsDTO.setCreditCardDetails(creditCardDetailsMap);

        return cardDetailsDTO;
    }
}

答案 2 :(得分:1)

实际上你有很多方法可以做到,我会展示一些。

无效方法

public void populateMaps (Map<String, String> debitCardDetailsMap, Map<String, String> creditCardDetailsMap) {
    debitCardDetailsMap.put("type", "VISA");
    debitCardDetailsMap.put("bank", "BofA");
    debitCardDetailsMap.put("rank", "2");

    creditCardDetailsMap.put("type", "VISA");
    creditCardDetailsMap.put("number","23345");
    creditCardDetailsMap.put("bank", "citi");
}

你需要实例化并从调用方法传入地图,这就是你如何避免返回类型(它将保持无效)

返回Map<String, Map<String, String>>

public Map<String, Map<String, String>> populateMaps() {
    //instantiate the maps
    Map<String, Map<String, String>> map = new HashMap<>();
    final Map<String, String> debitCardDetailsMap = new HashMap<>();
    final Map<String, String> creditCardDetailsMap = new HashMap<>();

    //populate the core maps
    debitCardDetailsMap.put("type", "VISA");
    debitCardDetailsMap.put("bank", "BofA");
    debitCardDetailsMap.put("rank", "2");

    creditCardDetailsMap.put("type", "VISA");
    creditCardDetailsMap.put("number","23345");
    creditCardDetailsMap.put("bank", "citi");

    //populate the main map
    map.put(debitCardDetailsMap);
    map.put(creditCardDetailsMap);

    return map;
}

这样您就不需要在此方法之外实例化地图。

有更多方法可以创建这些地图,但我认为上面的一次展示会更好一次。