-3

what does it mean it's a array or it's a list type string

List<String> list = new ArrayList<String>(); // why parenthesis
List<String> removeList = new ArrayList<String>();//why parenthesis

and this method so What is the deference between List and Collection and arrys

// what mean this collection
    private void removeColors(Collection<String> collection1,Collection<String> collection2)
    {
        Iterator<String> iterator = collection1.iterator();//what does mean
        while(iterator.hasNext())//what does mean
            if(collection2.contains(iterator.next()))//what does mean
                iterator.remove();//what does mean
    }
  • its for compile time type safety so you won't add something else to your List that is made to have String only – Adi Aug 27 '14 at 11:12
  • The parenthesis are needed because you're calling a method (the constructor) – Eric Aug 27 '14 at 11:15
  • 1
    you should read some basic tutorials before putting question here.See Generics in java – Kumar Abhinav Aug 27 '14 at 11:15
  • See this page about the differences between arraylist and list: http://stackoverflow.com/questions/8028892/what-are-the-major-differences-between-a-collection-an-arraylist-and-a-list-in – babsaai Aug 27 '14 at 11:19

4 Answers4

5

You should read documentation about collections. http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html

Andrey E
  • 856
  • 8
  • 18
1

A Collection is - a collection of data. This is the base interface for any kind of collection (list, map, tree, ...). Any collection is (or should be) Iterable, that means that you can iterate through all the elements of a collection (for example, in a loop).

A List is obviously a collection as well, since a list is a collection of data. That's why the List interface extends the Collection interface.

However, since there are lots of ways to implement a List, List is merely an interface rather than a class. ArrayList implements a list by wrapping an array. Another example would be LinkedList, which stores data by linking the elements to each other. I don't want to discuss their advantages and disadvantages (as you can look them up).

The last thing to consider is the fact that the data you store inside a collection (a list) has a type. Usually, you will only store one type of Object inside a specific collection, which is why the Collection interface (and all of its subinterfaces like List) take a type parameter. This parameter specifies the type of data you would like to store in your list which is good for type safety and convenience.

Now, in your code:

List<String> list = new ArrayList<String>();
List<String> removeList = new ArrayList<String>();

You create a variable called "list". The type of this variable is List<String>, which means: a list of strings. With the new operator, you create the actual object. Obviously, you have to choose an implementation, and you chose "ArrayList". Naturally, you want Strings in your collection, so you specify String as the type parameter. Since you are calling ArrayList's constructor, you need empty parenthesis (so you call the constructor that doesn't take any arguments). The second line of code does the same.

//this method takes two collections
//that means that you can pass any type of collection (including list) to it
//the advantage is that you could also pass another type of collection if you chose to do so
private void removeColors(Collection<String> collection1,Collection<String> collection2)
{
    Iterator<String> iterator = collection1.iterator();//this line takes the iterator of your first collection
    //an iterator is an object that allows you to go through a collection (list)
    //you can get the objects one by one by calling the next() method
    while(iterator.hasNext())//basically, that's what you're doing here:
    //you let the loop continue as long as there are more items inside the iterator, that is, the first collection
        if(collection2.contains(iterator.next()))//so if there's another item, you take it by calling next() and check if the second collection contains it
            iterator.remove();//if that's the case, you remove the item from the first collection
}
//what you've basically achieved:
//you removed all the items from the first collection that you can find in the second collection as well, so you could say:
//collection1 = collection1 - collection2

Now, you could fill the list of strings you created above with data (strings) and do the same with removeList, and then "subtract" removeList from list by calling:

removeColors(list, removeList);
J4v4
  • 780
  • 4
  • 9
0

In Java an array has got a specific size that can't be changed. If you come from other scripting languages like php this may be new to you.

That's why for example ArrayLists are often used. You got a dynamic size where you can add and remove elements as you want.

List<String> tells your compiler that only Strings are accepted ( https://en.wikipedia.org/wiki/Type_safety ).

A List is a specialization of a Collection.

Type Parameters: E - the type of elements in this list

All Superinterfaces: Collection, Iterable

source: http://docs.oracle.com/javase/7/docs/api/java/util/List.html

more information:

http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html

jvecsei
  • 1,936
  • 2
  • 17
  • 24
0

explained below

List<String> list = new ArrayList<String>(); // to call constructor for creating Arraylist of type String
List<String> removeList = new ArrayList<String>(); // same as above


private void removeColors(Collection<String> collection1,Collection<String> collection2)
{
    Iterator<String> iterator = collection1.iterator();//to get typesafe iterator of underlying collection
    while(iterator.hasNext())//to check if there is another element in collection
        if(collection2.contains(iterator.next()))
                      //interator.next() - to get next String from collection
                      //collection2.contains(..) - to check if String already presents in another collection
        iterator.remove();//remove element from iteration and move to next element
}
Adi
  • 2,364
  • 1
  • 17
  • 23