List of usage examples for java.util Set size
int size();
From source file:Main.java
/** * @param disorderBoss//from w w w . j av a2s .com * @return */ public static <T> List<T> set2List(Set<T> set) { List<T> ret = newArrayList(set.size()); for (T t : set) { ret.add(t); } return ret; }
From source file:Main.java
public static <T> T getRandomFrom(Set<T> set, Random random) { int randomIndex = random.nextInt(set.size()); Iterator<T> iterator = set.iterator(); for (int i = 0; i < randomIndex; i++) { if (!iterator.hasNext()) break; iterator.next();// w ww . j a va 2s . c om } return iterator.next(); }
From source file:Main.java
/** * Given a set, determine the proper initial size for a new set to hold the same number of values. * Specifically we want to account for load size and load factor to prevent immediate resizing. * * @param original The original set/*from w ww .ja va 2 s . c om*/ * * @return The proper size. */ public static int determineProperSizing(Set original) { return determineProperSizing(original.size()); }
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> T[] toArray(Set<T> items, Class<T> tClass) { if (items == null || items.size() == 0) return null; int size = items.size(); try {/*www. ja v a 2 s . c o m*/ T[] array = (T[]) Array.newInstance(tClass, size); return items.toArray(array); } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:Main.java
public static String encode(Map<String, String> params, String encoding) { StringBuilder encodedParams = new StringBuilder(); try {/*from www . j a va2 s . co m*/ Set uee = params.entrySet(); int size = uee.size(); int index = 0; Iterator iterator = uee.iterator(); while (iterator.hasNext()) { Map.Entry entry = (Map.Entry) iterator.next(); encodedParams.append(URLEncoder.encode((String) entry.getKey(), encoding)); encodedParams.append('='); encodedParams.append(URLEncoder.encode((String) entry.getValue(), encoding)); ++index; if (index < size) { encodedParams.append('&'); } } return encodedParams.toString(); } catch (UnsupportedEncodingException e) { throw new RuntimeException("Encoding not supported: " + encoding, e); } }
From source file:Main.java
public static <T> List<T> convertSetToList(Set<T> set) { List<T> list = new ArrayList<T>(); if (set != null && set.size() > 0) { for (T t : set) { list.add(t);//from w w w. ja v a2 s . c o m } } return list; }
From source file:Main.java
public static Set minus(Set oriSet, Set tarSet) { if (oriSet == null || oriSet.size() == 0) return oriSet; if (tarSet == null || tarSet.size() == 0) return oriSet; Iterator oriItor = oriSet.iterator(); Set minusSet = new HashSet(); while (oriItor.hasNext()) { Object oriObj = oriItor.next(); Iterator tarItor = tarSet.iterator(); boolean isExist = false; while (tarItor.hasNext()) { Object tarObj = tarItor.next(); if (tarObj.equals(oriObj)) { isExist = true;// w w w . j av a 2 s . c o m break; } } if (!isExist) { minusSet.add(oriObj); } } return minusSet; }
From source file:Main.java
/** * patterns: ["/util//note"]// w w w . j a v a2 s . co m * @param Set<String> patterns * @return /util/note */ public static String first(Set<String> patterns) { String str = ""; if (patterns != null && patterns.size() > 0) { Iterator<String> iterator = patterns.iterator(); while (iterator.hasNext()) { str = (String) iterator.next(); str = str.replaceAll("//", "/"); break; } } return str; }
From source file:Main.java
public static Map<? extends Object, ? extends Object> transformSetToMap(Set<? extends Object> objects) { Map<Object, Object> map = new HashMap<Object, Object>(objects.size()); for (Object obj : objects) { map.put(obj, obj);//from w w w. j a v a 2s. co m } return map; }
From source file:Main.java
public static List toList(Set set) { if (set == null) { return null; }/*from w w w . jav a 2 s. c om*/ if (set.size() == 0) return new ArrayList(); List list = new ArrayList(); Iterator itor = set.iterator(); while (itor.hasNext()) { Object obj = itor.next(); list.add(obj); } return list; }