Contents

  1. ./FindDuplicates2.java
  2. ./FindDuplicates3.java
  3. ./FindDuplicates.java
  4. ./ListIteratorExamples.java
  5. ./MapExample.java

./FindDuplicates2.java 1/5

[
top][prev][next]
import java.util.HashSet;
import java.util.Set;

/**
 * From the array of command-line arguments, identify the duplicates.
 * 
 * 
 * 
 * @author CSCI209 Class
 * 
 */
public class FindDuplicates2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
	    
	    System.out.println("This program finds the duplicates in the command-line arguments.");
		
	    Set<String> nonDuplicates = new HashSet<>();
	    
	    for( String string : args ) {
            if( nonDuplicates.contains(string) ) {
                System.out.println("found duplicate " + string); 
            } else{
                nonDuplicates.add(string);   
            }
	    }
	}

}

./FindDuplicates3.java 2/5

[
top][prev][next]
import java.util.HashSet;
import java.util.Set;

/**
 * From the array of command-line arguments, identify the duplicates.
 * 
 * Solution leverages the add method, which returns a boolean value--
 * true if that element was not already in the set (so the element was 
 * added to the set) or false if it was already in the set.
 * 
 * @author CSCI209 Class
 * 
 */
public class FindDuplicates3 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
	    System.out.println("This program finds the duplicates in the command-line arguments.");
	     
	    Set<String> nonDuplicates = new HashSet<>();
	    
	    for( String string : args ) {
            if( ! nonDuplicates.add(string) ) {
                System.out.println("found duplicate " + string); 
            }
	    }

	}

}

./FindDuplicates.java 3/5

[
top][prev][next]
import java.util.HashSet;
import java.util.Set;

/**
 * From the array of command-line arguments, identify the duplicates
 * 
 * @author CSCI209 Class
 * 
 */
public class FindDuplicates {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
	    System.out.println("This program finds the duplicates in the command-line arguments.");
	}

}

./ListIteratorExamples.java 4/5

[
top][prev][next]
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.List;
import java.util.Random;

/**
 * Examples of using List Iterators
 * 
 * @author CSCI209
 */
public class ListIteratorExamples {

	/**
	 * Condition (we are using this for the items that we want to keep in our
	 * list).
	 * 
	 * @param value the value to consider
	 * @return true if the value is evenly divisible by 2
	 */
	public static boolean condition(int value) {
		return value % 2 == 0;
	}

	/**
	 * Display the given list, starting with the header.
	 *
	 * @param myList the list to be displayed
	 * @param header the header to display the list
	 */
	private static void displayList(List<Integer> myList, String header) {
		System.out.println(header);
		for (Integer i : myList) {
			System.out.println(i);
		}
	}

	public static void main(String[] args) {
	    
	    Random numberGenerator = new Random();

		// set up a list
		List<Integer> myList = new ArrayList<>();
		for (int i = 0; i < 10; i++) {
			myList.add(numberGenerator.nextInt());
		}

		displayList(myList, "Original List:");
		System.out.println();

		Iterator<Integer> iter = myList.iterator();

		while (iter.hasNext()) {
			Integer item = iter.next();
			if (!condition(item)) {
				// alternatively, could mutate this object, if the object has
				// mutator methods available.
				// Can't replace this item or remove the item and add in a new
				// object.
				iter.remove();
			}
		}

		displayList(myList, "Next List:");
		
		ListIterator<Integer> iter2 = myList.listIterator(myList.size());
		int counter = myList.size();
		while( iter2.hasPrevious() ) {
		    iter2.previous();
		    if( counter % 2 == 0 ) {
		        iter2.add(counter);
		    }
		    counter--;
		}
		
		System.out.println();
		displayList(myList, "Final List:");

	}

}

./MapExample.java 5/5

[
top][prev][next]
import java.util.HashMap;
import java.util.Map;

/**
 * Trivial example of using a map.
 * Maps the argument to the length of the argument
 * 
 * @author CSCI209
 *
 */
public class MapExample {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
	    // State the key's type and the value's type
		Map<String, Integer> argToLength = new HashMap<>();

		for (String arg : args) {
			// if there are duplicate arguments, this will write over that entry in the map.
			argToLength.put(arg, arg.length());
		}
		
		// display the results
		System.out.println("Arguments' Lengths: ");
		for( String arg : argToLength.keySet() ) {
			System.out.println(arg + " --> " + argToLength.get(arg));
		}
		
		// ------- Another Example --------
		// count the arguments
		Map<String, Integer> argToCount = new HashMap<>();
		
		System.out.println("\n");

		for (String arg : args) {
			// if there are duplicate arguments, this will write over that entry in the map.
			if( argToCount.containsKey(arg) ) {
			    int oldCount = argToCount.get(arg);
			    // showing using put and getting the old value back
			    int oldValue = argToCount.put(arg, oldCount + 1);
			    System.out.println("The old value for " + arg + " was " + oldValue);
			} else {
			    argToCount.put(arg, 1);   
			}
		}
		
		// display the results
		System.out.println("\nArguments' Number of Occurrences: ");
		for( String arg : argToLength.keySet() ) {
			System.out.println(arg + " --> " + argToCount.get(arg));
		}

	}

}

Generated by GNU Enscript 1.6.5.90.