Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Java in General and the fly likes problem in converting List to ConcurrentSkipListSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "problem in converting List to ConcurrentSkipListSet" Watch "problem in converting List to ConcurrentSkipListSet" New topic
Author

problem in converting List to ConcurrentSkipListSet

coder hunter
Greenhorn

Joined: Apr 20, 2012
Posts: 1
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: problem in converting List to ConcurrentSkipListSet