Code Comments

Code Comments

Tips and short tutorials on various programming technologies

Code Comments RSS Feed
 
 
 
 

Archive for October, 2008

How to See Which Processes Are Running in Linux

This is pretty straightforward to experienced Linux users, but it wasn’t obvious to me when I started using Linux. Linux has a nifty program that will tell you what processes are currently running, how much memory/CPU they are using, etc. It also allows you to kill processes that may be running in the background.
At the [...] Read more »

Add an Array to an ArrayList in Java

No personal offense to the people at Sun, but this has to be one of the most awkward things to do in Java that should not be awkward. You could go about it by looping through the array and adding each element. But this could be expensive if you have a large array or at [...] Read more »

Fast Way to Get All Unique Values from a List in Java

Let’s say you have an ArrayList of string values and want to get all unique values from it. A seemingly obvious way to do this would be to create a list and before adding the next item to check that an identical item doesn’t already exist in the list.

ArrayList uniqueVals = new ArrayList();
 
for (Object x [...] Read more »

Find Union of Two Collections in Java

I explained in this post how to find the intersection of two collections in Java. This post will explain how to find the union of two collections. A union means finding all unique values from the two lists combined.

import java.util.*;
 
public static Collection Union(Collection coll1, Collection coll2)
{
Set union = new HashSet(coll1);
[...] Read more »

Intersecting Two Lists/Sets in Java

If you have two collections of objects, sometimes you want to find the intersection of those collections. This means finding only those items that overlap between the two collections. This is straightforward to do in Java.

import java.util.*;
 
public static Set Intersect(Set set1, Set set2)
{
Set intersection = new HashSet(set1);
intersection.retainAll(new [...] Read more »

Creating a Custom Iterator in Java

Whenever you use a for-each loop in Java, you are iterating over a class that implements the Iterable interface and that uses a second class that implements the Iterator interface to determine which objects are looped over.
Arrays and most Collection classes handle this behind the scenes. Here’s an example of this in action:

String[] items = [...] Read more »

How to Read Really Large Files in Java

Here’s a class that makes this really easy. The entire file is never read into memory, so it should be able to handle files of any size (that your operating system can handle).

import java.util.*;
import java.io.*;
 
public class BigFile implements Iterable<String>
{
private BufferedReader _reader;
 
public BigFile(String filePath) throws Exception
[...] Read more »

Determine Whether a String Contains a Substring in Python

This can be done simply using the “find” method. If the value is found, the first index of that value is returned. If it is not found, -1 is returned.
def contains(theString, theQueryValue):
  return theString.find(theQueryValue) > -1
x = “abcdefg”
print contains(x, “abc”) // True
print contains(x, “xyz”) //False Read more »

Encrypt and Decrypt Large Files in Linux

One tool I’ve found really useful and easy to use for encrypting files is called bcrypt. This is an open source project that uses the BlowFish encryption algorithm. All you have to do is enter the file pattern for the files you want to encrypt and a password. Then when you want to decrypt them, [...] Read more »