JAVA List LinkedList HashSet HashMap TreeSet TreeMap Queue

 

import java.util.ArrayList


List<Integer> arrList = new ArrayList<Integer>(10);

arrList.add(1);

arrList.add(2);

arrList.add(3);

arrList.add(4);

arrList.add(5);

arrList.add(6);

System.out.println(arrList);  1,2,3,4,5,6

arrList.remove(3);  remove 3rd element that is 4 


LinkedList

LinkedList<String> linkist = new LinkedList<String>();

linkist .add("A");
linkist .addLast("C");
linkist .addFirst("D");
linkist .add(2,"B");

// D A B C

linkist .remove("C");
linkist .add(2); //by position 
linkist .removeFirst("A");
linkist .removeLast("A");


Hashset

Set<String> hashset = new HashSet<>();

boolean ret = bhashset.add("A"); //true if added false if not added
hashset.add("B");
hashset.add("B"); ///return false here

hashset.contains("A");

hashset.remove("A");


HashMap

Map<String, Integer> hmap = new HashMap<>();

hmap.put("a",10);
hmap.put("b",20);
hmap.put("c",30);

// { a=10, b=20, c=30}
hmap.size();  // 3 

hmap.containsKey("a"); //boolean 

hmap.get("a");

hmap.keySet();  // array of keys

for(String k: hmap.keySet()){
   // hmap.get(k);
}

for(Entry <String, Integer> data : hmap.entrySet()){
   // data.getKey();  

   // data.getValue();
}


TreeSet

Naturally sorted in Treeset

TreeSet<String> tset = new TreeSet<String>();

tset.add("B");
tset.add("A");
tset.add("C");
tset.add("C");

// A, B, C   // sort //ordered by default


TreeMap

Naturally sorted in Treeap

Map< Integer, String> tmap= new HashMap<>();

tmap.put(1,"a");
mtap.put(2,"b");
tmap.put(3,"c");

// {1=a,2=b,3=c}

Map< String, Integer> tmap= new HashMap<>();

tmap.put("b",2);
mtap.put("a",1);
tmap.put("c",3);

// {a=1,b=2,c=3}


Stacks

Stack <String> stack = new Stack<>();

stack.push("A");
stack.push("B");
stack.push("C");

String  poped = stack.pop(); // C  remove from stack

String  peeked = stack.peek(); // C , doesnt remove from stack

//Stack is filo


Queue

// fifo arrangement

Queue<> que = new PriorityQueue<>();

que .push("A");
que .push("B");
que .push("C");


que.remove(); // 

que.peek(); // 

que.poll(); // 





Share this

Related Posts

Previous
Next Post »