Following example shows how to add values in all types collections in Java.
Sample Program
AddValueToCollection.java
Result
The above code will produce the following output.
Sample Program
AddValueToCollection.java
package com.javatutorialcorner.collection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
public class AddValueToCollection {
public static void main(String[] args) {
System.out.println("********** LinkedList **********");
List lnkLst = new LinkedList();
lnkLst.add("Java");
lnkLst.add("Spring");
lnkLst.add("Spring MVC");
lnkLst.add("Spring Security");
printCollection(lnkLst);
System.out.println("********** Array List **********");
List aryLst = new ArrayList();
aryLst.add("Spring Social");
aryLst.add("Spring Integration");
aryLst.add("Spring Batch");
aryLst.add("Hibernate");
printCollection(aryLst);
System.out.println("********** HashSet **********");
Set hashSet = new HashSet();
hashSet.add("RESTFul Webservice");
hashSet.add("SOAP Webservice");
hashSet.add("JSON");
hashSet.add("XML Parsing");
printCollection(hashSet);
System.out.println("********** TreeSet **********");
SortedSet treeSet = new TreeSet();
treeSet.add("Log4j");
treeSet.add("Java 7");
treeSet.add("Java Mail API");
treeSet.add("Jav Best practices");
printCollection(treeSet);
System.out.println("********** LinkedHashSet **********");
LinkedHashSet lnkHashset = new LinkedHashSet();
lnkHashset.add("AngularJS");
lnkHashset.add("JUnit");
lnkHashset.add("EasyMock");
lnkHashset.add("Big Data");
printCollection(lnkHashset);
System.out.println("********** HashMap **********");
Map map1 = new HashMap();
map1.put("xml1", "DOM Parser");
map1.put("xml2", "SAX Parser");
map1.put("xml3", "JDOM");
map1.put("xml4", "DOM4J");
map1.put("xml5", "XPATH");
map1.put("xml6", "JAXB");
printCollection(map1.keySet());
printCollection(map1.values());
System.out.println("********** TreeMap **********");
SortedMap map2 = new TreeMap();
map2.put("JSON1", "JSONSimple");
map2.put("JSON2", "Jackson");
map2.put("JSON3", "GSON");
printCollection(map2.keySet());
printCollection(map2.values());
System.out.println("********** LinkedHashMap **********");
LinkedHashMap map3 = new LinkedHashMap();
map3.put("db1", "MySQL");
map3.put("db2", "PostgreSQL");
map3.put("db3", "Oracle");
map3.put("db4", "MS SQL Server");
printCollection(map3.keySet());
printCollection(map3.values());
}
private static void printCollection(Collection collection) {
Iterator itr = collection.iterator();
while (itr.hasNext()) {
String str = (String) itr.next();
System.out.print(str + " ");
}
System.out.println();
}
}
Result
The above code will produce the following output.
********** LinkedList **********
Java Spring Spring MVC Spring Security
********** Array List **********
Spring Social Spring Integration Spring Batch Hibernate
********** HashSet **********
JSON SOAP Webservice RESTFul Webservice XML Parsing
********** TreeSet **********
Jav Best practices Java 7 Java Mail API Log4j
********** LinkedHashSet **********
AngularJS JUnit EasyMock Big Data
********** HashMap **********
xml4 xml3 xml6 xml5 xml2 xml1
DOM4J JDOM JAXB XPATH SAX Parser DOM Parser
********** TreeMap **********
JSON1 JSON2 JSON3
JSONSimple Jackson GSON
********** LinkedHashMap **********
db1 db2 db3 db4
MySQL PostgreSQL Oracle MS SQL Server




0 comments:
Post a Comment