Java Set容器添加相同元素的返回值判断

Java Set容器添加相同元素的返回值判断代码如下:
 

  1. /**
  2.  * @from www.everycoding.com
  3.  * @Description:Java Set容器添加相同元素的返回值判断
  4.  */
  5. public class SetAdd {
  6. public static void main(String[] args) {
  7. Set<String> set = new HashSet<String>();
  8.     set.add("A");
  9.     // Set容器添加元素时,若此元素已存在,则返回false
  10.     if (set.add("A")) {
  11.       System.out.println("添加成功");
  12.     } else {
  13.       System.out.println("添加失败,原因: A 已存在,Set容器不能存入两个相同的元素");
  14.     }
  15. }
  16. }
  17.  附录:spring  XmlBeanDefinitionReader 的loadBeanDefinitions方法的源码 
  18.  
  19. /**
  20.  * Load bean definitions from the specified XML file.
  21.  * @param encodedResource the resource descriptor for the XML file,
  22.  * allowing to specify an encoding to use for parsing the file
  23.  * @return the number of bean definitions found
  24.  * @throws BeanDefinitionStoreException in case of loading or parsing errors
  25.  */
  26. public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {
  27. Assert.notNull(encodedResource, "EncodedResource must not be null");
  28. if (logger.isInfoEnabled()) {
  29. logger.info("Loading XML bean definitions from " + encodedResource.getResource());
  30. }

  31. Set<EncodedResource> currentResources = this.resourcesCurrentlyBeingLoaded.get();
  32. if (currentResources == null) {
  33. currentResources = new HashSet<EncodedResource>(4);
  34. this.resourcesCurrentlyBeingLoaded.set(currentResources);
  35. }
  36. if (!currentResources.add(encodedResource)) {
  37. throw new BeanDefinitionStoreException(
  38. "Detected cyclic loading of " + encodedResource + " - check your import definitions!");
  39. }
  40. try {
  41. InputStream inputStream = encodedResource.getResource().getInputStream();
  42. try {
  43. InputSource inputSource = new InputSource(inputStream);
  44. if (encodedResource.getEncoding() != null) {
  45. inputSource.setEncoding(encodedResource.getEncoding());
  46. }
  47. return doLoadBeanDefinitions(inputSource, encodedResource.getResource());
  48. }
  49. finally {
  50. inputStream.close();
  51. }
  52. }
  53. catch (IOException ex) {
  54. throw new BeanDefinitionStoreException(
  55. "IOException parsing XML document from " + encodedResource.getResource(), ex);
  56. }
  57. finally {
  58. currentResources.remove(encodedResource);
  59. if (currentResources.isEmpty()) {
  60. this.resourcesCurrentlyBeingLoaded.remove();
  61. }
  62. }
  63. }
  64.  
  65.  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值