Collection

                                          Set

import java.util.HashSet;
import java.util.Set;
public class FindDuplicate {
     public static void main(String[] args) {
         Set<String> s = new HashSet<String>();
         s.add("aman");
         s.add("raja");
         s.add("raja");
         s.add("kumar");
        
         for (String a : args)
             if (!s.add(a))
                 System.out.println("Duplicate detected: " + a);
                    System.out.println(s.size() + " distinct words: " + s);
     }
 }

/*
3 distinct words: [kumar, aman, raja]*/

--------------------------------------------------------------------------------------------------------------------------
import java.util.Collection;
import java.util.LinkedHashSet;
public class SetDuplicate {
  public static void main(String[] args) {
   Collection<Integer>noDups=new LinkedHashSet<Integer>();
   noDups.add(1);
   noDups.add(2);
   noDups.add(3);
   noDups.add(2);
   System.out.println(noDups);
   LinkedHashSet<Integer>lhs=new LinkedHashSet<Integer>();
   lhs.add(1);
   lhs.add(2);
   lhs.add(3);
   lhs.add(5);
   lhs.add(3);
   System.out.println(lhs);
       
 }
}
/*

[1, 2, 3]

[1, 2, 3, 5]
*/
--------------------------------------------------------------------------------------------------------------------------
 
public class SetExample {
    public static void main(String[] args) {
        // Set example with implement TreeSet
        Set<String> s=new TreeSet<String>();
        s.add("b");
        s.add("a");
        s.add("d");
        s.add("c");
       
        @SuppressWarnings("rawtypes")
  Iterator it=s.iterator();
        while(it.hasNext())
        {
          String value=(String)it.next();
          System.out.println("Value :"+value);
        }
    }
}
/*

Value :a
Value :b
Value :c
Value :d

*/
----------------------------------------------------------------------------------------------------------

                                           QUEUE

import java.io.*;
import java.util.*;
public class QueueImplement {
 LinkedList<Integer> list;
 String str;
 int num;
 public static void main(String[] args) {
  @SuppressWarnings("unused")
  QueueImplement q = new QueueImplement();
 }
 public QueueImplement() {
  try {
   list = new LinkedList<Integer>();
   InputStreamReader ir = new InputStreamReader(System.in);
   BufferedReader bf = new BufferedReader(ir);
   System.out.println("Enter number of elements : ");
   str = bf.readLine();
   if ((num = Integer.parseInt(str)) == 0) {
    System.out.println("You have entered either zero/null.");
    System.exit(0);
   } else {
    System.out.println("Enter elements : ");
    for (int i = 0; i < num; i++) {
     str = bf.readLine();
     int n = Integer.parseInt(str);
     list.add(n);
    }
   }
   System.out.println("First element :" + list.removeFirst());
   System.out.println("Last element :" + list.removeLast());
   System.out.println("Rest elements in the list :");
   while (!list.isEmpty()) {
    System.out.print(list.remove() + "\t");
   }
  } catch (IOException e) {
   System.out.println(e.getMessage() + " is not a legal entry.");
   System.exit(0);
  }
 }
}
/*
Enter number of elements :
12
Enter elements :
1
2
3
1
5
3
5
6
7
8
9
11
First element :1
Last element :11
Rest elements in the list :
2 3 1 5 3 5 6 7 8 9 
*/
------------------------------------------------------------------------------------------------------------

                                         HashMap

import java.util.HashMap;
public class HasMapDemo {
 public static void main(String[] args) {
  @SuppressWarnings("rawtypes")
  HashMap<Comparable, Comparable> map = new HashMap<Comparable, Comparable>();
  map.put("2.2", "3.2");
  map.put("key2", "value1");
  map.put("key5", "value2");
  map.put("key3", "value1");
  map.put(2.8, 5.5);
  System.out.println(map.size());
  System.out.println(map.containsKey("2.2"));
  System.out.println(map);
  System.out.println(map.getClass());
  System.out.println(map.values());
  // System.out.println(map.get(2.8));
 }
}
/*
5
true
{key3=value1, key5=value2, key2=value1, 2.8=5.5, 2.2=3.2}
class java.util.HashMap
[value1, value2, value1, 5.5, 3.2]
*/
-----------------------------------------------------------------------------------------------------------
import java.util.HashMap;
import java.util.Map;
public class MapDemo {
 public static void main(String[] args) {
  Map<String, String> mymap = new HashMap<String, String>();
  mymap.put("1", "one");
  mymap.put("1", "not one");
  mymap.put("1", "surely not one");
  // the following line is case 2 for duplicate
  // mymap.put("1","one");
  System.out.println(mymap.get("1"));
  
 }
}
/*
surely not one
*/

 

Comments

Popular posts from this blog