Sunday 13, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Sunday, 31 May 2015

How to add value to all types of Collections

Following example shows how to add values in all types collections in Java.

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

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • How to add value to all types of Collections Following example shows how to add values in all types collections in Java. Sample Program AddValueToCollection.java package com.javatutorialcorner.collection; import java.util.ArrayList; import java.util.Collection; impo… Read More
  • How to reverse the Collection (List) Following example shows how to reverse the collection (List) using Collections.reverse(arg0); method in Java. void java.util.Collections.reverse(List<?> list)reversepublic static void reverse(List<?> list) Rever… Read More
  • How to rotate element of List Following example shows how to rotate the element of List using Collections.rotate(list, distance); method in Java. void java.util.Collections.rotate(List<?> list, int distance) rotate public static void rotate(List&l… Read More
  • How to get the size of Collection Following example shows how to get the size of collection using collection.size() method in Java. int java.util.List.size()sizeint size() Returns the number of elements in this list. If this list contains more than Integer.… Read More
  • How to convert Array into List Following example shows how to convert Array into List using Arrays.asList(nameArray) method in Java. <String> List<String> java.util.Arrays.asList(String... a) @SafeVarargs asList @SafeVarargs public static &… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: How to add value to all types of Collections Rating: 5 Reviewed By: eHowToNow