Set is a collection (collection) that contains the data, where no data duplication. relationship diagram for the class object set can be seen in the previous image. To use this object, we have to declare the package:
Import java.util. *;Initialize the object set with the following declaration:
Set set = new HashSet ();
How to insert data into the object set as follows:
Set.add ("element 1");
Set.add ("element 2");
Set.add ("third element");
On top kit amenambahkan string object.
To access his data, we adapat to loop and retrieve data. we can use the Iterator object. The following illustration programya code:
Iterator iterator = set.iterator ();
While (iterator.hastnext ()); {
String element = iterator.next ();
System.out.println (element);
}
If we run the code above, then the result will be like in the picture below:

If we add the same data on the object set, then the data will not be stored on the object set. Her illustrations as follows:
Set.add ("element 1");Set.add ("element 2");
Set.add ("third element");
// test dupliaksi
Set.add ("element 1");
Set.add ("element 2");
If we run the above program, we will get the result as shown in the picture below.
If we want to remove the object from the collection, we can make use of the method remove () with the parameter object content to be deleted. suppose we remove the object for which data = "element 2" then I remove it as follows:
Boolean removed = set.remove ("element 1");
System.out.println (removed);
If we execute the above program we will memeproleh results as shown in the figure below:

SET IN JAVA
2. Map
Map is an object such as a list, but we need a key to enter the data. folder object class diagram can be seen in the image below. to use this object, we have to declare the package.Import java.util. *;
Object Instansisasi folder with the following declaration:
MAPA map = new HashMap ();

MAP in JAVA
How to insert data into a folder object as follows:mapA.put ("key1", "element 1");
mapA.put ("key2", "element 2");
mapA.put ("key3", "third element");
above we added a string object and its key. whereas to access his data we can do looping and retrieve data. There are two ways to access the data of the object folder. The following illustration of the program:
// How 1
System.out.println ("1 === ==== way");
String element1 = (String) mapA.get ("key1");
System.out.println ("element 1:" + element1);
// How 2
System.out.println ("2 == === way");
Iterator iterator = mapA.keyset (). Iterator ();
While (iterator.hasnext ()); {
Object key = iterator.next ():
Object value = mapA.get (key);
System.out.println ("key:" + key);
System.out.println ("value:" + value);
System.out.println ("==========");
}
Save this program code and run the program code that will get the results as seen in the image below. Kalu we want to remove the object from the collection, we will use the method remove () with the key parameters of the object. Suppose we remove objects from the collection of the key = "key1".
Objects removed = mapA.remove ("Key 1");
System.out.println ("delete data on key 1, data =" + removed);
Run the above program code, we'll get the results as shown in the figure below:

MAP in JAVA
3. QueueQueue is a special collection of objects which approach is FIFO, First In First Out. the reader can see the class diagram for the Queue object in the image below. how do I use it? to use this object we have to declare the following package:
Import java.util. *;
Object instantiation queue as follows:
Queue queueA = new LinkedList ();
How to put it in the queue object as follows: \
queueA.add ("element 1");
queueA.add ("element 2");
queueA.add ("third element");
the above program code, we add to the string object in the object queue.

QUEUE in Java
To access its data we can do in two ways:// How 1
Objects firstElment = queueA.element ();
System.out.println ("Element 1:" + firtElemenet);
// How 2
Iterator iterator = queueA.iterator ();
While (iterator.hasnext ()) {
String element = (String) iterator.next ();
System.out.println ("element:" + element);
}
If you run the above program code, kits aakan get:

image queue
To delete the data, we can call the method remove (). here will erase the first data. examples of program code as follows:Objects removeElement = queueA.remove ();
System.out.println ("Element deleted:" + removeElement);
The results of execution are:

// Take the head queue
Objects head = queueA.peek ();
System.out.println ("Element Head:" + Head);
// take the head queue and delete
Objects head2 = queueA.pool ();
System.out.println ("head element:" + head2);
previous material is Collection in java programming
next material about STACK in JAVA
version in Indonesian set, map dan queue dalam pemrograman java
No comments:
Post a Comment