微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

获取包含用户所有标签的hmap列表

如何解决获取包含用户所有标签的hmap列表

1问题在于,当添加新的hmap时,其元素会添加到最旧的元素中。如何获取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 {      

List l = new ArrayList(); HashMap hmap =新的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; 
  }  

解决方法

原因是您为该对象使用了相同的引用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=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=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;
       
     }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?