TreeMap in Java

import java.util.TreeMap;   public class dimensionsExample { public static void main(String[] args) { TreeMap<String,String> dimensions = new TreeMap<String,String>(); dimensions.put("NC", "2333"); dimensions.put("NY", "2334"); dimensions.put("SC", "2335"); dimensions.put("CA", "2336"); System.out.println("CA: " + dimensions.get("CA")); dimensions.put("AZ", "2337"); System.out.println("Size of dimensions: " + dimensions.size()); dimensions.remove("SC"); System.out.println("Size of dimensions: " + dimensions.size()); } }
Continue reading…

Enjoyed this post? Share it!

 
 

HashMap in Java

import java.util.HashMap;   public class prices { public static void main(String[] args) { HashMap<String,Integer> prices = new HashMap<String,Integer>(); prices.put("walmart", 88); prices.put("target", 100); prices.put("kmart", 77); prices.put("lowes", 90); System.out.println("walmart’s price: " + prices.get("walmart")); System.out.println("Number of prices: " + prices.size()); if (prices.containsKey("target")) { prices.remove("target"); System.out.println("Record successfully removed."); } System.out.println("number of prices: " + prices.size()); } }
Continue reading…

Enjoyed this post? Share it!

 
 

HashSet in Java

import java.util.HashSet;   public class myHashsetExample { public static void main(String[] args) { HashSet<String> languages = new HashSet<String>(); languages.add("hindi"); languages.add("english"); languages.add("spanish"); System.out.println("The number of languages is " + languages.size()); for (String name : languages) { System.out.println(name); } languages.remove("hindi"); languages.add("tamil"); String name = "hindi"; if (languages.contains(name)) { System.out.println(name + " is in the collection."); } else […]
Continue reading…

Enjoyed this post? Share it!

 
 

PriorityQueue in Java

import java.util.PriorityQueue;   public class Cars { public static void main(String[] args) { PriorityQueue<String> car = new PriorityQueue<String>(); car.add("lincoln"); car.add("ford"); car.add("suzuki"); car.add("toyota"); car.add("nissan"); System.out.println("The car: "); for (String name : car) { System.out.println(name); } car.poll(); System.out.println("The car: "); for (String name : car) { System.out.println(name); } car.remove("suzuki"); System.out.println("The car: "); for (String name : car) […]
Continue reading…

Enjoyed this post? Share it!

 
 

TreeSet in Java

import java.util.TreeSet;   public class myTreeSet { public static void main(String[] args) { TreeSet<String> animals = new TreeSet<String>(); animals.add("Dog"); animals.add("Cat"); animals.add("Sparrow"); animals.add("Iguana"); animals.add("Dragon"); animals.add("Peacock"); System.out.println("Number of animals = " + animals.size()); for (String name : animals) { System.out.println(name); } System.out.println("Animal before Sparrow: " + animals.lower("Sparrow")); System.out.println("Animal after Sparrow: " + animals.higher("Sparrow")); System.out.println("First element: " + […]
Continue reading…

Enjoyed this post? Share it!

 
 

Arraylist in Java

import java.util.ArrayList;   public class myArrayList { public static void main(String[] args) { ArrayList<Integer> vals = new ArrayList<Integer>(); vals.add(100); vals.add(90); vals.add(80); vals.add(70); vals.add(60); int total = 0; /*for(int i = 0; i < vals.size(); ++i) { total += vals.get(i); }*/ for (Integer val : vals) { total += val; } double average = total / […]
Continue reading…

Enjoyed this post? Share it!