英文:
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<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 <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(); //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;
}
答案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<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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论