Skip to content

Instantly share code, notes, and snippets.

@samsoft00
Created August 6, 2016 05:55
Show Gist options
  • Save samsoft00/7effe797ae2a6da9b3fbaaeda0aae8d2 to your computer and use it in GitHub Desktop.
Save samsoft00/7effe797ae2a6da9b3fbaaeda0aae8d2 to your computer and use it in GitHub Desktop.
Java Collect Example
import java.util.ArrayList;
public class AList {
public static void main(String[] args){
ArrayList<Integer> grades = new ArrayList<Integer>();
grades.add(100);
grades.add(90);
grades.add(80);
grades.add(70);
grades.add(60);
grades.add(50);
int total = 0;
for(int i = 0; i < grades.size(); ++i){
total += grades.get(i);
}
/*
for(Integer grade : grades){
total += grade;
}
*/
double average = total /grades.size();
System.out.println("Size of grades: "+grades.size());
System.out.println("Average: "+ double);
grades.remove(4);
System.out.println("New Size:"+ grades.size());
grades.add(60);
grades.add(50);
System.out.println("Newer Size: "+ grades.size());
}
}
import java.util.HashSet;
public class Names{
public static void main(String[] args){
HashSet<String> names = new HashSet<String>();
name.add("David");
names.add("Bryan");
name.add("Cynthia");
System.out.println("The number of names is "+names.size());
for(String name : names){
System.out.println(name);
}
name.remove("David");
name.remove("Raymond");
String name = "David";
if(names.contains(name)){
System.out.println(name + "is in the collection.");
}else{
System.out.println(name +" is not in the collection");
}
for(String n : names){
System.out.println(n);
}
names.clear();
System.out.println("The number of names is +"names.size());
}
}
import java.util.TreeSet;
public class Alpha{
public static void main(String[] args){
names.add("Raymond");
names.add("Mike");
names.add("Bryan");
names.add("Jennifer");
names.add("Terrill");
System.out.println("Number of name = "+names.size());
for(String name : names){
System.out.println(name);
}
System.out.println("Name before Jannifer "+names.lower("Jennifer"));
System.out.println("Name after Jannifer "+names.higher("Jennifer"));
System.out.println("First element: "+names.first());
System.out.println("Last element: "+names.last());
}
}
//PriorityQueue is abstraction for basic queue line
import java.util.PriorityQueue;
public class Line{
public static void main(String[] args){
PriorityQueue<String> line = new PriorityQueue<String>{};
line.add("David");
line.add("Cynthia");
line.add("Raymond");
line.add("Bryan");
line.add("Clayton");
for (String name : line){
System.out.println(name);
}
// to remove item from the head
line.poll();
for (String name : line){
System.out.println(name);
}
//also remove item/object by using remove method
line.remove("Raymond");
for (String name : line){
System.out.println(name);
}
//peek at the head
System.out.println("The head: "+name.peek());
}
}
//Simulation of using the above collection. first using TreeSet.
//1. Using a TreeSet to fliter out duplicate words
import java.util.TreeSet;
public class WordList {
public static void main(String[] args) {
String text;
text = " Let no one caught in sin remain Inside the lie of inward shame We fix our eyes upon the cross "
+ " And run to him who showed great love And bled for us Freely you bled for us Christ is risen from the dead "
+ " Trampling over death by death Come awake, come awake Come and rise up from the grave Christ is risen from the dead "
+ " We are one with him again Come awake come awake Come and rise up from the grave Beneath the weight of all our sin "
+ " You bow to none but heavens will No scheme of hell no scoffer's crown No burden great can hold you down "
+ " In strength you reign Forever let your church proclaim Christ is risen from the dead Trampling over death by death "
+ " Come awake come awake Come and rise up from the grave Christ is risen from the dead We are one with him again "
+ " Come awake come awake Come and rise up from the grave Oh death Where is your sting Oh hell Where is your victory "
+ " Oh Church Come stand in the light The glory of God has defeated the night Oh death Where is your sting "
+ " Oh hell Where is your victory Oh Church Come stand in the light Our God is not dead hes alive hes alive "
+ " Christ is risen from the dead Trampling over death by death Come awake come awake Come and rise up from the grave "
+ " Christ is risen from the dead We are one with him again Come awake come awake Come and rise up from the grave "
//text can be put into a file and read
String[] words = text.split(" ");
for(String w : words){
System.out.println(w);
}
System.out.println("The number of words :"+ text.length());
TreeSet<String> = wordList = new TreeSet<String>();
for(String w : words){
wordList.add(w);
}
System.out.println("The number of unique words :"+ wordList.length());
for(String w : wordList){
System.out.println(w);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment