java - How to remove repeated items of a list and copy the list into another list -


i have list of items need remove repeated ones , copy list list. problem cant copy list other list.

code

    .....     private list mylist = new arraylist();     .....      linkedhashset hs = new linkedhashset();     hs.addall(mylist);     mylist.clear();     mylist.addall(hs);       myclass.getitems().clear();     myclass.setitems(mylist); 

myclass.java

  .....    private list items = new arraylist();     public void setitems(list myitems) {          (int = 0; < myitems.size(); i++) {    <<this loop shows items             system.out.println(myitems.get(i));         }         this.items.clear();         this.items.addall(myitems);         (int = 0; < items.size(); i++) {  << loop not show            system.out.println(items.get(i));         }     } 

desired result

mylist  >> a,b,c,a,d,c change a,b,c,d copy items items >> a,b,c,d 

you can use linkedhashset. preserve insertion order , insure no duplicates.

for problem number 2 adding anyting list. code have works

list<integer> mylist = new arraylist<integer>();     mylist.add(3);     mylist.add(3);       hashset hs = new hashset();     hs.addall(mylist);     mylist.clear();     mylist.addall(hs);       system.out.println(mylist.size()); //prints 1     system.out.println(hs.size());// prints 1 

response edited question:

they both seem print out list fine

public class tmp {    private list<integer> items = new arraylist<integer>();  public void setitems(list<integer> myitems) {      (int = 0; < myitems.size(); i++) {    //<<this loop shows items         system.out.println(myitems.get(i));     }     this.items.clear();     this.items.addall(myitems);     system.out.println();     (int = 0; < items.size(); i++) {  //<< loop shows item         system.out.println(items.get(i));     } } public static void main(string[] args) {     tmp t = new tmp();     list<integer> mylist = new arraylist<integer>();     mylist.add(3);     mylist.add(4);      t.setitems(mylist);   } 

}

are both these pieces of code in same class , both list variables pointing same arraylist instance. if calling clear on 1 clears out both lists (since both variables pointing same list)


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -