Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
I'm sure there's a good reason, but could someone please explain why the java.util.Set interface lacks get(int Index), or any similar get() method? It seems that sets are great for putting things into, but I can't find an elegant way of retrieving a single item from it. If I know I want the first item, I can use set.iterator().next(), but otherwise it seems I have to cast to an Array to retri...
I'm a beginner in Java. Please suggest which collection(s) can/should be used for maintaining a sorted list in Java. I have tried Map and Set but they weren't what I was looking for.
I'm making a turn-based RPG game, and my method that sorts all "Actor" objects in the order in which they all attack sorts them completely randomly. I, however, want to improve this method so that an "agility" stat that every actor has is able to improve their roll. I've looked at several methods in the Collections class, and Arrays as well, but didn't seem to find anything that does what I wan...
I am using a ConcurrentSkipListSet, that is obviously accessed through multiple threads. Now, the values that are used by the compareTo-method of the underlying objects change overtime. Because of this, I want to 'update' the ordering of the list (by resorting it, or something similar). However, java.util.Collections.sort(list) doesn't work, and just rebuilding the list is probably too slow (...
In Java, i have an arrayList of ip address. how do i find the min and max ? I have used the Collection.min() but it doesnt work given a case like : 192.168.0.1 <--min 192.168.0.250 192.168.0.9 <--max how do i return 192.168.0.1 <--min 192.168.0.250 <--max instead? ArrayList is retrieve from the database. I would need to do this operation per tick (each tick is at 5 s...
Suppose I need TreeSet with elements sorted with some domain logic. By this logic it doesn't meter order of some elements that doesn't equal so compare method can return 0, but in this case I couldn't put they in TreeSet. So, question: what disadvantages I'll have from code like this: class Foo implements Comparable<Foo>{} new TreeSet<Foo>(new Comparator<Foo>(){ @Overrid...
Let's say I have a collection of objects which can be sorted using a number of different comparators based on the different fields of the object. It would be nice to be able to know later on in the code which comparator was used to sort the Collection with and if it was ascending or descending. Is there anyway to do this elegantly instead of using a bunch of Booleans to keep track of things?
I'm accessing the minimum element of a binary tree lots of times. What implementations allow me to access the minimum element in constant time, rather than O(log n)?
I need some data structure that I can build from standard collections or using guava. So it should be mutable Map<Enum, V>. Where V is pretty interesting structure. V requirements: mutable sorted by comparator (with allowing elements such as compare(a, b) == 0) - these is need for iterations set (there is no such a and b, that a.equals(b) == true) - optional extra optional requirem...
public class Student implements java.io.Serializable { private long studentId; private String studentName; private Set<Course> courses = new HashSet<Course>(0); public Student() { } public Student(String studentName) { this.studentName = studentName; } public Student(String studentName, Set<Course> courses) { this.studentName = studentName; this.courses = cours...
In many algorithm, you're supposed to iterate over a set of elements, while the set is not empty. Since you might change the set while iterating it, you typically pull an element out of the set, and then do the iteration, possibly adding or removing elements to or from the set. Here is a typical Java code to do that. Set<Integer> possibleFactors = Sets.newHashSet(2,3,4,5,6,7,8,100); wh...
I've been able to read a four column text file into a hashmap and get it to write to a output file. However, I need to get the second column(distinct values) into a hashset and write to the output file. I've been able to create the hashset, but it is grabbing everything and not sorting. By the way I'm new, so please take this into consideration when you answer. Thanks
I'm pretty new to Java and not from a programming background. I am doing a course and am stuck on a piece I hope this is appropriate to ask a question here). The question ask to create a method that takes an array of integers as an argumetn and returns a sorted set containing the elements of the that array. I am not including the code as I don't want the answer but I would like a clue. It's...
Imagine that I need to create a Collection of elements, where order could or could not matter. Effectively all I plan on doing is using the iterator. I notice most of my colleagues using an ArrayList vs LinkedHashSet/HashSet. My question is, if I know that these elements should be unique, should I be using a Set or a List? Effectively it doesn't really make a difference, but doesn't Set more ef...
Lets say I have about 1000 sentences that I want to offer as suggestions when user is typing into a field. I was thinking about running lucene in memory search and then feeding the results into the suggestions set. The trigger for running the searches would be space char and exit from the input field. I intend to use this with GWT so the client with be just getting the results from se...
I have problem with Java's ArrayList. I've created an Object, that contains two attributes, x and y. Now I've loaded some object in my ArrayList. Problem is that I don't know how to find index of some object with x atribute I'm searching. Is there any way to do this?
Assume I have a user defined Java class called Foo such as: public class Foo { private String aField; @Override public String toString() { return aField; } } and a Collection such as: List<Foo> aList; What I am looking to do is to sort the List alphabetically based upon each member's returned '.toString()' value. I have tried using the Collections.sort()...
I'm having problems trying to pass an Integer object from a driver class as an argument for function of a SortedArray Generic class I created. From my driver class, I convert the user's int input into an Integer object to be cast onto Comparable of my SortedArray class. I continue to receive the error: "Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast t...
So here is the problem I am facing, I'm trying to implement a HashMap using an Id as key, and an object pointer as the value. Within each object, there are a couple of Integers, let's say they represent investment amounts in various categories defined in the object. However, I would like to be able to sort the investment amount in a particular category and access the Nth element, while access...
In Java, the class String implements Comparable, which means there's a total ordering on the String objects. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method. The set of String objects is also countable in the mathematical sense. I need a function that takes a String and returns the next one accordin...
Hello Can anyone help me to know how can I have a "Set" of Bytes in Java? Thank you
  /*
   * Copyright 1998-2006 Sun Microsystems, Inc.  All Rights Reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.  Sun designates this
   * particular file as subject to the "Classpath" exception as provided
   * by Sun in the LICENSE file that accompanied this code.
  *
  * This code is distributed in the hope that it will be useful, but WITHOUT
  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  * version 2 for more details (a copy is included in the LICENSE file that
  * accompanied this code).
  *
  * You should have received a copy of the GNU General Public License version
  * 2 along with this work; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  *
  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  * CA 95054 USA or visit www.sun.com if you need additional information or
  * have any questions.
  */
 
 package java.util;

A Set that further provides a total ordering on its elements. The elements are ordered using their natural ordering, or by a Comparator typically provided at sorted set creation time. The set's iterator will traverse the set in ascending element order. Several additional operations are provided to take advantage of the ordering. (This interface is the set analogue of SortedMap.)

All elements inserted into a sorted set must implement the Comparable interface (or be accepted by the specified comparator). Furthermore, all such elements must be mutually comparable: e1.compareTo(e2) (or comparator.compare(e1, e2)) must not throw a ClassCastException for any elements e1 and e2 in the sorted set. Attempts to violate this restriction will cause the offending method or constructor invocation to throw a ClassCastException.

Note that the ordering maintained by a sorted set (whether or not an explicit comparator is provided) must be consistent with equals if the sorted set is to correctly implement the Set interface. (See the Comparable interface or Comparator interface for a precise definition of consistent with equals.) This is so because the Set interface is defined in terms of the equals operation, but a sorted set performs all element comparisons using its compareTo (or compare) method, so two elements that are deemed equal by this method are, from the standpoint of the sorted set, equal. The behavior of a sorted set is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Set interface.

All general-purpose sorted set implementation classes should provide four "standard" constructors: 1) A void (no arguments) constructor, which creates an empty sorted set sorted according to the natural ordering of its elements. 2) A constructor with a single argument of type Comparator, which creates an empty sorted set sorted according to the specified comparator. 3) A constructor with a single argument of type Collection, which creates a new sorted set with the same elements as its argument, sorted according to the natural ordering of the elements. 4) A constructor with a single argument of type SortedSet, which creates a new sorted set with the same elements and the same ordering as the input sorted set. There is no way to enforce this recommendation, as interfaces cannot contain constructors.

Note: several methods return subsets with restricted ranges. Such ranges are half-open, that is, they include their low endpoint but not their high endpoint (where applicable). If you need a closed range (which includes both endpoints), and the element type allows for calculation of the successor of a given value, merely request the subrange from lowEndpoint to successor(highEndpoint). For example, suppose that s is a sorted set of strings. The following idiom obtains a view containing all of the strings in s from low to high, inclusive:

   SortedSet<String> sub = s.subSet(low, high+"\0");
A similar technique can be used to generate an open range (which contains neither endpoint). The following idiom obtains a view containing all of the Strings in s from low to high, exclusive:
   SortedSet<String> sub = s.subSet(low+"\0", high);

This interface is a member of the Java Collections Framework.

Parameters:
<E> the type of elements maintained by this set
Author(s):
Josh Bloch
Since:
1.2
See also:
Set
TreeSet
SortedMap
Collection
java.lang.Comparable
Comparator
java.lang.ClassCastException
public interface SortedSet<E> extends Set<E> {
    
Returns the comparator used to order the elements in this set, or null if this set uses the natural ordering of its elements.

Returns:
the comparator used to order the elements in this set, or null if this set uses the natural ordering of its elements
    Comparator<? super E> comparator();

    
Returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive. (If fromElement and toElement are equal, the returned set is empty.) The returned set is backed by this set, so changes in the returned set are reflected in this set, and vice-versa. The returned set supports all optional set operations that this set supports.

The returned set will throw an IllegalArgumentException on an attempt to insert an element outside its range.

Parameters:
fromElement low endpoint (inclusive) of the returned set
toElement high endpoint (exclusive) of the returned set
Returns:
a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive
Throws:
java.lang.ClassCastException if fromElement and toElement cannot be compared to one another using this set's comparator (or, if the set has no comparator, using natural ordering). Implementations may, but are not required to, throw this exception if fromElement or toElement cannot be compared to elements currently in the set.
java.lang.NullPointerException if fromElement or toElement is null and this set does not permit null elements
java.lang.IllegalArgumentException if fromElement is greater than toElement; or if this set itself has a restricted range, and fromElement or toElement lies outside the bounds of the range
    SortedSet<E> subSet(E fromElement, E toElement);

    
Returns a view of the portion of this set whose elements are strictly less than toElement. The returned set is backed by this set, so changes in the returned set are reflected in this set, and vice-versa. The returned set supports all optional set operations that this set supports.

The returned set will throw an IllegalArgumentException on an attempt to insert an element outside its range.

Parameters:
toElement high endpoint (exclusive) of the returned set
Returns:
a view of the portion of this set whose elements are strictly less than toElement
Throws:
java.lang.ClassCastException if toElement is not compatible with this set's comparator (or, if the set has no comparator, if toElement does not implement java.lang.Comparable). Implementations may, but are not required to, throw this exception if toElement cannot be compared to elements currently in the set.
java.lang.NullPointerException if toElement is null and this set does not permit null elements
java.lang.IllegalArgumentException if this set itself has a restricted range, and toElement lies outside the bounds of the range
    SortedSet<E> headSet(E toElement);

    
Returns a view of the portion of this set whose elements are greater than or equal to fromElement. The returned set is backed by this set, so changes in the returned set are reflected in this set, and vice-versa. The returned set supports all optional set operations that this set supports.

The returned set will throw an IllegalArgumentException on an attempt to insert an element outside its range.

Parameters:
fromElement low endpoint (inclusive) of the returned set
Returns:
a view of the portion of this set whose elements are greater than or equal to fromElement
Throws:
java.lang.ClassCastException if fromElement is not compatible with this set's comparator (or, if the set has no comparator, if fromElement does not implement java.lang.Comparable). Implementations may, but are not required to, throw this exception if fromElement cannot be compared to elements currently in the set.
java.lang.NullPointerException if fromElement is null and this set does not permit null elements
java.lang.IllegalArgumentException if this set itself has a restricted range, and fromElement lies outside the bounds of the range
    SortedSet<E> tailSet(E fromElement);

    
Returns the first (lowest) element currently in this set.

Returns:
the first (lowest) element currently in this set
Throws:
NoSuchElementException if this set is empty
    E first();

    
Returns the last (highest) element currently in this set.

Returns:
the last (highest) element currently in this set
Throws:
NoSuchElementException if this set is empty
    E last();
New to GrepCode? Check out our FAQ X