Looking for useful code examples to build your Java applications?
The Java Collections Framework is a collection of interfaces and classes which provides an efficient data processing. This framework has numerous useful classes which have very useful functions which makes a programming task incredibly easy.
1 - ArrayLists & Comparison using hasNext() Method
/**
* Author : Berk Soysal
* UsefulCodes.java
*/
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
public class UsefulCodes {
// ArrayLists
public static List<String> listeler(int a){
String[] words2={"eggs","lazers","hats","pie"};
String[] words3={"lazers","hats"};
List<String> list = new ArrayList<>();
List<String> list2 = new ArrayList<>();
//add array items to the list
for(String x: words2)
list.add(x);
for(String y: words3)
list2.add(y);
if(a==1)return list;
else return list2;
}
//editList
public static void editList(Collection<String> l1, Collection<String> l2){
Iterator<String> it = l1.iterator();
while(it.hasNext()){
if(l2.contains(it.next()))
it.remove();
}
System.out.println(l1.toString());
}
// Main Method
public static void main(String[] args) {
// Compares 2 lists and excludes the identical elements //from the first list
editList(listeler(1),listeler(2));
}
}
2 - LinkedLists Editing and Reversing
/**
* Author : Berk Soysal
* UsefulCodes.java
*/
import java.util.LinkedList;
import java.util.List;
public class UsefulCodes {
// LinkedLists
public static void linkedlisteler(){
String[] words={"funk","chunk","furry","terminator"};
String[] words2={"trunk","goats","harry","potter"};
List<String> list= new LinkedList<String>();
List<String> list2= new LinkedList<String>();
for(String x : words)
list.add(x);
for(String y : words2)
list2.add(y);
list.addAll(list2);
list2=null; // clear the allocated memory for list2
//Print the list
System.out.printf(" Original List: ");
for(String z: list)
System.out.printf("%s ",z);
//Remove first two elements from the list
list.remove(0);
//Since this is a linked list it shifts to the left
list.remove(0);
//Print the edited list
System.out.printf("\n Edited List: ");
for(String z: list)
System.out.printf("%s ",z);
//Reverse the list
System.out.printf("\n Reversed List: ");
for(int i=list.size()-1;i>=0;i--)
System.out.printf( list.get(i)+ " ");
}
// Main Method
public static void main(String[] args) {
linkedlisteler();
}
}
3 - Array-to-List & List-to-Array Conversions
/**
* Author : Berk Soysal
* UsefulCodes.java
*/
import java.util.Arrays;
import java.util.LinkedList;
public class UsefulCodes {
// Array-to-List & List-to-Array Conversions
public static void converter(){
String[] stuff = {"cookies", "watermelon","olive","zucchini"};
LinkedList<String> thelist = new LinkedList<String>(Arrays.asList(stuff));
thelist.add("LAST");
thelist.addFirst("FIRST");
// convert back to an array
stuff= thelist.toArray(new String[thelist.size()]);
for( String z: stuff)
System.out.printf(" %s",z);
}
// Main Method
public static void main(String[] args) {
converter();
}
}
4 - Collections and addAll() Method
/**
* Author : Berk Soysal
* UsefulCodes.java
*/
import java.util.ArrayList;
import java.util.Collections;
public class UsefulCodes {
//Collections and addAll() Method
public static void collection(){
String[] stuff = {"chocolate", "pizza","bread","rice"};
ArrayList<String> list = new ArrayList<String>();
list.add("youtube");
list.add("google");
list.add("outlook");
System.out.printf("Collection = ");
for(String z : list)
System.out.printf("%s ",z);
// Add all elements from stuff to List2
Collections.addAll(list,stuff);
System.out.printf("\nNew Collection = ");
for(String z : list)
System.out.printf("%s ",z);
}
// Main Method
public static void main(String[] args) {
collection();
}
}
5 - Stacks - Push & Pop Operations
/**
* Author : Berk Soysal
* UsefulCodes.java
*/
import java.util.Stack;
public class UsefulCodes {
// STACKS, PUSH, POP
public static void stacks(){
Stack<String> stack = new Stack<String>();
stack.push("Bottom");
printStack(stack);
stack.push("Second");
printStack(stack);
stack.push("Third");
printStack(stack);
//The last one goes in, goes out first when "pop" is used
stack.pop();
printStack(stack);
}
private static void printStack(Stack<String> s){
if(s.isEmpty()) System.out.println("Empty Stack !");
else System.out.printf("%s TOP of the Stack \n",s);
}
// Main Method
public static void main(String[] args) {
stacks();
}
}
6 - Priority Queues
/**
* Author : Berk Soysal
* UsefulCodes.java
*/
import java.util.PriorityQueue;
public class UsefulCodes {
// Queues
private static void queues(){
PriorityQueue<String> q = new PriorityQueue<String>();
q.offer("First");
q.offer("Second");
q.offer("Third");
System.out.println(q);
// Remove the highest priority element
q.poll();
System.out.println(q);
}
// Main Method
public static void main(String[] args) {
queues();
}
}
7 - HashSets
/**
* Author : Berk Soysal
* UsefulCodes.java
*/
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class UsefulCodes {
//Sets
private static void hashSet(){
String[] stuff = {"chocolate", "pizza","bread","pizza","rice","rice","bread","chocolate"};
List<String> list = Arrays.asList(stuff);
System.out.println(list);
Set<String> set = new HashSet<>(list);
System.out.println(set);
// Sets have unique elements only !
}
// Main Method
public static void main(String[] args) {
hashSet();
}
}
Continue Reading Useful Java Code Snippets 2 - Generic Methods
Disqus Comments Loading..