MultiValueMap(一个key对应多个value)

lingxue 2018-03-06 11:45:59 阅读 1991 评论 1 喜欢 0
MultiValueMap 

map的特点是一个Key对应一个Value,MultiValueMap可以实现一个key对应多个value,可以替换Map<K, List<V>> 的数据结构。

apache 和  spring 都有提供这个结构:

 

        import org.apache.commons.collections.map.MultiValueMap;
        import java.util.Collection;
        public class TestMain
        {
            public static void main(String[] args) throws IOException
            {
                MultiValueMap mailMap = new MultiValueMap();
                 mailMap.put("number", "1");
                 mailMap.put("number", "30");
                 mailMap.put("name", "wang");
                 mailMap.put("card", "12344");
                 mailMap.put("card", "65432");
                Collection mapcoll = null;
                mapcoll = mailMap.getCollection("card");
                Iterator ii = mapcoll.iterator();
                while(ii.hasNext()){
                   String mailValue = (String) ii.next();
                   System.out.println(mailValue);
                }
           }
   
        }


 

        import org.springframework.util.MultiValueMap;
 
        MultiValueMap<String, String> stringMultiValueMap = new LinkedMultiValueMap<>();
        stringMultiValueMap.add("早班 9:00-11:00", "周一");
        stringMultiValueMap.add("早班 9:00-11:00", "周二");
        stringMultiValueMap.add("中班 13:00-16:00", "周三");
        stringMultiValueMap.add("早班 9:00-11:00", "周四");
        stringMultiValueMap.add("测试1天2次 09:00 - 12:00", "周五");
        stringMultiValueMap.add("测试1天2次 09:00 - 12:00", "周六");
        stringMultiValueMap.add("中班 13:00-16:00", "周日");
         //打印所有值
        Set<String> keySet = stringMultiValueMap.keySet();
        for (String key : keySet) {
            List<String> values = stringMultiValueMap.get(key);
            System.out.println(StringUtils.join(values.toArray()," ")+":"+key);
        }
 /**
 * 用MultiValueMap接收前端提交的Form Data或Query String,可以接收相同的参数名的值到同一个list中
 * @author XuJijun
 * @param params
 * @return
 */
@RequestMapping(value = "multiValueMap")
public Map<String, List<Object>> test2(@RequestParam MultiValueMap<String, Object> params) {
Map<String, List<Object>> resultMap = new HashMap<>();
resultMap = params;
return resultMap;
}

 

支持@用户名回复

×  

:bowtie: :smile: :laughing: :blush: :smiley: :relaxed: :smirk: :heart_eyes: :kissing_heart: :kissing_closed_eyes: :flushed: :relieved: :satisfied: :grin: :wink: :stuck_out_tongue_winking_eye: :stuck_out_tongue_closed_eyes: :grinning: :kissing: :kissing_smiling_eyes: :stuck_out_tongue: :sleeping: :worried: :frowning: :anguished: :open_mouth: :grimacing: :confused: :hushed: :expressionless: :unamused: :sweat_smile: :sweat: :weary: :pensive: :disappointed: :confounded: :fearful: :cold_sweat: :persevere: :cry: :sob: :joy: :astonished: :scream: :neckbeard: :tired_face: :angry: :rage: :triumph: :sleepy: :yum: :mask: :sunglasses: :dizzy_face: :imp: :smiling_imp: :neutral_face: :no_mouth: :innocent: :alien: :yellow_heart: :blue_heart: :purple_heart: :heart: :green_heart: :broken_heart: :heartbeat: :heartpulse: :two_hearts: :revolving_hearts: :cupid: :sparkling_heart: :sparkles: :star: :star2: :dizzy: :boom: :collision: :anger: :exclamation: :question: :grey_exclamation: :grey_question: :zzz: :dash: :sweat_drops: :notes: :musical_note: :fire: :hankey: :poop: :shit: :+1: :thumbsup: :-1: :thumbsdown: :ok_hand: :punch: :facepunch: :fist: :v: :wave: :hand: :open_hands: :point_up: :point_down: :point_left: :point_right: :raised_hands: :pray: :point_up_2: :clap: :muscle: :metal: :walking: :runner: :running: :couple: :family: :two_men_holding_hands: :two_women_holding_hands: :dancer: :dancers: :ok_woman: :no_good: :information_desk_person: :raised_hand: :bride_with_veil: :person_with_pouting_face: :person_frowning: :bow: :couplekiss: :couple_with_heart: :massage: :haircut: :nail_care: :boy: :girl: :woman: :man: :baby: :older_woman: :older_man: :person_with_blond_hair: :man_with_gua_pi_mao: :man_with_turban: :construction_worker: :cop: :angel: :princess: :smiley_cat: :smile_cat: :heart_eyes_cat: :kissing_cat: :smirk_cat: :scream_cat: :crying_cat_face: :joy_cat: :pouting_cat: :japanese_ogre: :japanese_goblin: :see_no_evil: :hear_no_evil: :speak_no_evil: :guardsman: :skull: :feet: :lips: :kiss: :droplet: :ear: :eyes: :nose: :tongue: :love_letter: :bust_in_silhouette: :busts_in_silhouette: :speech_balloon: :thought_balloon:

共1条评论

相似文章

  • 顶部
  • 关灯
  • 留言
  • 点赞