【问题标题】:Geting list of hmaps where containing all tags of a user获取包含用户所有标签的地图列表
【发布时间】:2020-12-08 19:35:10
【问题描述】:

1问题是当添加一个新的hmap时,它的元素被添加到最旧的元素上。如何获取每个包含用户所有标签的 hmap 列表?

public List<HashMap<String, String>>tag_pp_user(String name,List<String> c) throws TwitterException, IOException, InterruptedException{ 
              List<HashMap<String, String>> c4 = new ArrayList<>();
              // c4 id a list of hashmap where each hashmap must contain the hahstags of a user wherr the key is the hashtag and the value is the name of the user
              HashMap<String, String> hmap = new HashMap<String, String>();          
        if (c.size()>40) {
            // c is a list of friends where i'm searching the tags of  each c.get(j) and i need just 40 friend
            for(int j=0;j<40;j++){
                hmap=hashtag_user(c.get(j));
                c4.add(hmap);  }
            }
        else {
            
            for(int j=0;j<c.size();j++){
                hmap=hashtag_user(c.get(j));
                System.out.println(hmap.size());
                c4.add(hmap);     
             }  
        }
   
        // when i view c4 i find that the new hashmap is added in the previous hmaps  
        return c4;
   
 }

the result

public HashMap<String, String> hashtag_user(String name) throws TwitterException, IOException {      

列表 l= new ArrayList(); HashMap hmap = new HashMap();

Paging page = new Paging(1,200);
int p=1;
while(p<=10){
    page.setPage(p);
    
    statuses.addAll(twitter.getUserTimeline(name,page));
    
    p++;
} 
   for(Status s:statuses){
      
                    HashtagEntity[]  hts =s.getHashtagEntities(); //hmap.clear();
                   
                  // System.out.println(s.getText());
                            if ( hts.length > 0) {
                               // hmap.clear();
                               for(int j =0;j<hts.length;j++){//contains(hts[j].getText())
                                 //  if(!hmap.containsKey(hts[j].getText()))//{
                                  // System.out.println("**********"+hts[j].getText());
                                
                        if( !hts[j].getText().equals("hashtag") && !hts[j].getText().equals("tweet") && !hts[j].getText().equals("RT") && !hts[j].getText().equals("rt") && !hts[j].getText().equals("https")&& !hts[j].getText().equals("HTTPS")){
                                //System.out.println("**********"+hts[j].getText());
                                
                                 hmap.put(hts[j].getText(),name);//}
                                
                           
                              }
                               }
                            
                            
                               }   // System.out.println(hts.length);  
                       //Collections.unmodifiableMap(hmap);
                               
   }

 return hmap; 
  }  

【问题讨论】:

  • 请重新格式化您的问题以正确显示代码!
  • 在 for 循环中为 if-else HashMap&lt;String, String&gt; hmap = new HashMap&lt;String, String&gt;() 初始化 HashMap 目前所有的 hashmap 都具有相同的引用
  • 这给出了同样的问题i.stack.imgur.com/w02Lx.png

标签: java arraylist hashmap hashtag


【解决方案1】:

原因是您对对象使用了相同的引用 hmap,它在 for 循环之外并且它不是该 for 循环的本地对象,并且该引用将具有 hmap 指向的最新对象......以保护它,每次要添加到列表时都创建新的 HashMap!!!

 public List<HashMap<String, String>>tag_pp_user(String name,List<String> c) throws TwitterException, IOException, InterruptedException{ 
                  List<HashMap<String, String>> c4 = new ArrayList<>();
                  // c4 id a list of hashmap where each hashmap must contain the hahstags of a user wherr the key is the hashtag and the value is the name of the user
                  //HashMap<String, String> hmap = new HashMap<String, String>();          
            if (c.size()>40) {
                // c is a list of friends where i'm searching the tags of  each c.get(j) and i need just 40 friend
                for(int j=0;j<40;j++){
                    //HashMap<String, String> hmap = new HashMap<String, String>();                        
                    //hmap=hashtag_user(c.get(j));
                    c4.add(hashtag_user(c.get(j)));  }
                }
            else {
                
                for(int j=0;j<c.size();j++){
                    //HashMap<String, String> hmap = new HashMap<String, String>();          
                    //hmap=hashtag_user(c.get(j));
                    //System.out.println(hmap.size());
                    c4.add(hashtag_user(c.get(j)));     
                 }  
            }
       
            // when i view c4 i find that the new hashmap is added in the previous hmaps  
            return c4;
       
     }

【讨论】:

  • 我试过这个语句,但它给出了相同的结果i.stack.imgur.com/w02Lx.png
  • @amelahlem 我已经更新了代码,请使用代码...您可以直接添加到c4列表中,因为“hashtag_user(c.get(j))”将返回您可以添加的地图到 c4 列表
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-30
  • 2015-08-19
  • 2019-10-30
  • 2021-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多