get subset of elements from set in circular way [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Consider a set with like 100 elements.
Set<String> originalSet; //[1....100] size is 100
From originalSet, (m) subset of elements of some size(n) with some starting index(i) have to retrieved.
Example:
m = 4, n = 45, i = 1
Following have to be retrieved
subset1[1-45], subset2[46-90], subset3[91-35], subset4[36-80]
Whats the best way of doing this.

First of all, Set is unordered so it does not make sense to talk about indices etc. List would make more sense here.
Next, you have to be explicit about what you mean by "best". Performance on insertion? Random access? Creation of your n-from-i subset? These are important question to choose the implementation.
I think two primary options would be linked list with special handling of the last element in subList operation or an array-based list.

Assuming your set has some notion of order, you could write this with
Iterable<Iterable<String>> slices =
Iterables.limit(
Iterables.partition(
Iterables.skip(
Iterables‌​.cycle(originalSet),
i),
n),
m);
If you wanted a set out of this, you'd have to do a transform or something; if you have Java 8 that'd just be something like Iterables.transform(..., ImmutableSet::copyOf).

Related

Are there any Java collection types that are unordered but sorted? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
Ordered collections in Java can be sorted or unsorted, but is it possible to have the reverse? Can unordered collections ever be sorted?
Any pointers are appreciated. Thanks.
I think we need to define what we mean by ordered and sorted.
I assume that:
by order you mean insertion order
by sorted you mean sorting according to a comparator
In that case, yes. A NavigableSet/SortedSet such as TreeSet does not remember the insert order but will keep (and return) the items offered in their natural order.

I need 2 denominational array sorted [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am working on a project that has a list of student name and numbers, for example
James Bloggs,1
Paul Jonson,43
Andt Peters,23
Once I have them in an array I then need them sorted.
What is the best way of going about this. Its not the sort Im stuck on its the referencing the names to the numbers. I would have thought if I do a 2 denominational array only one would be sorted.
Any help would be great,
EDIT: I just realized this question was asking about a 2-dimensional array and my answer doesn't directly deal with that. I am skeptical that arrays should be involved at all. Arrays are usually for dealing with primitive data, and maybe if you are coming from a C background you'd think they'd be the natural thing to use. If you really honestly have to use arrays then this probably isn't the way to go.
https://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html
public void foo(){
// Use a TreeMap. It will sort keys on insertion.
Map<Integer,String> nameByNumber = new TreeMap<>();
nameByNumber.put(1, "James Boggs");
// etc. put all the entries in however you need to
List<Integer> sortedNumbers = personByNumber.getKeys();
List<String> namesSortedByNumber = personByNumber.getNames();
}
If you need it to be more organized and complex, you can encapsulate the name and number into a Class with a name and number property. Then you'd still use the number as the key, but you'd have the full class as the value. Do this if you need to have more than just a name, like last name, first name, address, etc.

Is it possible to insert a number into a sorted array, time: log(n) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a sorted array of integers (of size n) and would like to insert a new element into the array, is it possible to do so in O(log(n))? I need to keep an insertion and find computational complexity of O(log(n))).
Right now the only idea I have is to do binary search in order to find the desired index for insertion - this would take O(log(n)), but then I would have to create a new array and copy the entire cells which would take O(n).
EDIT:
It was solved by using an AVL Tree instead, that way any new elements added takes O(log(n)) and finding an element takes O(log(n))
"is it possible to do so in log(n)? " - in short no. From my recollections and experience inserting into an arbitrary position in an array is always O(N), almost by definition. If you want faster performance use something like the TreeMap class.

Take a result of two long numbers using an array in Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to take a sum, quotient, remainder of two numbers using an array in java.
123456789012345+7654321, 123456789012345/7654321. What is a simplest way to calculate it using Java?(I am new to Java.)
Since you are new to java I recommend reading up on some tutorials. As it seems you are not familiar with java in general. An example, which I have not used myself, is http://www.javaworld.com/blog/java-101. It may be worth your time to read this over.
As for your actual question, you would create a variable in java. Then assign your first number to this variable. After doing this, you can perform some operations on the number.
An example in sudo code to give you an idea while not doing the work for you.
void method
var number = 100
number = number + 200
number = number / 20
print("result" . number)
If you plan to use an array its the same process in a loop.
http://www.tutorialspoint.com/java/java_loop_control.htm

Groovy remove value from collection [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In groovy is there a specific way to remove a value from a collection. For example I have a list of form fields but two of them are hidden fields and I'm trying to figure out how to remove them from the collection. The two parameters I'm trying to remove are salesKey and topicSelection. Groovy newbie so code samples are most helpful
request.requestParameterMap.collect { key, value -> "$key: ${value[0].string}" }.join("\n")
key.remove("salesKey")
key.remove("topicSelection")
I think you could use findAll:
request.requestParameterMap.findAll { key, value ->
!( key in ["salesKey", "topicSelection"] )
}
Check out this answer.
Also, depending on your specific aims, there are a couple of other ways to remove a pair, including dropWhile (which is more or less iterating over your data struct) and minus (which isn't so much removing a pair as creating a new structure without the specified pair). Official doc here.

Categories

Resources