+92 332 4229 857 99ProjectIdeas@Gmail.com

Collections In Java


ArrayList:


ArrayList aList = new ArrayList();
aList.add("Hello, ");
aList.add("This is");
aList.add("ArrayList");
aList.add(1, "Guys");//Adding data at specified index
             

  //Displaying arrayList contents
    System.out.print(aList); //It will display all contents without loop
 
 //Displaying arrayList contents/data on console through for loop
    for (int index = 0; index < aList.size(); index++) {
           System.out.print(aList.get(index) + " ");
    }
             
 //Displaying arrayList contents through foreach loop
    for (Object object : aList) {
           System.out.print(object + " ");
    }
              
 //Displaying arrayList contents using "Iterator" class object
    Iterator it = aList.iterator();
        while(it.hasNext()) {
           System.out.print(it.next() + " ");
        }

 //Displaying size of arrayList
    System.out.println(aList.size());
             
 //Getting content/data at a specified index in arratList
    String str = (String)aList.get(2);//getting data of index 2
    System.out.println(str); //it displays ArrayList
             
 //Removing content of arrayList at specified index
    aList.remove(2);        
                           
 //Removing content of arrayList by specifying object
    aList.remove("This is");
                       
  //For specifying the arrayList to hold Integer data in it
    ArrayList<Integer> integerList = new ArrayList<Integer>();
    integerList.add(1);
    integerList.add(2);
    integerList.add(3);
    integerList.add(4);
             
  //Displaying integerList contents on console through for loop
    for (int i = 0; i < integerList.size(); i++) {
         System.out.print(integerList.get(i) + " ");
    }
             
  //For specifying the arrayList to hold String data in it
    ArrayList<String> stringList = new ArrayList<String>();


  //Adding arrayList contents and showing their sum
    ArrayList intArrayList = new ArrayList();
    intArrayList.add("5");
    intArrayList.add("4");
    intArrayList.add("2");
    intArrayList.add("3");
    intArrayList.add("1");
      
  //way one to sum up contents
    int sum = 0;
      for (int i = 0; i < intArrayList.size(); i++) {
             String str = (String)intArrayList.get(i);
             sum += Integer.parseInt(str); //casting string to integer
      }
      System.out.print("The sum of arrayList contents is : " + sum);
             
  //way second to sum up contents through Object array
    int sum2 = 0;
     Object[] objArray = intArrayList.toArray();//arrayList elements to object array
       for (int i = 0; i < objArray.length; i++) {
             sum2 += Integer.parseInt(objArray[i].toString());
       }
      System.out.print("The sum of arrayList contents is : " + sum2);


 //Sorting a arrayList
      Collections.sort(intArrayList);
           
HashMap:

       HashMap hm = new HashMap();
       hm.put("1", "Rehan");
       hm.put("2", "Maryam");
       hm.put("3", "Zainab");
       hm.put("4", "Ayaan");
       hm.put("5", "Cruise");
             
       //Dispalying HashMap contents
             
        System.out.println(hm);
             
         //Or
             
        System.out.print(hm.values());
      
      //Displaying Through Loop/Iterator
        Set set = hm.entrySet();
        Iterator it = set.iterator();
           while(it.hasNext()) {
                Map.Entry me = (Map.Entry)it.next();
                System.out.println(me.getKey() + " = " + me.getValue());
           }
             
      //Sorting values in HashMap Way One
        SortedSet sortedSetKey = new TreeSet(hm.keySet());
        Iterator iKey = sortedSetKey.iterator();
           while (iKey.hasNext()) {
                String key = iKey.next().toString();
                String value = hm.get(key).toString();
                System.out.println(key + " = " + value);      
           }
             
     //Sorting a HashMap Way Two
        Vector v = new Vector(hm.keySet());
        Collections.sort(v);
          for(Enumeration e = v.elements(); e.hasMoreElements();) {
                String key = e.nextElement().toString();
                String value = hm.get(key).toString();
                System.out.println(key + " = " + value);
          }
             
    //Removing Content From HashMap By Specifying Key
        hm.remove("5");
        System.out.print(hm);//it removes the value of "5 = Cruise" in HashMap
             
   //Specifying the HashMap to hold key as Integer and value as String in it
        HashMap<Integer, String> hash = new HashMap<Integer, String>();
        hash.put(1, "Its Me");
        hash.put(2, "Its You");
        hash.put(5, "Who are you?");
        hash.put(3, "Are you ok?");
        System.out.print(hash);

Stack:  
        Stack st = new Stack();
        st.push("This");
        st.push("is");
        st.push("stack");
             
        //Displaying Stack Contents
          for(int i = 0 ; i < st.size(); i++) {
              System.out.print(st.get(i) + " "); //it displays "This is stack"
          }
             
        //Displaying Stack Contents
          while(!st.empty()) {
              System.out.print(st.pop() + " "); //it displays "stack is This"
          }

LinkedList:

         LinkedList ll = new LinkedList();
         ll.add("Proud");
         ll.add("To");
         ll.add("Be");
         ll.add("A Pakistani");
             
        //Displaying All LinkedList Nodes
         System.out.println(ll);
             
        //Adding Collection in LinkedList
         ArrayList al = new ArrayList();
         al.add("Pakistan");
         al.add("is");
         al.add("a islamic");
         al.add("republic");
         al.add("state");
             
        LinkedList ll2 = new LinkedList(al); //adding ArrayList in LinkedList

        //Displaying All LinkedList Nodes
         System.out.println(ll2);


  

0 comments: