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

huangapple go评论83阅读模式
英文:

Geting list of hmaps where containing all tags of a user

问题

public List<HashMap<String, String>> tag_pp_user(String name, List<String> c) throws TwitterException, IOException, InterruptedException {
    List<HashMap<String, String>> c4 = new ArrayList<>();
    HashMap<String, String> hmap = new HashMap<String, String>();

    if (c.size() > 40) {
        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);
        }
    }

    return c4;
}

public HashMap<String, String> hashtag_user(String name) throws TwitterException, IOException {
    List<String> l = new ArrayList();
    HashMap<String, String> hmap = new 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();
        if (hts.length > 0) {
            for (int j = 0; j < hts.length; j++) {
                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")) {
                    hmap.put(hts[j].getText(), name);
                }
            }
        }
    }

    return hmap;
}

Please note that the provided code has some potential issues, such as using the statuses variable without prior declaration and initialization. Additionally, there could be missing imports and parts of the code that are not provided in your original text.

英文:

1The problem is when adding a new hmap, its elements are added to the oldest ones. How can I get list of hmaps where each one contain all tags of a user?

public List&lt;HashMap&lt;String, String&gt;&gt;tag_pp_user(String name,List&lt;String&gt; c) throws TwitterException, IOException, InterruptedException{ 
List&lt;HashMap&lt;String, String&gt;&gt; c4 = new ArrayList&lt;&gt;();
// 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&lt;String, String&gt; hmap = new HashMap&lt;String, String&gt;();          
if (c.size()&gt;40) {
// c is a list of friends where i&#39;m searching the tags of  each c.get(j) and i need just 40 friend
for(int j=0;j&lt;40;j++){
hmap=hashtag_user(c.get(j));
c4.add(hmap);  }
}
else {
for(int j=0;j&lt;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&lt;String, String&gt; hashtag_user(String name) throws TwitterException, IOException {      

List <String> l= new ArrayList();
HashMap<String, String> hmap = new HashMap<String, String>();

Paging page = new Paging(1,200);
int p=1;
while(p&lt;=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 &gt; 0) {
// hmap.clear();
for(int j =0;j&lt;hts.length;j++){//contains(hts[j].getText())
//  if(!hmap.containsKey(hts[j].getText()))//{
// System.out.println(&quot;**********&quot;+hts[j].getText());
if( !hts[j].getText().equals(&quot;hashtag&quot;) &amp;&amp; !hts[j].getText().equals(&quot;tweet&quot;) &amp;&amp; !hts[j].getText().equals(&quot;RT&quot;) &amp;&amp; !hts[j].getText().equals(&quot;rt&quot;) &amp;&amp; !hts[j].getText().equals(&quot;https&quot;)&amp;&amp; !hts[j].getText().equals(&quot;HTTPS&quot;)){
//System.out.println(&quot;**********&quot;+hts[j].getText());
hmap.put(hts[j].getText(),name);//}
}
}
}   // System.out.println(hts.length);  
//Collections.unmodifiableMap(hmap);
}
return hmap; 
}  

答案1

得分: 0

原因是您在对象外部使用了相同的引用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是一个哈希映射列表,其中每个哈希映射必须包含用户的哈希标签,其中键是哈希标签,值是用户的名称
    //HashMap<String, String> hmap = new HashMap<String, String>();
    if (c.size() > 40) {
        // c是一个朋友列表,我正在查找每个c.get(j)的标签,我只需要40个朋友
        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)));
        }
    }
    // 当我查看c4时,我发现新的哈希映射添加在先前的hmap中
    return c4;
}
英文:

The reason is that you are using same reference hmap for the Object, which is outside of for loop and its not local to that for loop, and the reference will have the latest object pointed by hmap...to protect it, create every time new HashMap whenever you are going to add to list!!!

 public List&lt;HashMap&lt;String, String&gt;&gt;tag_pp_user(String name,List&lt;String&gt; c) throws TwitterException, IOException, InterruptedException{ 
List&lt;HashMap&lt;String, String&gt;&gt; c4 = new ArrayList&lt;&gt;();
// 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&lt;String, String&gt; hmap = new HashMap&lt;String, String&gt;();          
if (c.size()&gt;40) {
// c is a list of friends where i&#39;m searching the tags of  each c.get(j) and i need just 40 friend
for(int j=0;j&lt;40;j++){
//HashMap&lt;String, String&gt; hmap = new HashMap&lt;String, String&gt;();                        
//hmap=hashtag_user(c.get(j));
c4.add(hashtag_user(c.get(j)));  }
}
else {
for(int j=0;j&lt;c.size();j++){
//HashMap&lt;String, String&gt; hmap = new HashMap&lt;String, String&gt;();          
//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;
}

huangapple
  • 本文由 发表于 2020年8月19日 21:54:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63488569.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定