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

fruv7luv  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(256)

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;

 }

结果呢

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

list l=新数组列表();hashmap<string,string>hmap=新hashmap<string,string>();

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; 
  }
7ivaypg9

7ivaypg91#

原因是你正在为对象使用相同的引用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;

     }

相关问题