Java Collections

in #utopian-io8 years ago (edited)

What Will We Learn from this tutorial ?

In this Tutorial , We are learning the basic knowledge of Java Collections.

Collections: Collections represent a single unit of object. We perform on data such as searching ,sorting, Insert,update,delete can be performed by Java Collections.

  • We will learn from this tutorial about Java Linked List and Array List.
  • Also learn Java Map Interface and Java Hash Set.
  • Java Tree Map and Sorting Collections .

Requirements of Java Collections

Write here a bullet list of the requirements for the user in order to follow this tutorial.

  • Basic knowledge of Java like class, Object,data type
  • Basic Concepts of Object Oriented Programming (OOP).
  • Array, Link List, Queue

Difficulty

There is no difficulty in this tutorial . It is primary level contents and easy.

  • Basic

Tutorial Contents

Collections in java is a framework that provides an architecture to store and manipulate the group of objects. It Provides many Interfaces and classes. The Collections framework was designed for High performance. We mainly focus the basic concepts of collections .

Java Array List : Java Array List class contain duplicate elements . It uses a dynamic array for storing the elements. It is maintain Insertion Order. It allows random access. There is an array list ( ) method. It is used to build an empty array list.

We can see an example of Java array list to the given below :

import java.util.*;  
class arraylist{  
 public static void main(String args[]){  
  ArrayList<String> list=new ArrayList<String>();//Creating arraylist  
//Adding object in arraylist  
  list.add("wahid");
  list.add("mursalat");  
  list.add("tanvir");  
  list.add("safat");  
  
  Iterator itr=list.iterator();  
  while(itr.hasNext()){  
   System.out.println(itr.next());  
  }  
 }  
}  

OutPut : After running the following program, we get the output -
1.PNG

Java LinkedList : Java LinkedList class uses doubly linked list to store the elements. It provides a linked-list data structure. java linkedlist class is non synchronized. It is also contains duplicate elements. It can be used as list, stack ,queue .

Let's show an example of Java Linkedlist-

import java.util.*;  
public class linkedlist{  
 public static void main(String args[]){  
  
  LinkedList<String> al=new LinkedList<String>();  
  al.add("wahid");  
  al.add("mursalat");  
  al.add("tanvir");  
  al.add("safat");  
  
  Iterator<String> itr=al.iterator();  
  while(itr.hasNext()){  
   System.out.println(itr.next());  
  }  
 }  
}  

Output : After running the program , we will get the following Output :
2.PNG

Java List Interface : Java List interface is the sub Interface of collection.It contains methods to insert and delete elements in index basis.

Here we can see an example of the Java List Interface-
import java.util.*;  
public class listinterface{  
public static void main(String args[]){  
ArrayList<String> al=new ArrayList<String>();  
al.add("wahid");  
al.add("mursalat");  
al.add("mominul");  
al.add(1,"salim");  
System.out.println("Element at 3rd position: "+al.get(3));  
for(String s:al){  
 System.out.println(s);  
}  
}  
}  

After running this program , we can see that Output:
3.PNG

HashSet : Java HashSet class is used to create a collection that uses a hash table for storage.

Here , We can see an example of the Hashset :

import java.util.*;  
class hashset{  
 public static void main(String args[]){  
  //Creating HashSet and adding elements  
  HashSet<String> set=new HashSet<String>();  
  set.add("kamal ");  
  set.add("salim");  
  set.add("reza");  
  set.add("jafor");  
  //Traversing elements  
  Iterator<String> itr=set.iterator();  
  while(itr.hasNext()){  
   System.out.println(itr.next());  
  }  
 }  
}  

Output :
4.PNG

Java TreeSet: Java TreeSet class implements the Set interface that uses a tree for storage. It inherits AbstractSet class and implements NavigableSet interface. It contains Unique elements only like Hashset.

Here we can see an example of Java TreeSet -

import java.util.*;  
class treelist{  
 public static void main(String args[]){  
  //Creating and adding elements  
  TreeSet<String> al=new TreeSet<String>();  
  al.add("anwar");  
  al.add("jamal");  
  al.add("rafi");  
  al.add("mostak");  
  //Traversing elements  
  Iterator<String> itr=al.iterator();  
  while(itr.hasNext()){  
   System.out.println(itr.next());  
  }  
 }  
}  

Output : After running the program , we see the following Output :
5.PNG

Java Map Interface : A map contains values on the basis of key and value pair. Each key and value pair is known as an entry. Map contains only unique keys. Map Interface normally uses in search , Insert,Update and delete.

Let's see an example of Java Map Interface-

import java.util.*;  
class mapinterface{  
 public static void main(String args[]){  
  Map<Integer,String> map=new HashMap<Integer,String>();  
  map.put(1,"kamal");  
  map.put(2,"rahul");  
  map.put(3,"rafi");  
  for(Map.Entry m:map.entrySet()){  
   System.out.println(m.getKey()+" "+m.getValue());  
  }  
 }  
}  

Output : After the running proram ,we can see the following Output :
6.PNG

Java Queue Interface : Java Queue interface orders the element in FIFO. In FIFO, first element is removed first and last element is removed at last.

Example :

import java.util.*;  
class queue{  
public static void main(String args[]){  
PriorityQueue<String> queue=new PriorityQueue<String>();  
queue.add("wahid");  
queue.add("kamal");  
queue.add("raafi");  
queue.add("joy");  
queue.add("rahul");  
System.out.println("head:"+queue.element());  
System.out.println("head:"+queue.peek());  
System.out.println("iterating the queue elements:");  
Iterator itr=queue.iterator();  
while(itr.hasNext()){  
System.out.println(itr.next());  
}  
queue.remove();  
queue.poll();  
System.out.println("after removing two elements:");  
Iterator<String> itr2=queue.iterator();  
while(itr2.hasNext()){  
System.out.println(itr2.next());  
}  
}  
}  

OutPut :
7.PNG

Sorting in Collections :

Collections class provides static methods for sorting the elements of collection.If collection elements are of Set type, we can use TreeSet. But We cannot sort the elements of List.
Let's see an example of sorting in collections :

import java.util.*;  
class sorting{  
public static void main(String args[]){  
  
ArrayList<String> al=new ArrayList<String>();  
al.add("wahid");  
al.add("kamal");  
al.add("sharif");  
al.add("arshad");  
  
Collections.sort(al);  
Iterator itr=al.iterator();  
while(itr.hasNext()){  
System.out.println(itr.next());  
 }  
}  
}  

Output :
8.PNG



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @wahidurrahman I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • This is your first accepted contribution here in Utopian. Welcome!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x