-1

I want to iteate List and If that key is "Happy" then I want to store that in a variable.

Eg:- List<Map> list=service.getValues();

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Gen
  • 2,400
  • 4
  • 24
  • 46

4 Answers4

4

first of all DONT USE RAW Types

after that, do an enhance for loop to iterate the list, every element is a map, then get the entrySet of that

List<Map<String, String>> list = ...;
        for (final Map<String, String> element : list) {
            for (final Entry<String, String> entryElem : element.entrySet()) {
                System.out.println("Key: " + entryElem.getKey());
                System.out.println("Value: " + entryElem.getValue());
            }
        }

java8

the nested for loops can be resumed to:

list.forEach(element-> element.entrySet().forEach(System.out::println));
Community
  • 1
  • 1
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

I just traversed the list. Getting the map then travesring the map.

String output="";
            List<Map<String,String>> list=new ArrayList<Map<String,String>>();
            for(int i=0;i<list.size();i++)
            {
                Map<String,String> map=list.get(i);
                Set<String> set=map.keySet();
                Iterator it=set.iterator();
                while(it.hasNext())
                {
                    String key=(String) it.next();
                    if(key.equals("Happy"))
                        output=map.get(key);
                }
            }
kaushik
  • 312
  • 1
  • 7
0
    String var=null;
    List<Map> list=new ArrayList<Map>(0);
    for(Map map:list){
    for(Object key:map.keySet()){
    String obj=key.toString(); 
    if(obj.equals("Happy"))
    {
      var=key;
      System.out.println(var);
    }
    }
}
  1. Create a List object
  2. Iterate through it using enhanced for loop.
  3. Store the key one by one in a object(or else you will get Type mismatch error)
  4. convert the object into string 5.finally match your string with "Happy"
  5. if matches, Stroe it in variable and return successfully.
rackdevelop247
  • 86
  • 1
  • 10
  • Also consider removing the usage of raw types: http://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it – Tom Jan 11 '17 at 12:55
-1
List<Map> list = service.getValues();    

for(Map map : list) {
    for(String key : map.keySet()) {
        if(key.equals("Happy")) {
           //do something with key
        }
    }
}
JavaBoy
  • 182
  • 7
  • Type mismatch: cannot convert from element type Object to String – Gen Jan 11 '17 at 11:30
  • where u type case this.. – kaushik Jan 11 '17 at 11:30
  • `Map` because for now, this is like `Map` – AxelH Jan 11 '17 at 11:32
  • Ofcourse, my code is generic. I have no idea how your Map looks like. What type are the Keys? What type are the values? Please tell me – JavaBoy Jan 11 '17 at 11:32
  • JavaBoy, don't write "code only" answers. Explain your answer. – Tom Jan 11 '17 at 11:34
  • @Tom I see you run around stackoverflow and act like the comment-police? Please contribute by helping people, instead of generating comments. Thanks. – JavaBoy Jan 11 '17 at 11:51
  • This does not provide an answer to the question. Once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/14851671) – bsiamionau Jan 11 '17 at 12:36
  • @bsiamionau A code-only answer might not be a good one, but it's still an answer. I would recommend you this post about the LQPRQ: [You're doing it wrong: A plea for sanity in the Low Quality Posts queue](http://meta.stackoverflow.com/questions/287563/youre-doing-it-wrong-a-plea-for-sanity-in-the-low-quality-posts-queue) – FelixSFD Jan 11 '17 at 12:38
  • @FelixSFD this code is not valid, it doesn't provide an answer – bsiamionau Jan 11 '17 at 12:42
  • @bsiamionau That's an issue on StackOverflow, even a broken/incomplete answer is considered an answer and therefore not eligible to be flagged as either VLQ or NAA. – Tom Jan 11 '17 at 12:45
  • @Tom That's not a problem. We have downvotes to show, that an answer is incorrect or not useful. – FelixSFD Jan 11 '17 at 12:47
  • @FelixSFD That *is* a problem, as it pollutes the site with bad/incomplete/wrong answers/question which remains here. This site was supposed to be a collection of question of all kinds (in the scope of this site) with quality answers. One can still vote about the quality of *working* answers, but something that doesn't work shouldn't be tolerated. – Tom Jan 11 '17 at 12:51