Java tutorial
//package com.java2s; /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ import java.util.Collection; import java.util.Dictionary; import java.util.Enumeration; import java.util.Iterator; import java.util.Vector; public class Main { /** * Counts how often the given Object occurs in the given * collection using equals() for comparison. * * @param c collection in which to search * @param o object to search * @return frequency * @since Ant 1.8.0 */ public static int frequency(Collection<?> c, Object o) { // same as Collections.frequency introduced with JDK 1.5 int freq = 0; if (c != null) { for (Iterator<?> i = c.iterator(); i.hasNext();) { Object test = i.next(); if (o == null ? test == null : o.equals(test)) { freq++; } } } return freq; } /** * Please use Vector.equals() or List.equals(). * @param v1 the first vector. * @param v2 the second vector. * @return true if the vectors are equal. * @since Ant 1.5 * @deprecated since 1.6.x. */ public static boolean equals(Vector<?> v1, Vector<?> v2) { if (v1 == v2) { return true; } if (v1 == null || v2 == null) { return false; } return v1.equals(v2); } /** * Dictionary does not have an equals. * Please use Map.equals(). * * <p>Follows the equals contract of Java 2's Map.</p> * @param d1 the first directory. * @param d2 the second directory. * @return true if the directories are equal. * @since Ant 1.5 * @deprecated since 1.6.x. */ public static boolean equals(Dictionary<?, ?> d1, Dictionary<?, ?> d2) { if (d1 == d2) { return true; } if (d1 == null || d2 == null) { return false; } if (d1.size() != d2.size()) { return false; } Enumeration<?> e1 = d1.keys(); while (e1.hasMoreElements()) { Object key = e1.nextElement(); Object value1 = d1.get(key); Object value2 = d2.get(key); if (value2 == null || !value1.equals(value2)) { return false; } } // don't need the opposite check as the Dictionaries have the // same size, so we've also covered all keys of d2 already. return true; } }