• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem in converting List to ConcurrentSkipListSet

 
coder hunter
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.util.List;
import java.util.concurrent.ConcurrentSkipListSet;

public class ConcurrentSkipListSetEx {

public static void main(String[] args)
{

List<String> l1 = new ArrayList<String>();

ConcurrentSkipListSet<String> l = new ConcurrentSkipListSet<String>(l1);
System.out.println(l1.add("ramesh"));
System.out.println(l1.add("babu"));
System.out.println(l1.add("yagnam"));
System.out.println(l1.add("adi"));
System.out.println(l1.add("adi"));

System.out.println(l);
}

}

this is simple , i am trying to create ConcurrentSkipListSet from List , but when trying to print created ConcurrentSkipListSet , l , it is presenting empty , can any one suggest , if any issue in this
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have two totally separate list objects. You're adding to one, not to the other.

This line: creates a new CSLS object based on the current state of the AL object at the time. After that, you have two totally separate List objects. Adding to or removing from one has no effect on the other. If you put that line after you've added to the AL, you'll see those objects in the CSLS.

(Note also that whatever objects are present in the original list when you copy it to the second one, if you modify the state of any of those objects (as opposed to modifying the state of one of the lists), you'll see that change in both lists. This is because the constructor does a shallow copy. The new list has its own set of references to the objects that the original list had, and changing the references in one list doesn't affect the other. But each reference from one list points to the same object as the corresponding reference in the other list, so changing the state (contents) of that object is visible through both lists' references.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic