Adding Elements to a Set

From Java Example Source Code

Jump to: navigation, search

Contents

[edit] Overview - Adding Elements to a Set

This Java example program shows how to add elements to a Set.

[edit] Java Source Code

  • Package: zukowski.john
  • File: Iterate.java
package zukowski.john;
 
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
 
public class Iterate {
    public static void main(String args[]) {
	String elements[] = { "Irish Setter", "Poodle", "English Setter", "Gordon Setter", "Pug" };
	Set set = new HashSet(Arrays.asList(elements));
	Iterator iter = set.iterator();
	while (iter.hasNext()) {
	    System.out.println(iter.next());
	}
    }
}

[edit] What Result You Can Get

Run the program, you will get:

Gordon Setter
Irish Setter
English Setter
Poodle
Pug

[edit] Required External Libraries and/or Files for this Java Example

Need nothing.


[edit] How to Run this Java Example Program

We recommend running this Java example program with Eclipse.

For assistance in working with Eclipse, please see How to Run Java Program with Eclipse.

It's fairly easy.



[edit] Question & Answer

Any question?

Click edit and post your question or answer here.


Personal tools