Redundancy Checker

Tuesday, August 4th, 2009

/*
 *        Code by David Beuze. No rights reserved :) - 2006
 *
 *        contact: smerpy@gmail.com
 */

/*
 *        This code snippet takes a list of objects and checks for any redundancy in the list.
 *        In this example I’ve taken a list of Integer, but it can be generalized to any kind
 *        of object.
 */

/*Output:
 * 
 * Oh no! The value 1 is redundant.
 * 
 * */

import java.util.Arrays;
import java.util.List;
import java.util.ListIterator;

public class RedundancyChecker {

  public static void main(String[] a) {
    new RedundancyChecker();
  }

  public RedundancyChecker() {

    Integer[] array = new Integer[5]; // Create and
    // array of
    // Integer
    Integer i0 = new Integer(0);
    array[0] = i0;
    Integer i1 = new Integer(1); // <——–
    array[1] = i1; // |
    Integer i2 = new Integer(2); // |— redundant
    // values
    array[2] = i2; // |
    Integer i3 = new Integer(1); // <——–
    array[3] = i3;
    Integer i4 = new Integer(4);
    array[4] = i4;

    // transform the array into a List
    List l = Arrays.asList(array);

    // Check the List
    checkForRedundancy(l);
  }

  public boolean checkForRedundancy(List l) {
    ListIterator li1 = l.listIterator(); // Make a
    // general
    // ListIterator
    // on the list

    while (li1.hasNext()) {
      // Store the value of the actual first checked
      // element of the List,
      // it needs to be stored because it calls the
      // .next(), which we can call only once per loop
      // in order to sweep the whole list.
      int check1 = ((Integer) li1.next()).intValue();

      // Make a second ListIterator that will start
      // with the element that is just after the
      // actual first checked one,
      // in order to avoid checking several times the
      // same elements.
      ListIterator li2 = l.listIterator(li1.nextIndex() + 1);

      while (li2.hasNext()) {
        // Store the following elements one by
        // one and check to see if they match
        // the actual one.
        int check2 = ((Integer) li2.next()).intValue();
        if (check1 == check2) {
          System.out.println("Oh no! The value " + check1 + " is redundant.");
          return true;
        }
      }
      // The .next() method has already been called at
      // the beginning of the loop.
    }
    return false;
  }
}
           
       

A Map implemented with ArrayLists

Monday, July 20th, 2009

// : c11:SlowMap.java
// A Map implemented with ArrayLists.
// From ’Thinking in Java, 3rd ed.’ (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class SlowMap extends AbstractMap {

  private List keys = new ArrayList(), values = new ArrayList();

  public Object put(Object key, Object value) {
    Object result = get(key);
    if (!keys.contains(key)) {
      keys.add(key);
      values.add(value);
    } else
      values.set(keys.indexOf(key), value);
    return result;
  }

  public Object get(Object key) {
    if (!keys.contains(key))
      return null;
    return values.get(keys.indexOf(key));
  }

  public Set entrySet() {
    Set entries = new HashSet();
    Iterator ki = keys.iterator(), vi = values.iterator();
    while (ki.hasNext())
      entries.add(new MPair(ki.next(), vi.next()));
    return entries;
  }

  public String toString() {
    StringBuffer s = new StringBuffer("{");
    Iterator ki = keys.iterator(), vi = values.iterator();
    while (ki.hasNext()) {
      s.append(ki.next() + "=" + vi.next());
      if (ki.hasNext())
        s.append(", ");
    }
    s.append("}");
    return s.toString();
  }

  public static void main(String[] args) {
    SlowMap m = new SlowMap();
    m.put("Adobe", "Mountain View, CA");
    m.put("IBM", "White Plains, NY");
    m.put("Learning Tree", "Los Angeles, CA");
    m.put("Microsoft", "Redmond, WA");
    m.put("Netscape", "Mountain View, CA");
    m.put("O’Reilly", "Sebastopol, CA");
    m.put("Sun", "Mountain View, CA");
    System.out.println(m);

  }
} ///:~
class MPair implements Entry, Comparable {
  Object key, value;
  MPair(Object k, Object v) {
    key = k;
    value = v;
  }
  public Object getKey() { return key; }
  public Object getValue() { return value; }
  public Object setValue(Object v){
    Object result = value;
    value = v;
    return result;
  }
  public boolean equals(Object o) {
    return key.equals(((MPair)o).key);
  }
  public int compareTo(Object rv) {
    return ((Comparable)key).compareTo(
      ((MPair)rv).key);
  }
} ///:~

           
       

new List() Add(assembly1) Indexer

Wednesday, July 15th, 2009

 
 
 
using System;
using System.Reflection;
using System.Collections.Generic;

class MainClass {
    public static void Main(string[] args) {

        AssemblyName assembly1 = new AssemblyName("com.microsoft.crypto, Culture=en, PublicKeyToken=a5d015c7d5a0b012, Version=1.0.0.0");

        List<AssemblyName> assemblyList = new List<AssemblyName>();
        assemblyList.Add(assembly1);
        AssemblyName ass2 = assemblyList[0];
        Console.WriteLine("\nFound AssemblyName in list: {0}", ass2);
    }
}

 

Sequence

Monday, July 13th, 2009

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector;

public class Sequence {
  public static void main(String args[]) throws IOException {
    Vector v = new Vector(3);
    v.add(new FileInputStream("/etc/motd"));
    v.add(new FileInputStream("foo.bar"));
    v.add(new FileInputStream("/temp/john.txt"));
    Enumeration e = v.elements();
    SequenceInputStream sis = new SequenceInputStream(e);
    InputStreamReader isr = new InputStreamReader(sis);
    BufferedReader br = new BufferedReader(isr);
    String line;
    while ((line = br.readLine()) != null) {
      System.out.println(line);
    }
    br.close();
  }
}
           
       

Remove range from ArrayList

Saturday, July 11th, 2009

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

class Program {
    static void Main(string[] args) {
        ArrayList baseballTeams = new ArrayList();
        baseballTeams.Add("s");
        baseballTeams.Add("r");
        baseballTeams.Add("i");

        string[] myStringArray = new string[2];
        myStringArray[0] = "t";
        myStringArray[1] = "L";

        baseballTeams.AddRange(myStringArray);

        baseballTeams.RemoveRange(2, 2);

        foreach (string item in baseballTeams) {
            Console.Write(item + "\n");
        }

        Console.ReadLine();

    }
}