当前位置: 首页>>代码示例>>Java>>正文


Java HashSet.remove方法代码示例

本文整理汇总了Java中java.util.HashSet.remove方法的典型用法代码示例。如果您正苦于以下问题:Java HashSet.remove方法的具体用法?Java HashSet.remove怎么用?Java HashSet.remove使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.util.HashSet的用法示例。


在下文中一共展示了HashSet.remove方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: main

import java.util.HashSet; //导入方法依赖的package包/类
public static void main(String[] args) {     
    /* Read some input */
    Scanner scan = new Scanner(System.in);
    int n = scan .nextInt();
    
    /* Count pairs */
    HashSet<Integer> set = new HashSet<>();
    int pairs = 0;
    for (int i = 0; i < n; i++) {
        int cost = scan.nextInt();
        if (set.contains(cost)) {
            set.remove(cost);
            pairs++;
        } else {
            set.add(cost);
        }
    }

    /* Print output */
    scan.close();
    System.out.println(pairs);
}
 
开发者ID:rshaghoulian,项目名称:HackerRank_solutions,代码行数:23,代码来源:Solution.java

示例2: intersection

import java.util.HashSet; //导入方法依赖的package包/类
/**
 * Returns a new list containing all elements that are contained in
 * both given lists.
 *
 * @param <E> the element type
 * @param list1  the first list
 * @param list2  the second list
 * @return  the intersection of those two lists
 * @throws NullPointerException if either list is null
 */
public static <E> List<E> intersection(final List<? extends E> list1, final List<? extends E> list2) {
    final List<E> result = new ArrayList<E>();

    List<? extends E> smaller = list1;
    List<? extends E> larger = list2;
    if (list1.size() > list2.size()) {
        smaller = list2;
        larger = list1;
    }

    final HashSet<E> hashSet = new HashSet<E>(smaller);

    for (final E e : larger) {
        if (hashSet.contains(e)) {
            result.add(e);
            hashSet.remove(e);
        }
    }
    return result;
}
 
开发者ID:funkemunky,项目名称:HCFCore,代码行数:31,代码来源:ListUtils.java

示例3: main

import java.util.HashSet; //导入方法依赖的package包/类
public static void main(String[] args) {

    //create HashSet object
    HashSet hSet = new HashSet();

    /*
      To get the size of HashSet use
      int size() method of HashSet class. It returns the number of elements
      stored in HashSet object.
    */
    System.out.println("Size of HashSet : " + hSet.size());

    //add elements to HashSet object
    hSet.add(new Integer("1"));
    hSet.add(new Integer("2"));
    hSet.add(new Integer("3"));

    System.out.println("Size of HashSet after addition : " + hSet.size());

    //remove one element from HashSet using remove method
    hSet.remove(new Integer("1"));
    System.out.println("Size of HashSet after removal : " + hSet.size());
  }
 
开发者ID:tranleduy2000,项目名称:javaide,代码行数:24,代码来源:GetSizeOfJavaHashSetExample.java

示例4: main

import java.util.HashSet; //导入方法依赖的package包/类
public static void main(String[] args) {
    Scanner scann = new Scanner(System.in);
    HashSet<String> nums = new HashSet<>();
    String[] input;
    while(!(input = scann.nextLine().split(", "))[0].equals("END")){
        if(input[0].equals("IN")){
            nums.add(input[1]);
        } else {
            nums.remove(input[1]);
        }
    }
    if(nums.isEmpty()){
        System.out.println("Parking Lot is Empty");

    } else {
        for (String num :
                nums) {
            System.out.println(num);
        }
    }


}
 
开发者ID:kostovhg,项目名称:SoftUni,代码行数:24,代码来源:p04_ParkingLot.java

示例5: main

import java.util.HashSet; //导入方法依赖的package包/类
public static void main(String[] args) {
    
    HashSet<Integer> hs = new HashSet<>(5);        
    System.out.println("Init hashset 5. Size: " + hs.size());
    
    // add some elements
    hs.add(1);
    hs.add(4);
    hs.add(1);
    hs.add(2);
    System.out.println("Add. Content: " + hs.toString());        
    hs.add(3);
    System.out.println("Add 3. Content: " + hs.toString());
    System.out.println("Current Size: " + hs.size());
    
    // remove
    hs.remove(2);
    System.out.println("remove 2. Content: " + hs.toString());
    
    // contains
    System.out.println("Is Set contains 4? "+hs.contains(4));                              
}
 
开发者ID:mkdika,项目名称:learnjava8,代码行数:23,代码来源:TestHashSet.java

示例6: testNodesUnderRootRepresentTheirFiles

import java.util.HashSet; //导入方法依赖的package包/类
public void testNodesUnderRootRepresentTheirFiles () throws Exception {
    HashSet<File> roots = new HashSet<File>(Arrays.asList (File.listRoots()));
    
    Node[] arr = FavoritesNode.getNode ().getChildren ().getNodes (true);
    
    for (int i = 0; i < arr.length; i++) {
        File f = FavoritesNode.fileForNode (arr[i]);
        if (roots.remove (f)) {
            Node[] nexts = arr[i].getChildren().getNodes (true);
            for (int j = 0; j < nexts.length; j++) {
                File file = FavoritesNode.fileForNode (nexts[i]);
                assertNotNull ("For node: " + nexts[i] + " there has to be file", file);
                assertEquals ("Correct parent for " + nexts[i], f, file.getParentFile());
            }
        }
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:18,代码来源:RootsTest.java

示例7: getPinnings

import java.util.HashSet; //导入方法依赖的package包/类
private boolean[] getPinnings(DatanodeInfo[] nodes, boolean shouldLog) {
  if (favoredNodes == null) {
    return null;
  } else {
    boolean[] pinnings = new boolean[nodes.length];
    HashSet<String> favoredSet =
        new HashSet<String>(Arrays.asList(favoredNodes));
    for (int i = 0; i < nodes.length; i++) {
      pinnings[i] = favoredSet.remove(nodes[i].getXferAddrWithHostname());
      if (DFSClient.LOG.isDebugEnabled()) {
        DFSClient.LOG.debug(nodes[i].getXferAddrWithHostname() +
            " was chosen by name node (favored=" + pinnings[i] +
            ").");
      }
    }
    if (shouldLog && !favoredSet.isEmpty()) {
      // There is one or more favored nodes that were not allocated.
      DFSClient.LOG.warn(
          "These favored nodes were specified but not chosen: " +
          favoredSet +
          " Specified favored nodes: " + Arrays.toString(favoredNodes));

    }
    return pinnings;
  }
}
 
开发者ID:naver,项目名称:hadoop,代码行数:27,代码来源:DFSOutputStream.java

示例8: changeIDom

import java.util.HashSet; //导入方法依赖的package包/类
/**
 * BasicBlock parent's new dominator is newBB. Update parent's dominance frontier
 * to reflect this change.
 * @param bb
 * @param newBB
 * @param dt
 */
public void changeIDom(BasicBlock bb, BasicBlock newBB,
        DomTree dt)
{
     HashSet<BasicBlock> newDF = find(newBB);
     HashSet<BasicBlock> df = find(bb);

     if (df == null)
         return;

     for (BasicBlock dfMember : df)
     {
         if (!dt.dominates(newBB, dfMember))
             newDF.add(dfMember);
     }
     newDF.remove(bb);
}
 
开发者ID:JianpingZeng,项目名称:xcc,代码行数:24,代码来源:DominanceFrontier.java

示例9: matches

import java.util.HashSet; //导入方法依赖的package包/类
@Override
public boolean matches(Object o) {
  ContainerLocalizationRequestEvent evt =
      (ContainerLocalizationRequestEvent) o;
  final HashSet<LocalResourceRequest> expected =
      new HashSet<LocalResourceRequest>(resources);
  for (Collection<LocalResourceRequest> rc : evt.getRequestedResources()
      .values()) {
    for (LocalResourceRequest rsrc : rc) {
      if (!expected.remove(rsrc)) {
        return false;
      }
    }
  }
  return expected.isEmpty();
}
 
开发者ID:naver,项目名称:hadoop,代码行数:17,代码来源:TestContainer.java

示例10: dfs

import java.util.HashSet; //导入方法依赖的package包/类
private void dfs(final T node, final ArrayList<T> result, final HashSet<T> tmpMarked) {
  if (result.contains(node)) {
    // We've already seen and added the node to the result list, skip...
    return;
  }
  if (tmpMarked.contains(node)) {
    throw new RuntimeException("This graph contains cyclic dependencies");
  }
  // Temporarily mark the node
  tmpMarked.add(node);
  // Recursively dfs all of the node's edges
  final ArrayList<T> edges = mGraph.get(node);
  if (edges != null) {
    for (int i = 0, size = edges.size(); i < size; i++) {
      dfs(edges.get(i), result, tmpMarked);
    }
  }
  // Unmark the node from the temporary list
  tmpMarked.remove(node);
  // Finally add it to the result list
  result.add(node);
}
 
开发者ID:commonsguy,项目名称:cwac-crossport,代码行数:23,代码来源:DirectedAcyclicGraph.java

示例11: onPackageRemoved

import java.util.HashSet; //导入方法依赖的package包/类
@Override
public void onPackageRemoved(String packageName, UserHandleCompat user) {
    String prefKey = INSTALLED_PACKAGES_PREFIX + mUserManager.getSerialNumberForUser(user);
    HashSet<String> packageSet = new HashSet<>();
    if (getUserApps(packageSet, prefKey) && packageSet.remove(packageName)) {
        mPrefs.edit().putStringSet(prefKey, packageSet).apply();
    }

    onLauncherPackageRemoved(packageName, user);
}
 
开发者ID:TeamBrainStorm,项目名称:SimpleUILauncher,代码行数:11,代码来源:CachedPackageTracker.java

示例12: onPackageRemoved

import java.util.HashSet; //导入方法依赖的package包/类
@Override
public void onPackageRemoved(String packageName, UserHandle user) {
    String prefKey = INSTALLED_PACKAGES_PREFIX + mUserManager.getSerialNumberForUser(user);
    HashSet<String> packageSet = new HashSet<>();
    if (getUserApps(packageSet, prefKey) && packageSet.remove(packageName)) {
        mPrefs.edit().putStringSet(prefKey, packageSet).apply();
    }

    onLauncherPackageRemoved(packageName, user);
}
 
开发者ID:enricocid,项目名称:LaunchEnr,代码行数:11,代码来源:CachedPackageTracker.java

示例13: removeWatcher

import java.util.HashSet; //导入方法依赖的package包/类
public synchronized void removeWatcher(Watcher watcher) {
    HashSet<String> paths = watch2Paths.remove(watcher);
    if (paths == null) {
        return;
    }
    for (String p : paths) {
        HashSet<Watcher> list = watchTable.get(p);
        if (list != null) {
            list.remove(watcher);
            if (list.size() == 0) {
                watchTable.remove(p);
            }
        }
    }
}
 
开发者ID:l294265421,项目名称:ZooKeeper,代码行数:16,代码来源:WatchManager.java

示例14: leaveBest

import java.util.HashSet; //导入方法依赖的package包/类
private static void leaveBest( HashSet<Badge> list, Badge...badges ) {
	for (int i=badges.length-1; i > 0; i--) {
		if (list.contains( badges[i])) {
			for (int j=0; j < i; j++) {
				list.remove( badges[j] );
			}
			break;
		}
	}
}
 
开发者ID:mango-tree,项目名称:UNIST-pixel-dungeon,代码行数:11,代码来源:Badges.java

示例15: testCreateDb

import java.util.HashSet; //导入方法依赖的package包/类
/**
     * This method tests that our database contains all of the tables that we think it should
     * contain. Although in our case, we just have one table that we expect should be added
     * <p>
     * {@link com.example.android.sunshine.data.WeatherContract.WeatherEntry#TABLE_NAME}.
     * <p>
     * Despite only needing to check one table name in Sunshine, we set this method up so that
     * you can use it in other apps to test databases with more than one table.
     */
    @Test
    public void testCreateDb() {
        /*
         * Will contain the name of every table in our database. Even though in our case, we only
         * have only table, in many cases, there are multiple tables. Because of that, we are
         * showing you how to test that a database with multiple tables was created properly.
         */
        final HashSet<String> tableNameHashSet = new HashSet<>();

        /* Here, we add the name of our only table in this particular database */
        tableNameHashSet.add(REFLECTED_TABLE_NAME);
        /* Students, here is where you would add any other table names if you had them */
//        tableNameHashSet.add(MyAwesomeSuperCoolTableName);
//        tableNameHashSet.add(MyOtherCoolTableNameThatContainsOtherCoolData);

        /* We think the database is open, let's verify that here */
        String databaseIsNotOpen = "The database should be open and isn't";
        assertEquals(databaseIsNotOpen,
                true,
                database.isOpen());

        /* This Cursor will contain the names of each table in our database */
        Cursor tableNameCursor = database.rawQuery(
                "SELECT name FROM sqlite_master WHERE type='table'",
                null);

        /*
         * If tableNameCursor.moveToFirst returns false from this query, it means the database
         * wasn't created properly. In actuality, it means that your database contains no tables.
         */
        String errorInCreatingDatabase =
                "Error: This means that the database has not been created correctly";
        assertTrue(errorInCreatingDatabase,
                tableNameCursor.moveToFirst());

        /*
         * tableNameCursor contains the name of each table in this database. Here, we loop over
         * each table that was ACTUALLY created in the database and remove it from the
         * tableNameHashSet to keep track of the fact that was added. At the end of this loop, we
         * should have removed every table name that we thought we should have in our database.
         * If the tableNameHashSet isn't empty after this loop, there was a table that wasn't
         * created properly.
         */
        do {
            tableNameHashSet.remove(tableNameCursor.getString(0));
        } while (tableNameCursor.moveToNext());

        /* If this fails, it means that your database doesn't contain the expected table(s) */
        assertTrue("Error: Your database was created without the expected tables.",
                tableNameHashSet.isEmpty());

        /* Always close the cursor when you are finished with it */
        tableNameCursor.close();
    }
 
开发者ID:fjoglar,项目名称:android-dev-challenge,代码行数:64,代码来源:TestSunshineDatabase.java


注:本文中的java.util.HashSet.remove方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。