0% found this document useful (0 votes)
3 views9 pages

Collection 1

The document provides an overview of various types of collections in programming, including lists, stacks, queues, sets, and maps. It details specific implementations such as ArrayList, LinkedList, Stack, Queue, HashSet, LinkedHashSet, TreeSet, HashMap, LinkedHashMap, and TreeMap, along with example code snippets. Each collection type is described with its characteristics and syntax for creating instances.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Collection 1

The document provides an overview of various types of collections in programming, including lists, stacks, queues, sets, and maps. It details specific implementations such as ArrayList, LinkedList, Stack, Queue, HashSet, LinkedHashSet, TreeSet, HashMap, LinkedHashMap, and TreeMap, along with example code snippets. Each collection type is described with its characteristics and syntax for creating instances.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

Collections

-----------------------
Collections is an object that store a group of an object.

types:
-------------
1)list
2)stack
3)Queue
4)set
5)map

I.list
--------
--->collection of value with in square brackets

types:
------------
1)ArrayList
2)LinkedList

1)ArrayList
---------------------
Array--->collection of same (oe)similar Data
----------------
--->index value
---->fixed data

ArrayList
--------------------
--->collection of different data
--->no fixed value
--->No index value

ArrayList
-----------------
syntax:
------------
ArrayList<> var=new ArrayList<>();

EX:
-----
package Pack1;

import java.util.ArrayList;

public class One


{

public static void main(String[] args)


{

ArrayList a1=new ArrayList<>();

System.out.println(a1);
System.out.println(a1.size());
a1.add("BESANT TECHNOLOGIES");

a1.add(100);

a1.add(23);

a1.add(true);

a1.add(2.3f);

System.out.println(a1);

System.out.println(a1.size());

}
}
-----------------------------------------------------------
LinkedList
--------------------
package Pack1;

import java.util.ArrayList;

public class One


{

public static void main(String[] args)


{

ArrayList<Integer> a1=new ArrayList<>();

System.out.println(a1);

System.out.println(a1.size());

a1.add(55);
a1.add(23);
a1.add(30);
a1.add(22);

System.out.println(a1);

System.out.println(a1.size());

a1.add(2324);

System.out.println(a1);

}
}
--------------------------------------------------------------------
LinkedList
--------------------
Ex:
------
package Pack1;
import java.util.*;

public class One


{

public static void main(String[] args)


{

LinkedList a1=new LinkedList<>();

System.out.println(a1);

System.out.println(a1.size());

a1.add("BESANT TECHNOLOGIES");

a1.add(100);

a1.add(23);

a1.add(true);

a1.add(2.3f);

System.out.println(a1);

System.out.println(a1.size());

a1.add(2324);

System.out.println(a1);

a1.addFirst(55);

System.out.println(a1);

}
}
-----------------------------------------------------------------------------
Stack
-----------
--->FILO --->First In Last Out

EX:
-----
package Pack1;

import java.util.*;

public class One


{

public static void main(String[] args)


{
Stack a1=new Stack();

a1.add("BESANT TECHNOLOGIES");

a1.add(100);

a1.add(23);

a1.add(true);

a1.add(2.3f);

System.out.println(a1);

System.out.println("Head value "+a1.peek());


System.out.println("Rmove value is "+a1.pop());
System.out.println("Head value "+a1.peek());

}
}

Queue
--------------

---->Queue is an Interface

package Pack1;

import java.util.*;

public class One


{

public static void main(String[] args)


{

Queue a1=new LinkedList();

a1.add("BESANT TECHNOLOGIES");

a1.add(100);

a1.add(23);

a1.add(true);

a1.add(2.3f);

System.out.println(a1);

System.out.println("Head value "+a1.peek());


System.out.println("Rmove value is "+a1.remove());
System.out.println("Head value "+a1.peek());
}
}
-----------------------------------------------------------------------------
set
---------
types:
-----------
1)HashSet
2)LinkedHashSet
3)TreeSet

I.HashSet
--------------------
--->unorder collection of values

syntax:
-------------
HashSet var=new HashSet();

EX:
-----
package Pack1;

import java.util.*;

public class One


{

public static void main(String[] args)


{

HashSet a1=new HashSet();

a1.add("BESANT TECHNOLOGIES");

a1.add(100);

a1.add(23);

a1.add(true);

a1.add(2.3f);

System.out.println(a1);

}
}
----------------------------------------------------------
LinkedHashSet
------------------------------
--->it display the data in order

syntax:
----------------
LinkedHashSet var=new LinkedHashSet();

EX:
----
package Pack1;

import java.util.*;

public class One


{

public static void main(String[] args)


{

LinkedHashSet a1=new LinkedHashSet();

a1.add("BESANT TECHNOLOGIES");

a1.add(100);

a1.add(23);

a1.add(true);

a1.add(2.3f);

System.out.println(a1);

}
}

----------------------------------------------------------------
TreeSet
--------------
--->It display the data in ascending order.

syntax:
-----------------
TreeSet var=new TreeSet();

EX:
-----
package Pack1;

import java.util.*;

public class One


{

public static void main(String[] args)


{

TreeSet a1=new TreeSet();


// a1.add("BESANT TECHNOLOGIES");

a1.add(100);

a1.add(23);

a1.add(1);

a1.add(2);

a1.add(300);

a1.add(340);

System.out.println(a1);

}
}
------------------------------------------------------------------------------
Map
---------
types:
----------
1)HashMap
2)LinkedHashMap
3)TreeMap

1)HashMap
================
package Pack1;

import java.util.*;

public class One


{

public static void main(String[] args)


{
HashMap<Integer,String> h1=new HashMap();

h1.put(101, "C");
h1.put(102,"C++");
h1.put(300,"Python");
h1.put(250, "Java");
h1.put(99,"Php");

System.out.println(h1);

}
}

2)LinkedHashMap
----------------------------
package Pack1;

import java.util.*;

public class One


{

public static void main(String[] args)


{
LinkedHashMap<Integer,String> h1=new LinkedHashMap();

h1.put(101, "C");
h1.put(102,"C++");
h1.put(300,"Python");
h1.put(250, "Java");
h1.put(99,"Php");

System.out.println(h1);

}
}
----------------------------------------
TreeMap
----------------
package Pack1;

import java.util.*;

public class One


{

public static void main(String[] args)


{
TreeMap<Integer,String> h1=new TreeMap();

h1.put(101, "C");
h1.put(102,"C++");
h1.put(300,"Python");
h1.put(250, "Java");
h1.put(99,"Php");

System.out.println(h1);

}
}

You might also like