aspose file tools
The moose likes Beginning Java and the fly likes Collections vs Collections Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Collections vs Collections" Watch "Collections vs Collections" New topic
Author

Collections vs Collections

William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 965

So I am confused.
There is a collections class in the main api and a new seperate collections api.
What are the different "terms" for these? One is in the main api, how do you say that. One is an add on library, how do say that?
If I install the new collections api will all my existing collections code still work?
Thanks.


Please ignore post, I have no idea what I am talking about.
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Christopher,
The java.util.Collection is the parent interface of the collection API.
While the java.util.Collections class is a helper class (mostly static methods) to work with Collections.
Your collections (mainly Vectors) will continue to work after updating to Java 2 collections API.
Regards,
Manfred.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 965

So the parent interface java.util.Collection is part of the "standard" api.
And the helper class java.util.Collections is an "add on". By "add on" I mean you have to download and install another library. Is that correct?
So when I go to the sun site to look at the api, I select "java.util", than I select "Collections". Now is this the "standard" api only? Where do I find the "add on" api?
This is what I am talking about when I say add on
Thanks.


[This message has been edited by christopher foran (edited November 30, 2001).]
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1506
java.util.Collections is part of the standard java 2 API. IT could have been refered to as an add on maybe because it is new in java 1.2 and above.
------------------
Bosun
SCJP for the Java� 2 Platform


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 965

Using an example from this article (shown below) it was not working and a coworker told me I had to download an add on library to use collections.
But when I changed the include from "java.util.collections.*" to "java.util.*" it started working.
Can anyone clear that up for me? Thanks.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Sun did not create a separate directory in their jar structure dedicated to collections. They just mixed them in with the rest of the utils.
You can check out where they live in the API documentation. Just click on Collections or any class that implements Collection and look at the heirarchy.

[This message has been edited by Cindy Glass (edited November 30, 2001).]


"JavaRanch, where the deer and the Certified play" - David O'Meara
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9033
    
  10
Originally posted by christopher foran:
... a coworker told me I had to download an add on library to use collections.

But when I changed the include from "java.util.collections.*" to "java.util.*" it started working.

Can anyone clear that up for me? Thanks.

<pre>
import java.util.* ;
// The java.util package includes (since the 1.2 release) classes that
// implementjava.util.Collection interface


// import java.util.collections.* ;
// There is no package named java.util.collections in the current jdk.
// The code in the article is in error, and your friend is in error.


public class CollectionTest
{

public static void main( String[] args)
{
System.out.println("Collection Test") ;

HashSet collection = new HashSet() ;
// HashSet implements Collection and therefore is a Collection

String dog1 = "Max" ;
String dog2 = "Bailey" ;
String dog2 = "Harriet" ;

collection.add( dog1) ;
// add() is a method in the Collection interface and is
implemented in the HashSet class


collection.add( dog2) ;
collection.add( dog3) ;

System.out.println("Collection created, size = "
+ collection.size() + ", isEmpty = "
// size() is a method in the Collection interface and is
// implemented in the HashSet class


+ collection.isEmpty()) ;
// isEmpty() is a method in the Collection interface and is
// implemented in the HashSet class


System.out.println("Collection contains " + dog3
+ ":" + collection.contains( dog3)) ;
// contains() is a method in the Collection interface and is
// implemented in the HashSet class


System.out.println("Collection iteration (unsorted):") ;
Iterator itor = collection.iterator() ;
// iterator() is a method in the Collection interface and is
// implemented in the HashSet class


while( itor.hasNext())
{
System.out.println(" " + itor.next()) ;
}

collection.remove( dog1) ;
// Likewise
collection.clear() ;
// Likewise
}
}
</pre>


[This message has been edited by Marilyn deQueiroz (edited November 30, 2001).]


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 965


There is no package named java.util.collections in the current jdk. The code in the article is in error, and your friend is in error.

Ya, I finally figured that out.
With

every thing compiled except for the Iterator. Which is one of the reasons I started down the wrong path. My co-worker and the author of that article being the other two reasons.
Thanks for the help (as always).
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Collections vs Collections
 
Similar Threads
OOP-3 might be the end of me...
Help with Array Lists and Loops!
A forum for JAVA Collections
diff between Arraylist and vector
Question on collections framework in java